diff options
Diffstat (limited to 'spec/frontend/import_entities/import_groups/graphql/services')
3 files changed, 61 insertions, 166 deletions
diff --git a/spec/frontend/import_entities/import_groups/graphql/services/local_storage_cache_spec.js b/spec/frontend/import_entities/import_groups/graphql/services/local_storage_cache_spec.js new file mode 100644 index 00000000000..b44a2767ad8 --- /dev/null +++ b/spec/frontend/import_entities/import_groups/graphql/services/local_storage_cache_spec.js @@ -0,0 +1,61 @@ +import { + KEY, + LocalStorageCache, +} from '~/import_entities/import_groups/graphql/services/local_storage_cache'; + +describe('Local storage cache', () => { + let cache; + let storage; + + beforeEach(() => { + storage = { + getItem: jest.fn(), + setItem: jest.fn(), + }; + + cache = new LocalStorageCache({ storage }); + }); + + describe('storage management', () => { + const IMPORT_URL = 'http://fake.url'; + + it('loads state from storage on creation', () => { + expect(storage.getItem).toHaveBeenCalledWith(KEY); + }); + + it('saves to storage when set is called', () => { + const STORAGE_CONTENT = { fake: 'content ' }; + cache.set(IMPORT_URL, STORAGE_CONTENT); + expect(storage.setItem).toHaveBeenCalledWith( + KEY, + JSON.stringify({ [IMPORT_URL]: STORAGE_CONTENT }), + ); + }); + + it('updates status by job id', () => { + const CHANGED_STATUS = 'changed'; + const JOB_ID = 2; + + cache.set(IMPORT_URL, { + progress: { + id: JOB_ID, + status: 'original', + }, + }); + + cache.updateStatusByJobId(JOB_ID, CHANGED_STATUS); + + expect(storage.setItem).toHaveBeenCalledWith( + KEY, + JSON.stringify({ + [IMPORT_URL]: { + progress: { + id: JOB_ID, + status: CHANGED_STATUS, + }, + }, + }), + ); + }); + }); +}); diff --git a/spec/frontend/import_entities/import_groups/graphql/services/source_groups_manager_spec.js b/spec/frontend/import_entities/import_groups/graphql/services/source_groups_manager_spec.js deleted file mode 100644 index f06babcb149..00000000000 --- a/spec/frontend/import_entities/import_groups/graphql/services/source_groups_manager_spec.js +++ /dev/null @@ -1,64 +0,0 @@ -import { - KEY, - SourceGroupsManager, -} from '~/import_entities/import_groups/graphql/services/source_groups_manager'; - -const FAKE_SOURCE_URL = 'http://demo.host'; - -describe('SourceGroupsManager', () => { - let manager; - let storage; - - beforeEach(() => { - storage = { - getItem: jest.fn(), - setItem: jest.fn(), - }; - - manager = new SourceGroupsManager({ storage, sourceUrl: FAKE_SOURCE_URL }); - }); - - describe('storage management', () => { - const IMPORT_ID = 1; - const IMPORT_TARGET = { new_name: 'demo', target_namespace: 'foo' }; - const STATUS = 'FAKE_STATUS'; - const FAKE_GROUP = { id: 1, import_target: IMPORT_TARGET, status: STATUS }; - - it('loads state from storage on creation', () => { - expect(storage.getItem).toHaveBeenCalledWith(KEY); - }); - - it('saves to storage when createImportState is called', () => { - const FAKE_STATUS = 'fake;'; - manager.createImportState(IMPORT_ID, { status: FAKE_STATUS, groups: [FAKE_GROUP] }); - const storedObject = JSON.parse(storage.setItem.mock.calls[0][1]); - expect(Object.values(storedObject)[0]).toStrictEqual({ - status: FAKE_STATUS, - groups: [ - { - id: FAKE_GROUP.id, - importTarget: IMPORT_TARGET, - }, - ], - }); - }); - - it('updates storage when previous state is available', () => { - const CHANGED_STATUS = 'changed'; - - manager.createImportState(IMPORT_ID, { status: STATUS, groups: [FAKE_GROUP] }); - - manager.updateImportProgress(IMPORT_ID, CHANGED_STATUS); - const storedObject = JSON.parse(storage.setItem.mock.calls[1][1]); - expect(Object.values(storedObject)[0]).toStrictEqual({ - status: CHANGED_STATUS, - groups: [ - { - id: FAKE_GROUP.id, - importTarget: IMPORT_TARGET, - }, - ], - }); - }); - }); -}); diff --git a/spec/frontend/import_entities/import_groups/graphql/services/status_poller_spec.js b/spec/frontend/import_entities/import_groups/graphql/services/status_poller_spec.js deleted file mode 100644 index 9c47647c430..00000000000 --- a/spec/frontend/import_entities/import_groups/graphql/services/status_poller_spec.js +++ /dev/null @@ -1,102 +0,0 @@ -import MockAdapter from 'axios-mock-adapter'; -import Visibility from 'visibilityjs'; -import createFlash from '~/flash'; -import { STATUSES } from '~/import_entities/constants'; -import { StatusPoller } from '~/import_entities/import_groups/graphql/services/status_poller'; -import axios from '~/lib/utils/axios_utils'; -import Poll from '~/lib/utils/poll'; - -jest.mock('visibilityjs'); -jest.mock('~/flash'); -jest.mock('~/lib/utils/poll'); -jest.mock('~/import_entities/import_groups/graphql/services/source_groups_manager', () => ({ - SourceGroupsManager: jest.fn().mockImplementation(function mock() { - this.setImportStatus = jest.fn(); - this.findByImportId = jest.fn(); - }), -})); - -const FAKE_POLL_PATH = '/fake/poll/path'; - -describe('Bulk import status poller', () => { - let poller; - let mockAdapter; - let updateImportStatus; - - const getPollHistory = () => mockAdapter.history.get.filter((x) => x.url === FAKE_POLL_PATH); - - beforeEach(() => { - mockAdapter = new MockAdapter(axios); - mockAdapter.onGet(FAKE_POLL_PATH).reply(200, {}); - updateImportStatus = jest.fn(); - poller = new StatusPoller({ updateImportStatus, pollPath: FAKE_POLL_PATH }); - }); - - it('creates poller with proper config', () => { - expect(Poll.mock.calls).toHaveLength(1); - const [[pollConfig]] = Poll.mock.calls; - expect(typeof pollConfig.method).toBe('string'); - - const pollOperation = pollConfig.resource[pollConfig.method]; - expect(typeof pollOperation).toBe('function'); - }); - - it('invokes axios when polling is performed', async () => { - const [[pollConfig]] = Poll.mock.calls; - const pollOperation = pollConfig.resource[pollConfig.method]; - expect(getPollHistory()).toHaveLength(0); - - pollOperation(); - await axios.waitForAll(); - - expect(getPollHistory()).toHaveLength(1); - }); - - it('subscribes to visibility changes', () => { - expect(Visibility.change).toHaveBeenCalled(); - }); - - it.each` - isHidden | action - ${true} | ${'stop'} - ${false} | ${'restart'} - `('$action polling when hidden is $isHidden', ({ action, isHidden }) => { - const [pollInstance] = Poll.mock.instances; - const [[changeHandler]] = Visibility.change.mock.calls; - Visibility.hidden.mockReturnValue(isHidden); - expect(pollInstance[action]).not.toHaveBeenCalled(); - - changeHandler(); - - expect(pollInstance[action]).toHaveBeenCalled(); - }); - - it('does not perform polling when constructed', async () => { - await axios.waitForAll(); - - expect(getPollHistory()).toHaveLength(0); - }); - - it('immediately start polling when requested', async () => { - const [pollInstance] = Poll.mock.instances; - - poller.startPolling(); - - expect(pollInstance.makeRequest).toHaveBeenCalled(); - }); - - it('when error occurs shows flash with error', () => { - const [[pollConfig]] = Poll.mock.calls; - pollConfig.errorCallback(); - expect(createFlash).toHaveBeenCalled(); - }); - - it('when success response arrives updates relevant group status', () => { - const FAKE_ID = 5; - const [[pollConfig]] = Poll.mock.calls; - const FAKE_RESPONSE = { id: FAKE_ID, status_name: STATUSES.FINISHED }; - pollConfig.successCallback({ data: [FAKE_RESPONSE] }); - - expect(updateImportStatus).toHaveBeenCalledWith(FAKE_RESPONSE); - }); -}); |