summaryrefslogtreecommitdiff
path: root/spec/frontend/pipeline_schedules/components/pipeline_schedules_spec.js
blob: cce8f48092812533fa0b69c9501419232084608e (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
import { GlAlert, GlLoadingIcon, GlModal } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import PipelineSchedules from '~/pipeline_schedules/components/pipeline_schedules.vue';
import PipelineSchedulesTable from '~/pipeline_schedules/components/table/pipeline_schedules_table.vue';
import deletePipelineScheduleMutation from '~/pipeline_schedules/graphql/mutations/delete_pipeline_schedule.mutation.graphql';
import getPipelineSchedulesQuery from '~/pipeline_schedules/graphql/queries/get_pipeline_schedules.query.graphql';
import {
  mockGetPipelineSchedulesGraphQLResponse,
  mockPipelineScheduleNodes,
  deleteMutationResponse,
} from '../mock_data';

Vue.use(VueApollo);

describe('Pipeline schedules app', () => {
  let wrapper;

  const successHandler = jest.fn().mockResolvedValue(mockGetPipelineSchedulesGraphQLResponse);
  const failedHandler = jest.fn().mockRejectedValue(new Error('GraphQL error'));

  const deleteMutationHandlerSuccess = jest.fn().mockResolvedValue(deleteMutationResponse);
  const deleteMutationHandlerFailed = jest.fn().mockRejectedValue(new Error('GraphQL error'));

  const createMockApolloProvider = (
    requestHandlers = [[getPipelineSchedulesQuery, successHandler]],
  ) => {
    return createMockApollo(requestHandlers);
  };

  const createComponent = (requestHandlers) => {
    wrapper = shallowMount(PipelineSchedules, {
      provide: {
        fullPath: 'gitlab-org/gitlab',
      },
      apolloProvider: createMockApolloProvider(requestHandlers),
    });
  };

  const findTable = () => wrapper.findComponent(PipelineSchedulesTable);
  const findAlert = () => wrapper.findComponent(GlAlert);
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findModal = () => wrapper.findComponent(GlModal);

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

  it('displays table', async () => {
    createComponent();

    await waitForPromises();

    expect(findTable().exists()).toBe(true);
    expect(findAlert().exists()).toBe(false);
  });

  it('fetches query and passes an array of pipeline schedules', async () => {
    createComponent();

    expect(successHandler).toHaveBeenCalled();

    await waitForPromises();

    expect(findTable().props('schedules')).toEqual(mockPipelineScheduleNodes);
  });

  it('handles loading state', async () => {
    createComponent();

    expect(findLoadingIcon().exists()).toBe(true);

    await waitForPromises();

    expect(findLoadingIcon().exists()).toBe(false);
  });

  it('shows query error alert', async () => {
    createComponent([[getPipelineSchedulesQuery, failedHandler]]);

    await waitForPromises();

    expect(findAlert().text()).toBe('There was a problem fetching pipeline schedules.');
  });

  it('shows delete mutation error alert', async () => {
    createComponent([
      [getPipelineSchedulesQuery, successHandler],
      [deletePipelineScheduleMutation, deleteMutationHandlerFailed],
    ]);

    await waitForPromises();

    findModal().vm.$emit('primary');

    await waitForPromises();

    expect(findAlert().text()).toBe('There was a problem deleting the pipeline schedule.');
  });

  it('deletes pipeline schedule and refetches query', async () => {
    createComponent([
      [getPipelineSchedulesQuery, successHandler],
      [deletePipelineScheduleMutation, deleteMutationHandlerSuccess],
    ]);

    jest.spyOn(wrapper.vm.$apollo.queries.schedules, 'refetch');

    await waitForPromises();

    const scheduleId = mockPipelineScheduleNodes[0].id;

    findTable().vm.$emit('showDeleteModal', scheduleId);

    expect(wrapper.vm.$apollo.queries.schedules.refetch).not.toHaveBeenCalled();

    findModal().vm.$emit('primary');

    await waitForPromises();

    expect(deleteMutationHandlerSuccess).toHaveBeenCalledWith({
      id: scheduleId,
    });
    expect(wrapper.vm.$apollo.queries.schedules.refetch).toHaveBeenCalled();
  });

  it('modal should be visible after event', async () => {
    createComponent();

    await waitForPromises();

    expect(findModal().props('visible')).toBe(false);

    findTable().vm.$emit('showDeleteModal', mockPipelineScheduleNodes[0].id);

    await nextTick();

    expect(findModal().props('visible')).toBe(true);
  });

  it('modal should be hidden', async () => {
    createComponent();

    await waitForPromises();

    findTable().vm.$emit('showDeleteModal', mockPipelineScheduleNodes[0].id);

    await nextTick();

    expect(findModal().props('visible')).toBe(true);

    findModal().vm.$emit('hide');

    await nextTick();

    expect(findModal().props('visible')).toBe(false);
  });
});