summaryrefslogtreecommitdiff
path: root/spec/frontend/security_configuration/graphql/cache_utils_spec.js
blob: a40611cc02281c8d70d2dffbe179579f47c2ca46 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {
  updateSecurityTrainingCache,
  updateSecurityTrainingOptimisticResponse,
} from '~/security_configuration/graphql/cache_utils';

describe('EE - Security configuration graphQL cache utils', () => {
  describe('updateSecurityTrainingOptimisticResponse', () => {
    it('returns an optimistic response in the correct shape', () => {
      const changes = { isEnabled: true, isPrimary: true };
      const mutationResponse = updateSecurityTrainingOptimisticResponse(changes);

      expect(mutationResponse).toEqual({
        __typename: 'Mutation',
        securityTrainingUpdate: {
          __typename: 'SecurityTrainingUpdatePayload',
          training: {
            __typename: 'ProjectSecurityTraining',
            ...changes,
          },
          errors: [],
        },
      });
    });
  });

  describe('updateSecurityTrainingCache', () => {
    let mockCache;

    beforeEach(() => {
      // freezing the data makes sure that we don't mutate the original project
      const mockCacheData = Object.freeze({
        project: {
          securityTrainingProviders: [
            { id: 1, isEnabled: true, isPrimary: true },
            { id: 2, isEnabled: true, isPrimary: false },
            { id: 3, isEnabled: false, isPrimary: false },
          ],
        },
      });

      mockCache = {
        readQuery: () => mockCacheData,
        writeQuery: jest.fn(),
      };
    });

    it('does not update the cache when the primary provider is not getting disabled', () => {
      const providerAfterUpdate = {
        id: 2,
        isEnabled: true,
        isPrimary: false,
      };

      updateSecurityTrainingCache({
        query: 'GraphQL query',
        variables: { fullPath: 'gitlab/project' },
      })(mockCache, {
        data: {
          securityTrainingUpdate: {
            training: {
              ...providerAfterUpdate,
            },
          },
        },
      });

      expect(mockCache.writeQuery).not.toHaveBeenCalled();
    });

    it('sets the previous primary provider to be non-primary when another provider gets set as primary', () => {
      const providerAfterUpdate = {
        id: 2,
        isEnabled: true,
        isPrimary: true,
      };

      const expectedTrainingProvidersWrittenToCache = [
        // this was the previous primary primary provider and it should not be primary any longer
        { id: 1, isEnabled: true, isPrimary: false },
        { id: 2, isEnabled: true, isPrimary: true },
        { id: 3, isEnabled: false, isPrimary: false },
      ];

      updateSecurityTrainingCache({
        query: 'GraphQL query',
        variables: { fullPath: 'gitlab/project' },
      })(mockCache, {
        data: {
          securityTrainingUpdate: {
            training: {
              ...providerAfterUpdate,
            },
          },
        },
      });

      expect(mockCache.writeQuery).toHaveBeenCalledWith(
        expect.objectContaining({
          data: {
            project: {
              securityTrainingProviders: expectedTrainingProvidersWrittenToCache,
            },
          },
        }),
      );
    });
  });
});