summaryrefslogtreecommitdiff
path: root/spec/frontend/import_entities/import_groups/graphql/services/source_groups_manager_spec.js
blob: f06babcb149aa572908a09056cacdd96cb13b68e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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,
          },
        ],
      });
    });
  });
});