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

runner_type_tabs_spec.js « components « runner « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 22d2a9e60f7256549c6ca0ad8ef8f88fec5dbe15 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { GlTab } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import RunnerTypeTabs from '~/runner/components/runner_type_tabs.vue';
import RunnerCount from '~/runner/components/stat/runner_count.vue';
import { INSTANCE_TYPE, GROUP_TYPE, PROJECT_TYPE } from '~/runner/constants';

const mockSearch = { runnerType: null, filters: [], pagination: { page: 1 }, sort: 'CREATED_DESC' };

const mockCount = (type, multiplier = 1) => {
  let count;
  switch (type) {
    case INSTANCE_TYPE:
      count = 3;
      break;
    case GROUP_TYPE:
      count = 2;
      break;
    case PROJECT_TYPE:
      count = 1;
      break;
    default:
      count = 6;
      break;
  }
  return count * multiplier;
};

describe('RunnerTypeTabs', () => {
  let wrapper;

  const findTabs = () => wrapper.findAll(GlTab);
  const findActiveTab = () =>
    findTabs()
      .filter((tab) => tab.attributes('active') === 'true')
      .at(0);
  const getTabsTitles = () => findTabs().wrappers.map((tab) => tab.text().replace(/\s+/g, ' '));

  const createComponent = ({ props, stubs, ...options } = {}) => {
    wrapper = shallowMount(RunnerTypeTabs, {
      propsData: {
        value: mockSearch,
        countScope: INSTANCE_TYPE,
        countVariables: {},
        ...props,
      },
      stubs: {
        GlTab,
        ...stubs,
      },
      ...options,
    });
  };

  afterEach(() => {
    wrapper.destroy();
  });

  it('Renders all options to filter runners by default', () => {
    createComponent();

    expect(getTabsTitles()).toEqual(['All', 'Instance', 'Group', 'Project']);
  });

  it('Shows count when receiving a number', () => {
    createComponent({
      stubs: {
        RunnerCount: {
          props: ['variables'],
          render() {
            return this.$scopedSlots.default({
              count: mockCount(this.variables.type),
            });
          },
        },
      },
    });

    expect(getTabsTitles()).toEqual([`All 6`, `Instance 3`, `Group 2`, `Project 1`]);
  });

  it('Shows formatted count when receiving a large number', () => {
    createComponent({
      stubs: {
        RunnerCount: {
          props: ['variables'],
          render() {
            return this.$scopedSlots.default({
              count: mockCount(this.variables.type, 1000),
            });
          },
        },
      },
    });

    expect(getTabsTitles()).toEqual([
      `All 6,000`,
      `Instance 3,000`,
      `Group 2,000`,
      `Project 1,000`,
    ]);
  });

  it('Renders a count next to each tab', () => {
    const mockVariables = {
      paused: true,
      status: 'ONLINE',
    };

    createComponent({
      props: {
        countVariables: mockVariables,
      },
    });

    findTabs().wrappers.forEach((tab) => {
      expect(tab.find(RunnerCount).props()).toEqual({
        scope: INSTANCE_TYPE,
        skip: false,
        variables: expect.objectContaining(mockVariables),
      });
    });
  });

  it('Renders fewer options to filter runners', () => {
    createComponent({
      props: {
        runnerTypes: [GROUP_TYPE, PROJECT_TYPE],
      },
    });

    expect(getTabsTitles()).toEqual(['All', 'Group', 'Project']);
  });

  it('"All" is selected by default', () => {
    createComponent();

    expect(findActiveTab().text()).toBe('All');
  });

  it('Another tab can be preselected by the user', () => {
    createComponent({
      props: {
        value: {
          ...mockSearch,
          runnerType: INSTANCE_TYPE,
        },
      },
    });

    expect(findActiveTab().text()).toBe('Instance');
  });

  describe('When the user selects a tab', () => {
    const emittedValue = () => wrapper.emitted('input')[0][0];

    beforeEach(() => {
      createComponent();
      findTabs().at(2).vm.$emit('click');
    });

    it(`Runner type is emitted`, () => {
      expect(emittedValue()).toEqual({
        ...mockSearch,
        runnerType: GROUP_TYPE,
      });
    });

    it('Runner type is selected', async () => {
      const newValue = emittedValue();
      await wrapper.setProps({ value: newValue });

      expect(findActiveTab().text()).toBe('Group');
    });
  });

  describe('Component API', () => {
    describe('When .refetch() is called', () => {
      let mockRefetch;

      beforeEach(() => {
        mockRefetch = jest.fn();

        createComponent({
          stubs: {
            RunnerCount: {
              methods: {
                refetch: mockRefetch,
              },
              render() {},
            },
          },
        });

        wrapper.vm.refetch();
      });

      it('refetch is called for each count', () => {
        expect(mockRefetch).toHaveBeenCalledTimes(4);
      });
    });
  });
});