diff options
Diffstat (limited to 'spec/frontend/search/store')
-rw-r--r-- | spec/frontend/search/store/actions_spec.js | 79 | ||||
-rw-r--r-- | spec/frontend/search/store/utils_spec.js | 86 |
2 files changed, 109 insertions, 56 deletions
diff --git a/spec/frontend/search/store/actions_spec.js b/spec/frontend/search/store/actions_spec.js index 3755f8ffae7..9f8c83f2873 100644 --- a/spec/frontend/search/store/actions_spec.js +++ b/spec/frontend/search/store/actions_spec.js @@ -17,6 +17,7 @@ import { MOCK_GROUP, FRESH_STORED_DATA, MOCK_FRESH_DATA_RES, + PRELOAD_EXPECTED_MUTATIONS, PROMISE_ALL_EXPECTED_MUTATIONS, } from '../mock_data'; @@ -68,31 +69,31 @@ describe('Global Search Store Actions', () => { }); describe.each` - action | axiosMock | type | expectedMutations | flashCallCount | lsKey - ${actions.loadFrequentGroups} | ${{ method: 'onGet', code: 200 }} | ${'success'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.initGroups, PROMISE_ALL_EXPECTED_MUTATIONS.resGroups]} | ${0} | ${GROUPS_LOCAL_STORAGE_KEY} - ${actions.loadFrequentGroups} | ${{ method: 'onGet', code: 500 }} | ${'error'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.initGroups]} | ${1} | ${GROUPS_LOCAL_STORAGE_KEY} - ${actions.loadFrequentProjects} | ${{ method: 'onGet', code: 200 }} | ${'success'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.initProjects, PROMISE_ALL_EXPECTED_MUTATIONS.resProjects]} | ${0} | ${PROJECTS_LOCAL_STORAGE_KEY} - ${actions.loadFrequentProjects} | ${{ method: 'onGet', code: 500 }} | ${'error'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.initProjects]} | ${1} | ${PROJECTS_LOCAL_STORAGE_KEY} - `( - 'Promise.all calls', - ({ action, axiosMock, type, expectedMutations, flashCallCount, lsKey }) => { - describe(action.name, () => { - describe(`on ${type}`, () => { - beforeEach(() => { - storeUtils.loadDataFromLS = jest.fn().mockReturnValue(FRESH_STORED_DATA); - mock[axiosMock.method]().reply(axiosMock.code, MOCK_FRESH_DATA_RES); - }); + action | axiosMock | type | expectedMutations | flashCallCount + ${actions.loadFrequentGroups} | ${{ method: 'onGet', code: 200 }} | ${'success'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.resGroups]} | ${0} + ${actions.loadFrequentGroups} | ${{ method: 'onGet', code: 500 }} | ${'error'} | ${[]} | ${1} + ${actions.loadFrequentProjects} | ${{ method: 'onGet', code: 200 }} | ${'success'} | ${[PROMISE_ALL_EXPECTED_MUTATIONS.resProjects]} | ${0} + ${actions.loadFrequentProjects} | ${{ method: 'onGet', code: 500 }} | ${'error'} | ${[]} | ${1} + `('Promise.all calls', ({ action, axiosMock, type, expectedMutations, flashCallCount }) => { + describe(action.name, () => { + describe(`on ${type}`, () => { + beforeEach(() => { + state.frequentItems = { + [GROUPS_LOCAL_STORAGE_KEY]: FRESH_STORED_DATA, + [PROJECTS_LOCAL_STORAGE_KEY]: FRESH_STORED_DATA, + }; + + mock[axiosMock.method]().reply(axiosMock.code, MOCK_FRESH_DATA_RES); + }); - it(`should dispatch the correct mutations`, () => { - return testAction({ action, state, expectedMutations }).then(() => { - expect(storeUtils.loadDataFromLS).toHaveBeenCalledWith(lsKey); - flashCallback(flashCallCount); - }); + it(`should dispatch the correct mutations`, () => { + return testAction({ action, state, expectedMutations }).then(() => { + flashCallback(flashCallCount); }); }); }); - }, - ); + }); + }); describe('getGroupsData', () => { const mockCommit = () => {}; @@ -182,14 +183,38 @@ describe('Global Search Store Actions', () => { }); }); + describe('preloadStoredFrequentItems', () => { + beforeEach(() => { + storeUtils.loadDataFromLS = jest.fn().mockReturnValue(FRESH_STORED_DATA); + }); + + it('calls preloadStoredFrequentItems for both groups and projects and commits LOAD_FREQUENT_ITEMS', async () => { + await testAction({ + action: actions.preloadStoredFrequentItems, + state, + expectedMutations: PRELOAD_EXPECTED_MUTATIONS, + }); + + expect(storeUtils.loadDataFromLS).toHaveBeenCalledTimes(2); + expect(storeUtils.loadDataFromLS).toHaveBeenCalledWith(GROUPS_LOCAL_STORAGE_KEY); + expect(storeUtils.loadDataFromLS).toHaveBeenCalledWith(PROJECTS_LOCAL_STORAGE_KEY); + }); + }); + describe('setFrequentGroup', () => { beforeEach(() => { - storeUtils.setFrequentItemToLS = jest.fn(); + storeUtils.setFrequentItemToLS = jest.fn().mockReturnValue(FRESH_STORED_DATA); }); - it(`calls setFrequentItemToLS with ${GROUPS_LOCAL_STORAGE_KEY} and item data`, async () => { + it(`calls setFrequentItemToLS with ${GROUPS_LOCAL_STORAGE_KEY} and item data then commits LOAD_FREQUENT_ITEMS`, async () => { await testAction({ action: actions.setFrequentGroup, + expectedMutations: [ + { + type: types.LOAD_FREQUENT_ITEMS, + payload: { key: GROUPS_LOCAL_STORAGE_KEY, data: FRESH_STORED_DATA }, + }, + ], payload: MOCK_GROUP, state, }); @@ -204,12 +229,18 @@ describe('Global Search Store Actions', () => { describe('setFrequentProject', () => { beforeEach(() => { - storeUtils.setFrequentItemToLS = jest.fn(); + storeUtils.setFrequentItemToLS = jest.fn().mockReturnValue(FRESH_STORED_DATA); }); it(`calls setFrequentItemToLS with ${PROJECTS_LOCAL_STORAGE_KEY} and item data`, async () => { await testAction({ action: actions.setFrequentProject, + expectedMutations: [ + { + type: types.LOAD_FREQUENT_ITEMS, + payload: { key: PROJECTS_LOCAL_STORAGE_KEY, data: FRESH_STORED_DATA }, + }, + ], payload: MOCK_PROJECT, state, }); diff --git a/spec/frontend/search/store/utils_spec.js b/spec/frontend/search/store/utils_spec.js index 5055fa2cc3d..cd7f7dc3b5f 100644 --- a/spec/frontend/search/store/utils_spec.js +++ b/spec/frontend/search/store/utils_spec.js @@ -51,19 +51,25 @@ describe('Global Search Store Utils', () => { describe('setFrequentItemToLS', () => { const frequentItems = {}; + let res; describe('with existing data', () => { describe(`when frequency is less than ${MAX_FREQUENCY}`, () => { beforeEach(() => { frequentItems[MOCK_LS_KEY] = [{ ...MOCK_GROUPS[0], frequency: 1, lastUsed: PREV_TIME }]; - setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]); + res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]); }); - it('adds 1 to the frequency, tracks lastUsed, and calls localStorage.setItem', () => { + it('adds 1 to the frequency, tracks lastUsed, calls localStorage.setItem and returns the array', () => { + const updatedFrequentItems = [ + { ...MOCK_GROUPS[0], frequency: 2, lastUsed: CURRENT_TIME }, + ]; + expect(localStorage.setItem).toHaveBeenCalledWith( MOCK_LS_KEY, - JSON.stringify([{ ...MOCK_GROUPS[0], frequency: 2, lastUsed: CURRENT_TIME }]), + JSON.stringify(updatedFrequentItems), ); + expect(res).toEqual(updatedFrequentItems); }); }); @@ -72,16 +78,19 @@ describe('Global Search Store Utils', () => { frequentItems[MOCK_LS_KEY] = [ { ...MOCK_GROUPS[0], frequency: MAX_FREQUENCY, lastUsed: PREV_TIME }, ]; - setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]); + res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]); }); - it(`does not further increase frequency past ${MAX_FREQUENCY}, tracks lastUsed, and calls localStorage.setItem`, () => { + it(`does not further increase frequency past ${MAX_FREQUENCY}, tracks lastUsed, calls localStorage.setItem, and returns the array`, () => { + const updatedFrequentItems = [ + { ...MOCK_GROUPS[0], frequency: MAX_FREQUENCY, lastUsed: CURRENT_TIME }, + ]; + expect(localStorage.setItem).toHaveBeenCalledWith( MOCK_LS_KEY, - JSON.stringify([ - { ...MOCK_GROUPS[0], frequency: MAX_FREQUENCY, lastUsed: CURRENT_TIME }, - ]), + JSON.stringify(updatedFrequentItems), ); + expect(res).toEqual(updatedFrequentItems); }); }); }); @@ -89,14 +98,17 @@ describe('Global Search Store Utils', () => { describe('with no existing data', () => { beforeEach(() => { frequentItems[MOCK_LS_KEY] = []; - setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]); + res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]); }); - it('adds a new entry with frequency 1, tracks lastUsed, and calls localStorage.setItem', () => { + it('adds a new entry with frequency 1, tracks lastUsed, calls localStorage.setItem, and returns the array', () => { + const updatedFrequentItems = [{ ...MOCK_GROUPS[0], frequency: 1, lastUsed: CURRENT_TIME }]; + expect(localStorage.setItem).toHaveBeenCalledWith( MOCK_LS_KEY, - JSON.stringify([{ ...MOCK_GROUPS[0], frequency: 1, lastUsed: CURRENT_TIME }]), + JSON.stringify(updatedFrequentItems), ); + expect(res).toEqual(updatedFrequentItems); }); }); @@ -107,18 +119,21 @@ describe('Global Search Store Utils', () => { { id: 2, frequency: 1, lastUsed: PREV_TIME }, { id: 3, frequency: 1, lastUsed: PREV_TIME }, ]; - setFrequentItemToLS(MOCK_LS_KEY, frequentItems, { id: 3 }); + res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, { id: 3 }); }); - it('sorts the array by most frequent and lastUsed', () => { + it('sorts the array by most frequent and lastUsed and returns the array', () => { + const updatedFrequentItems = [ + { id: 3, frequency: 2, lastUsed: CURRENT_TIME }, + { id: 1, frequency: 2, lastUsed: PREV_TIME }, + { id: 2, frequency: 1, lastUsed: PREV_TIME }, + ]; + expect(localStorage.setItem).toHaveBeenCalledWith( MOCK_LS_KEY, - JSON.stringify([ - { id: 3, frequency: 2, lastUsed: CURRENT_TIME }, - { id: 1, frequency: 2, lastUsed: PREV_TIME }, - { id: 2, frequency: 1, lastUsed: PREV_TIME }, - ]), + JSON.stringify(updatedFrequentItems), ); + expect(res).toEqual(updatedFrequentItems); }); }); @@ -131,31 +146,35 @@ describe('Global Search Store Utils', () => { { id: 4, frequency: 2, lastUsed: PREV_TIME }, { id: 5, frequency: 1, lastUsed: PREV_TIME }, ]; - setFrequentItemToLS(MOCK_LS_KEY, frequentItems, { id: 6 }); + res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, { id: 6 }); }); - it('removes the last item in the array', () => { + it('removes the last item in the array and returns the array', () => { + const updatedFrequentItems = [ + { id: 1, frequency: 5, lastUsed: PREV_TIME }, + { id: 2, frequency: 4, lastUsed: PREV_TIME }, + { id: 3, frequency: 3, lastUsed: PREV_TIME }, + { id: 4, frequency: 2, lastUsed: PREV_TIME }, + { id: 6, frequency: 1, lastUsed: CURRENT_TIME }, + ]; + expect(localStorage.setItem).toHaveBeenCalledWith( MOCK_LS_KEY, - JSON.stringify([ - { id: 1, frequency: 5, lastUsed: PREV_TIME }, - { id: 2, frequency: 4, lastUsed: PREV_TIME }, - { id: 3, frequency: 3, lastUsed: PREV_TIME }, - { id: 4, frequency: 2, lastUsed: PREV_TIME }, - { id: 6, frequency: 1, lastUsed: CURRENT_TIME }, - ]), + JSON.stringify(updatedFrequentItems), ); + expect(res).toEqual(updatedFrequentItems); }); }); describe('with null data loaded in', () => { beforeEach(() => { frequentItems[MOCK_LS_KEY] = null; - setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]); + res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]); }); - it('wipes local storage', () => { + it('wipes local storage and returns empty array', () => { expect(localStorage.removeItem).toHaveBeenCalledWith(MOCK_LS_KEY); + expect(res).toEqual([]); }); }); @@ -163,14 +182,17 @@ describe('Global Search Store Utils', () => { beforeEach(() => { const MOCK_ADDITIONAL_DATA_GROUP = { ...MOCK_GROUPS[0], extraData: 'test' }; frequentItems[MOCK_LS_KEY] = []; - setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_ADDITIONAL_DATA_GROUP); + res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_ADDITIONAL_DATA_GROUP); }); - it('parses out extra data for LS', () => { + it('parses out extra data for LS and returns the array', () => { + const updatedFrequentItems = [{ ...MOCK_GROUPS[0], frequency: 1, lastUsed: CURRENT_TIME }]; + expect(localStorage.setItem).toHaveBeenCalledWith( MOCK_LS_KEY, - JSON.stringify([{ ...MOCK_GROUPS[0], frequency: 1, lastUsed: CURRENT_TIME }]), + JSON.stringify(updatedFrequentItems), ); + expect(res).toEqual(updatedFrequentItems); }); }); }); |