summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/components/jobs/list_spec.js
blob: 8797e07aef1b50f067f0b268446a27d6bb0729d6 (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
import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount, mount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import StageList from '~/ide/components/jobs/list.vue';
import Stage from '~/ide/components/jobs/stage.vue';

const localVue = createLocalVue();
localVue.use(Vuex);
const storeActions = {
  fetchJobs: jest.fn(),
  toggleStageCollapsed: jest.fn(),
  setDetailJob: jest.fn(),
};

const store = new Vuex.Store({
  modules: {
    pipelines: {
      namespaced: true,
      actions: storeActions,
    },
  },
});

describe('IDE stages list', () => {
  let wrapper;

  const defaultProps = {
    stages: [],
    loading: false,
  };

  const stages = ['build', 'test', 'deploy'].map((name, id) => ({
    id,
    name,
    jobs: [],
    status: { icon: 'status_success' },
  }));

  const createComponent = (props) => {
    wrapper = shallowMount(StageList, {
      propsData: {
        ...defaultProps,
        ...props,
      },
      localVue,
      store,
    });
  };

  afterEach(() => {
    Object.values(storeActions).forEach((actionMock) => actionMock.mockClear());
  });

  afterAll(() => {
    wrapper.destroy();
    wrapper = null;
  });

  it('renders loading icon when no stages & loading', () => {
    createComponent({ loading: true, stages: [] });

    expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
  });

  it('renders stages components for each stage', () => {
    createComponent({ stages });
    expect(wrapper.findAll(Stage).length).toBe(stages.length);
  });

  it('triggers fetchJobs action when stage emits fetch event', () => {
    createComponent({ stages });
    wrapper.find(Stage).vm.$emit('fetch');
    expect(storeActions.fetchJobs).toHaveBeenCalled();
  });

  it('triggers toggleStageCollapsed action when stage emits toggleCollapsed event', () => {
    createComponent({ stages });
    wrapper.find(Stage).vm.$emit('toggleCollapsed');
    expect(storeActions.toggleStageCollapsed).toHaveBeenCalled();
  });

  it('triggers setDetailJob action when stage emits clickViewLog event', () => {
    createComponent({ stages });
    wrapper.find(Stage).vm.$emit('clickViewLog');
    expect(storeActions.setDetailJob).toHaveBeenCalled();
  });

  describe('integration tests', () => {
    const findCardHeader = () => wrapper.find('.card-header');

    beforeEach(() => {
      wrapper = mount(StageList, {
        propsData: { ...defaultProps, stages },
        store,
        localVue,
      });
    });

    it('calls toggleStageCollapsed when clicking stage header', () => {
      findCardHeader().trigger('click');

      expect(storeActions.toggleStageCollapsed).toHaveBeenCalledWith(expect.any(Object), 0);
    });

    it('calls fetchJobs when stage is mounted', () => {
      expect(storeActions.fetchJobs.mock.calls.map(([, stage]) => stage)).toEqual(stages);
    });
  });
});