Welcome to mirror list, hosted at ThFree Co, Russian Federation.

local_state_spec.js « graphql « runner « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 915170b53f93d6bfa5b2509e55ef63232cdd3020 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { gql } from '@apollo/client/core';
import createApolloClient from '~/lib/graphql';
import { createLocalState } from '~/runner/graphql/list/local_state';
import getCheckedRunnerIdsQuery from '~/runner/graphql/list/checked_runner_ids.query.graphql';
import { RUNNER_TYPENAME } from '~/runner/constants';

const makeRunner = (id, deleteRunner = true) => ({
  id,
  userPermissions: {
    deleteRunner,
  },
});

describe('~/runner/graphql/list/local_state', () => {
  let localState;
  let apolloClient;

  const createSubject = () => {
    if (apolloClient) {
      throw new Error('test subject already exists!');
    }

    localState = createLocalState();

    const { cacheConfig, typeDefs } = localState;

    apolloClient = createApolloClient({}, { cacheConfig, typeDefs });
  };

  const addMockRunnerToCache = (id) => {
    // mock some runners in the cache to prevent dangling references
    apolloClient.writeFragment({
      id: `${RUNNER_TYPENAME}:${id}`,
      fragment: gql`
        fragment DummyRunner on CiRunner {
          __typename
        }
      `,
      data: {
        __typename: RUNNER_TYPENAME,
      },
    });
  };

  const queryCheckedRunnerIds = () => {
    const { checkedRunnerIds } = apolloClient.readQuery({
      query: getCheckedRunnerIdsQuery,
    });
    return checkedRunnerIds;
  };

  beforeEach(() => {
    createSubject();
  });

  afterEach(() => {
    localState = null;
    apolloClient = null;
  });

  describe('queryCheckedRunnerIds', () => {
    it('has empty checked list by default', () => {
      expect(queryCheckedRunnerIds()).toEqual([]);
    });

    it('returns checked runners that have a reference in the cache', () => {
      const id = 'a';

      addMockRunnerToCache(id);
      localState.localMutations.setRunnerChecked({
        runner: makeRunner(id),
        isChecked: true,
      });

      expect(queryCheckedRunnerIds()).toEqual(['a']);
    });

    it('return checked runners that are not dangling references', () => {
      addMockRunnerToCache('a'); // 'b' is missing from the cache, perhaps because it was deleted
      localState.localMutations.setRunnerChecked({ runner: makeRunner('a'), isChecked: true });
      localState.localMutations.setRunnerChecked({ runner: makeRunner('b'), isChecked: true });

      expect(queryCheckedRunnerIds()).toEqual(['a']);
    });
  });

  describe.each`
    inputs                                                   | expected
    ${[['a', true], ['b', true], ['b', true]]}               | ${['a', 'b']}
    ${[['a', true], ['b', true], ['a', false]]}              | ${['b']}
    ${[['c', true], ['b', true], ['a', true], ['d', false]]} | ${['c', 'b', 'a']}
  `('setRunnerChecked', ({ inputs, expected }) => {
    beforeEach(() => {
      inputs.forEach(([id, isChecked]) => {
        addMockRunnerToCache(id);
        localState.localMutations.setRunnerChecked({ runner: makeRunner(id), isChecked });
      });
    });
    it(`for inputs="${inputs}" has a ids="[${expected}]"`, () => {
      expect(queryCheckedRunnerIds()).toEqual(expected);
    });
  });

  describe.each`
    inputs                                       | expected
    ${[[['a', 'b'], true]]}                      | ${['a', 'b']}
    ${[[['a', 'b'], false]]}                     | ${[]}
    ${[[['a', 'b'], true], [['c', 'd'], true]]}  | ${['a', 'b', 'c', 'd']}
    ${[[['a', 'b'], true], [['a', 'b'], false]]} | ${[]}
    ${[[['a', 'b'], true], [['b'], false]]}      | ${['a']}
  `('setRunnersChecked', ({ inputs, expected }) => {
    beforeEach(() => {
      inputs.forEach(([ids, isChecked]) => {
        ids.forEach(addMockRunnerToCache);

        localState.localMutations.setRunnersChecked({
          runners: ids.map((id) => makeRunner(id)),
          isChecked,
        });
      });
    });

    it(`for inputs="${inputs}" has a ids="[${expected}]"`, () => {
      expect(queryCheckedRunnerIds()).toEqual(expected);
    });
  });

  describe('clearChecked', () => {
    it('clears all checked items', () => {
      ['a', 'b', 'c'].forEach((id) => {
        addMockRunnerToCache(id);
        localState.localMutations.setRunnerChecked({ runner: makeRunner(id), isChecked: true });
      });

      expect(queryCheckedRunnerIds()).toEqual(['a', 'b', 'c']);

      localState.localMutations.clearChecked();

      expect(queryCheckedRunnerIds()).toEqual([]);
    });
  });

  describe('when some runners cannot be deleted', () => {
    beforeEach(() => {
      addMockRunnerToCache('a');
      addMockRunnerToCache('b');
    });

    it('setRunnerChecked does not check runner that cannot be deleted', () => {
      localState.localMutations.setRunnerChecked({
        runner: makeRunner('a', false),
        isChecked: true,
      });

      expect(queryCheckedRunnerIds()).toEqual([]);
    });

    it('setRunnersChecked does not check runner that cannot be deleted', () => {
      localState.localMutations.setRunnersChecked({
        runners: [makeRunner('a', false), makeRunner('b', false)],
        isChecked: true,
      });

      expect(queryCheckedRunnerIds()).toEqual([]);
    });
  });
});