summaryrefslogtreecommitdiff
path: root/spec/frontend/runner/components/runner_bulk_delete_checkbox_spec.js
blob: 424a4e61ccd74b18b199cb724d0af1ad87292cd9 (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
import Vue from 'vue';
import { GlFormCheckbox } from '@gitlab/ui';
import VueApollo from 'vue-apollo';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import RunnerBulkDeleteCheckbox from '~/runner/components/runner_bulk_delete_checkbox.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import { createLocalState } from '~/runner/graphql/list/local_state';

Vue.use(VueApollo);

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

// Multi-select checkbox possible states:
const stateToAttrs = {
  unchecked: { disabled: undefined, checked: undefined, indeterminate: undefined },
  checked: { disabled: undefined, checked: 'true', indeterminate: undefined },
  indeterminate: { disabled: undefined, checked: undefined, indeterminate: 'true' },
  disabled: { disabled: 'true', checked: undefined, indeterminate: undefined },
};

describe('RunnerBulkDeleteCheckbox', () => {
  let wrapper;
  let mockState;
  let mockCheckedRunnerIds;

  const findCheckbox = () => wrapper.findComponent(GlFormCheckbox);

  const expectCheckboxToBe = (state) => {
    const expected = stateToAttrs[state];
    expect(findCheckbox().attributes('disabled')).toBe(expected.disabled);
    expect(findCheckbox().attributes('checked')).toBe(expected.checked);
    expect(findCheckbox().attributes('indeterminate')).toBe(expected.indeterminate);
  };

  const createComponent = ({ runners = [] } = {}) => {
    const { cacheConfig, localMutations } = mockState;
    const apolloProvider = createMockApollo(undefined, undefined, cacheConfig);

    wrapper = shallowMountExtended(RunnerBulkDeleteCheckbox, {
      apolloProvider,
      provide: {
        localMutations,
      },
      propsData: {
        runners,
      },
    });
  };

  beforeEach(() => {
    mockState = createLocalState();

    jest
      .spyOn(mockState.cacheConfig.typePolicies.Query.fields, 'checkedRunnerIds')
      .mockImplementation(() => mockCheckedRunnerIds);

    jest.spyOn(mockState.localMutations, 'setRunnersChecked');
  });

  describe('when all runners can be deleted', () => {
    const mockIds = ['1', '2', '3'];
    const mockIdAnotherPage = '4';
    const mockRunners = mockIds.map((id) => makeRunner(id));

    it.each`
      case                         | checkedRunnerIds                   | state
      ${'no runners'}              | ${[]}                              | ${'unchecked'}
      ${'no runners in this page'} | ${[mockIdAnotherPage]}             | ${'unchecked'}
      ${'all runners'}             | ${mockIds}                         | ${'checked'}
      ${'some runners'}            | ${[mockIds[0]]}                    | ${'indeterminate'}
      ${'all plus other runners'}  | ${[...mockIds, mockIdAnotherPage]} | ${'checked'}
    `('if $case are checked, checkbox is $state', ({ checkedRunnerIds, state }) => {
      mockCheckedRunnerIds = checkedRunnerIds;

      createComponent({ runners: mockRunners });
      expectCheckboxToBe(state);
    });
  });

  describe('when some runners cannot be deleted', () => {
    it('all allowed runners are selected, checkbox is checked', () => {
      mockCheckedRunnerIds = ['a', 'b', 'c'];
      createComponent({
        runners: [makeRunner('a'), makeRunner('b'), makeRunner('c', false)],
      });

      expectCheckboxToBe('checked');
    });

    it('some allowed runners are selected, checkbox is indeterminate', () => {
      mockCheckedRunnerIds = ['a', 'b'];
      createComponent({
        runners: [makeRunner('a'), makeRunner('b'), makeRunner('c')],
      });

      expectCheckboxToBe('indeterminate');
    });

    it('no allowed runners are selected, checkbox is disabled', () => {
      mockCheckedRunnerIds = ['a', 'b'];
      createComponent({
        runners: [makeRunner('a', false), makeRunner('b', false)],
      });

      expectCheckboxToBe('disabled');
    });
  });

  describe('When user selects', () => {
    const mockRunners = [makeRunner('1'), makeRunner('2')];

    beforeEach(() => {
      mockCheckedRunnerIds = ['1', '2'];
      createComponent({ runners: mockRunners });
    });

    it.each([[true], [false]])('sets checked to %s', (checked) => {
      findCheckbox().vm.$emit('change', checked);

      expect(mockState.localMutations.setRunnersChecked).toHaveBeenCalledTimes(1);
      expect(mockState.localMutations.setRunnersChecked).toHaveBeenCalledWith({
        isChecked: checked,
        runners: mockRunners,
      });
    });
  });

  describe('When runners are loading', () => {
    beforeEach(() => {
      createComponent();
    });

    it('is disabled', () => {
      expectCheckboxToBe('disabled');
    });
  });
});