summaryrefslogtreecommitdiff
path: root/spec/frontend/runner/graphql/local_state_spec.js
blob: 5c4302e4aa2824b829015885701bcb2b4a5533ee (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
import createApolloClient from '~/lib/graphql';
import { createLocalState } from '~/runner/graphql/list/local_state';
import getCheckedRunnerIdsQuery from '~/runner/graphql/list/checked_runner_ids.query.graphql';

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 queryCheckedRunnerIds = () => {
    const { checkedRunnerIds } = apolloClient.readQuery({
      query: getCheckedRunnerIdsQuery,
    });
    return checkedRunnerIds;
  };

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

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

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

  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]) => {
        localState.localMutations.setRunnerChecked({ runner: { 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) => {
        localState.localMutations.setRunnerChecked({ runner: { id }, isChecked: true });
      });

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

      localState.localMutations.clearChecked();

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