From 14d614e055e801545e711c09905f6d4da08d8c1c Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 2 Jun 2022 10:52:00 -0400 Subject: [PATCH] Start adding 'fetchAccountByUsername' tests --- .../actions/__tests__/accounts.test.ts | 74 ++++++++++++++++++- app/soapbox/actions/accounts.js | 8 +- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/app/soapbox/actions/__tests__/accounts.test.ts b/app/soapbox/actions/__tests__/accounts.test.ts index 75d0b7a64..c8e094907 100644 --- a/app/soapbox/actions/__tests__/accounts.test.ts +++ b/app/soapbox/actions/__tests__/accounts.test.ts @@ -5,7 +5,7 @@ import { mockStore } from 'soapbox/jest/test-helpers'; import rootReducer from 'soapbox/reducers'; import { normalizeAccount } from '../../normalizers'; -import { createAccount, fetchAccount } from '../accounts'; +import { createAccount, fetchAccount, fetchAccountByUsername } from '../accounts'; let store; @@ -130,3 +130,75 @@ describe('fetchAccount()', () => { }); }); }); + +describe('fetchAccountByUsername()', () => { + const id = '123'; + const username = 'tiger'; + let state, account; + + describe('when the account has already been cached in redux', () => { + beforeEach(() => { + account = normalizeAccount({ + id, + acct: username, + display_name: 'Tiger', + avatar: 'test.jpg', + birthday: undefined, + }); + + state = rootReducer(undefined, {}) + .set('accounts', ImmutableMap({ + [id]: account, + })); + + store = mockStore(state); + + __stub((mock) => { + mock.onGet(`/api/v1/accounts/${id}`).reply(200, account); + }); + }); + + it('should return null', async() => { + const result = await store.dispatch(fetchAccountByUsername(username)); + const actions = store.getActions(); + + expect(actions).toEqual([]); + expect(result).toBeNull(); + }); + }); + + describe('when "accountByUsername" feature is enabled', () => { + beforeEach(() => { + const state = rootReducer(undefined, {}) + .set('soapbox', ImmutableMap({ accountByUsername: true })) + .set('instance', { + version: '2.7.2 (compatible; Pleroma 2.4.52-1337-g4779199e.gleasonator+soapbox)', + pleroma: ImmutableMap({ + metadata: ImmutableMap({ + features: [], + }), + }), + }) + .set('me', '123'); + store = mockStore(state); + + __stub((mock) => { + mock.onGet(`/api/v1/accounts/${username}`).reply(200, account); + mock.onGet(`/api/v1/accounts/relationships?${[account.id].map(id => `id[]=${id}`).join('&')}`); + }); + }); + + it('should return dispatch the proper actions', async() => { + await store.dispatch(fetchAccountByUsername(username)); + const actions = store.getActions(); + + expect(actions[0]).toEqual({ + type: 'RELATIONSHIPS_FETCH_REQUEST', + ids: ['123'], + skipLoading: true, + }); + expect(actions[1].type).toEqual('ACCOUNTS_IMPORT'); + expect(actions[2].type).toEqual('ACCOUNT_FETCH_SUCCESS'); + }); + }); +}); diff --git a/app/soapbox/actions/accounts.js b/app/soapbox/actions/accounts.js index 56210c1d0..b93f0e599 100644 --- a/app/soapbox/actions/accounts.js +++ b/app/soapbox/actions/accounts.js @@ -160,7 +160,7 @@ export function fetchAccountByUsername(username) { if (account) { dispatch(fetchAccount(account.get('id'))); - return; + return null; } const instance = state.get('instance'); @@ -168,7 +168,7 @@ export function fetchAccountByUsername(username) { const me = state.get('me'); if (features.accountByUsername && (me || !features.accountLookup)) { - api(getState).get(`/api/v1/accounts/${username}`).then(response => { + return api(getState).get(`/api/v1/accounts/${username}`).then(response => { dispatch(fetchRelationships([response.data.id])); dispatch(importFetchedAccount(response.data)); dispatch(fetchAccountSuccess(response.data)); @@ -177,14 +177,14 @@ export function fetchAccountByUsername(username) { dispatch(importErrorWhileFetchingAccountByUsername(username)); }); } else if (features.accountLookup) { - dispatch(accountLookup(username)).then(account => { + return dispatch(accountLookup(username)).then(account => { dispatch(fetchAccountSuccess(account)); }).catch(error => { dispatch(fetchAccountFail(null, error)); dispatch(importErrorWhileFetchingAccountByUsername(username)); }); } else { - dispatch(accountSearch({ + return dispatch(accountSearch({ q: username, limit: 5, resolve: true,