summaryrefslogtreecommitdiff
path: root/spec/frontend/environments/mixins/environments_pagination_api_mixin_spec.js
blob: b624178e3dbca4e9d0b9794c21fc5c5075db7e08 (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
import { shallowMount } from '@vue/test-utils';
import environmentsPaginationApiMixin from '~/environments/mixins/environments_pagination_api_mixin';

describe('environments_pagination_api_mixin', () => {
  const updateContentMock = jest.fn();
  const mockComponent = {
    template: `
      <div>
        <button id='change-page' @click="changePageClick" />
        <button id='change-tab' @click="changeTabClick" />
      </div>
    `,
    methods: {
      updateContent: updateContentMock,
      changePageClick() {
        this.onChangePage(this.nextPage);
      },
      changeTabClick() {
        this.onChangeTab(this.nextScope);
      },
    },
    data() {
      return {
        scope: 'test',
      };
    },
  };

  let wrapper;

  const createWrapper = ({ scope, nextPage, nextScope }) =>
    shallowMount(mockComponent, {
      mixins: [environmentsPaginationApiMixin],
      data() {
        return {
          nextPage,
          nextScope,
          scope,
        };
      },
    });

  it.each([
    ['test-scope', 2],
    ['test-scope', 10],
    ['test-scope-2', 3],
  ])('should call updateContent when calling onChangePage', async (scopeName, pageNumber) => {
    wrapper = createWrapper({ scope: scopeName, nextPage: pageNumber });

    await wrapper.find('#change-page').trigger('click');

    expect(updateContentMock).toHaveBeenCalledWith({
      scope: scopeName,
      page: pageNumber.toString(),
      nested: true,
    });
  });

  it('should call updateContent when calling onChageTab', async () => {
    wrapper = createWrapper({ nextScope: 'stopped' });
    await wrapper.find('#change-tab').trigger('click');

    expect(updateContentMock).toHaveBeenCalledWith({
      scope: 'stopped',
      page: '1',
      nested: true,
    });
  });
});