summaryrefslogtreecommitdiff
path: root/spec/frontend/jobs/components/table/graphql/cache_config_spec.js
blob: ac79186cb46feb42589f5c35d27d9a1ca65993de (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
import cacheConfig from '~/jobs/components/table/graphql/cache_config';
import {
  CIJobConnectionExistingCache,
  CIJobConnectionIncomingCache,
  CIJobConnectionIncomingCacheRunningStatus,
} from '../../../mock_data';

const firstLoadArgs = { first: 3, statuses: 'PENDING' };
const runningArgs = { first: 3, statuses: 'RUNNING' };

describe('jobs/components/table/graphql/cache_config', () => {
  describe('when fetching data with the same statuses', () => {
    it('should contain cache nodes and a status when merging caches on first load', () => {
      const res = cacheConfig.typePolicies.CiJobConnection.merge({}, CIJobConnectionIncomingCache, {
        args: firstLoadArgs,
      });

      expect(res.nodes).toHaveLength(CIJobConnectionIncomingCache.nodes.length);
      expect(res.statuses).toBe('PENDING');
    });

    it('should add to existing caches when merging caches after first load', () => {
      const res = cacheConfig.typePolicies.CiJobConnection.merge(
        CIJobConnectionExistingCache,
        CIJobConnectionIncomingCache,
        {
          args: firstLoadArgs,
        },
      );

      expect(res.nodes).toHaveLength(
        CIJobConnectionIncomingCache.nodes.length + CIJobConnectionExistingCache.nodes.length,
      );
    });

    it('should contain the pageInfo key as part of the result', () => {
      const res = cacheConfig.typePolicies.CiJobConnection.merge({}, CIJobConnectionIncomingCache, {
        args: firstLoadArgs,
      });

      expect(res.pageInfo).toEqual(
        expect.objectContaining({
          __typename: 'PageInfo',
          endCursor: 'eyJpZCI6IjIwNTEifQ',
          hasNextPage: true,
          hasPreviousPage: false,
          startCursor: 'eyJpZCI6IjIxNzMifQ',
        }),
      );
    });
  });

  describe('when fetching data with different statuses', () => {
    it('should reset cache when a cache already exists', () => {
      const res = cacheConfig.typePolicies.CiJobConnection.merge(
        CIJobConnectionExistingCache,
        CIJobConnectionIncomingCacheRunningStatus,
        {
          args: runningArgs,
        },
      );

      expect(res.nodes).not.toEqual(CIJobConnectionExistingCache.nodes);
      expect(res.nodes).toHaveLength(CIJobConnectionIncomingCacheRunningStatus.nodes.length);
    });
  });
});