From 781639bde833e4e09f9f8be35226f7e538ad0fc3 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 18 Jul 2022 12:50:18 -0400 Subject: [PATCH 1/2] Add test for 'fetchMe' action --- app/soapbox/actions/__tests__/me.test.ts | 85 ++++++++++++++++++++++++ app/soapbox/actions/me.ts | 8 +-- 2 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 app/soapbox/actions/__tests__/me.test.ts diff --git a/app/soapbox/actions/__tests__/me.test.ts b/app/soapbox/actions/__tests__/me.test.ts new file mode 100644 index 0000000000..a01105b35b --- /dev/null +++ b/app/soapbox/actions/__tests__/me.test.ts @@ -0,0 +1,85 @@ +import { Map as ImmutableMap } from 'immutable'; + +import { __stub } from 'soapbox/api'; +import { mockStore, rootState } from 'soapbox/jest/test-helpers'; + +import { + fetchMe, +} from '../me'; + +jest.mock('../../storage/kv_store', () => ({ + __esModule: true, + default: { + getItemOrError: jest.fn().mockReturnValue(Promise.resolve({})), + }, +})); + +let store: ReturnType; + +describe('fetchMe()', () => { + describe('without a token', () => { + beforeEach(() => { + const state = rootState; + store = mockStore(state); + }); + + it('dispatches the correct actions', async() => { + const expectedActions = [{ type: 'ME_FETCH_SKIP' }]; + await store.dispatch(fetchMe()); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + + describe('with a token', () => { + const accountUrl = 'accountUrl'; + const token = '123'; + + beforeEach(() => { + const state = rootState + .set('auth', ImmutableMap({ + me: accountUrl, + users: ImmutableMap({ + [accountUrl]: ImmutableMap({ + 'access_token': token, + }), + }), + })) + .set('accounts', ImmutableMap({ + [accountUrl]: { + url: accountUrl, + }, + }) as any); + store = mockStore(state); + }); + + describe('with a successful API response', () => { + beforeEach(() => { + __stub((mock) => { + mock.onGet('/api/v1/accounts/verify_credentials').reply(200, {}); + }); + }); + + it('dispatches the correct actions', async() => { + const expectedActions = [ + { type: 'ME_FETCH_REQUEST' }, + { type: 'AUTH_ACCOUNT_REMEMBER_REQUEST', accountUrl }, + { type: 'ACCOUNTS_IMPORT', accounts: [] }, + { + type: 'AUTH_ACCOUNT_REMEMBER_SUCCESS', + account: {}, + accountUrl, + }, + { type: 'VERIFY_CREDENTIALS_REQUEST', token: '123' }, + { type: 'ACCOUNTS_IMPORT', accounts: [] }, + { type: 'VERIFY_CREDENTIALS_SUCCESS', token: '123', account: {} }, + ]; + await store.dispatch(fetchMe()); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); + }); +}); \ No newline at end of file diff --git a/app/soapbox/actions/me.ts b/app/soapbox/actions/me.ts index 074f5d4cc7..e76399d21c 100644 --- a/app/soapbox/actions/me.ts +++ b/app/soapbox/actions/me.ts @@ -41,13 +41,13 @@ const fetchMe = () => const accountUrl = getMeUrl(state); if (!token) { - dispatch({ type: ME_FETCH_SKIP }); return noOp(); + dispatch({ type: ME_FETCH_SKIP }); + return noOp(); } dispatch(fetchMeRequest()); - return dispatch(loadCredentials(token, accountUrl)).catch(error => { - dispatch(fetchMeFail(error)); - }); + return dispatch(loadCredentials(token, accountUrl)) + .catch(error => dispatch(fetchMeFail(error))); }; /** Update the auth account in IndexedDB for Mastodon, etc. */ From d807aae5021a1c192be581b308957274e6f4f791 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 18 Jul 2022 12:53:57 -0400 Subject: [PATCH 2/2] Add test for 'patchMe' action --- app/soapbox/actions/__tests__/me.test.ts | 32 +++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/app/soapbox/actions/__tests__/me.test.ts b/app/soapbox/actions/__tests__/me.test.ts index a01105b35b..6d37fc68c2 100644 --- a/app/soapbox/actions/__tests__/me.test.ts +++ b/app/soapbox/actions/__tests__/me.test.ts @@ -4,7 +4,7 @@ import { __stub } from 'soapbox/api'; import { mockStore, rootState } from 'soapbox/jest/test-helpers'; import { - fetchMe, + fetchMe, patchMe, } from '../me'; jest.mock('../../storage/kv_store', () => ({ @@ -82,4 +82,34 @@ describe('fetchMe()', () => { }); }); }); +}); + +describe('patchMe()', () => { + beforeEach(() => { + const state = rootState; + store = mockStore(state); + }); + + describe('with a successful API response', () => { + beforeEach(() => { + __stub((mock) => { + mock.onPatch('/api/v1/accounts/update_credentials').reply(200, {}); + }); + }); + + it('dispatches the correct actions', async() => { + const expectedActions = [ + { type: 'ME_PATCH_REQUEST' }, + { type: 'ACCOUNTS_IMPORT', accounts: [] }, + { + type: 'ME_PATCH_SUCCESS', + me: {}, + }, + ]; + await store.dispatch(patchMe({})); + const actions = store.getActions(); + + expect(actions).toEqual(expectedActions); + }); + }); }); \ No newline at end of file