summaryrefslogtreecommitdiff
path: root/spec/frontend/import_entities/import_groups/graphql/services/source_groups_manager_spec.js
blob: 5940ea544eae8101021302e17e8966ebf0b51764 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { defaultDataIdFromObject } from 'apollo-cache-inmemory';
import { SourceGroupsManager } from '~/import_entities/import_groups/graphql/services/source_groups_manager';
import ImportSourceGroupFragment from '~/import_entities/import_groups/graphql/fragments/bulk_import_source_group_item.fragment.graphql';
import { clientTypenames } from '~/import_entities/import_groups/graphql/client_factory';

describe('SourceGroupsManager', () => {
  let manager;
  let client;

  const getFakeGroup = () => ({
    __typename: clientTypenames.BulkImportSourceGroup,
    id: 5,
  });

  beforeEach(() => {
    client = {
      readFragment: jest.fn(),
      writeFragment: jest.fn(),
    };

    manager = new SourceGroupsManager({ client });
  });

  it('finds item by group id', () => {
    const ID = 5;

    const FAKE_GROUP = getFakeGroup();
    client.readFragment.mockReturnValue(FAKE_GROUP);
    const group = manager.findById(ID);
    expect(group).toBe(FAKE_GROUP);
    expect(client.readFragment).toHaveBeenCalledWith({
      fragment: ImportSourceGroupFragment,
      id: defaultDataIdFromObject(getFakeGroup()),
    });
  });

  it('updates group with provided function', () => {
    const UPDATED_GROUP = {};
    const fn = jest.fn().mockReturnValue(UPDATED_GROUP);
    manager.update(getFakeGroup(), fn);

    expect(client.writeFragment).toHaveBeenCalledWith({
      fragment: ImportSourceGroupFragment,
      id: defaultDataIdFromObject(getFakeGroup()),
      data: UPDATED_GROUP,
    });
  });

  it('updates group by id with provided function', () => {
    const UPDATED_GROUP = {};
    const fn = jest.fn().mockReturnValue(UPDATED_GROUP);
    client.readFragment.mockReturnValue(getFakeGroup());
    manager.updateById(getFakeGroup().id, fn);

    expect(client.readFragment).toHaveBeenCalledWith({
      fragment: ImportSourceGroupFragment,
      id: defaultDataIdFromObject(getFakeGroup()),
    });

    expect(client.writeFragment).toHaveBeenCalledWith({
      fragment: ImportSourceGroupFragment,
      id: defaultDataIdFromObject(getFakeGroup()),
      data: UPDATED_GROUP,
    });
  });

  it('sets import status when group is provided', () => {
    client.readFragment.mockReturnValue(getFakeGroup());

    const NEW_STATUS = 'NEW_STATUS';
    manager.setImportStatus(getFakeGroup(), NEW_STATUS);

    expect(client.writeFragment).toHaveBeenCalledWith({
      fragment: ImportSourceGroupFragment,
      id: defaultDataIdFromObject(getFakeGroup()),
      data: {
        ...getFakeGroup(),
        status: NEW_STATUS,
      },
    });
  });
});