summaryrefslogtreecommitdiff
path: root/spec/frontend/jobs/bridge/app_spec.js
blob: 210dcfa364b7b96a8159ffcd3afe2c7a37654660 (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
import Vue, { nextTick } from 'vue';
import { shallowMount } from '@vue/test-utils';

import { GlBreakpointInstance } from '@gitlab/ui/dist/utils';
import { GlLoadingIcon } from '@gitlab/ui';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import getPipelineQuery from '~/jobs/bridge/graphql/queries/pipeline.query.graphql';
import waitForPromises from 'helpers/wait_for_promises';
import BridgeApp from '~/jobs/bridge/app.vue';
import BridgeEmptyState from '~/jobs/bridge/components/empty_state.vue';
import BridgeSidebar from '~/jobs/bridge/components/sidebar.vue';
import CiHeader from '~/vue_shared/components/header_ci_component.vue';
import {
  MOCK_BUILD_ID,
  MOCK_PIPELINE_IID,
  MOCK_PROJECT_FULL_PATH,
  mockPipelineQueryResponse,
} from './mock_data';

describe('Bridge Show Page', () => {
  let wrapper;
  let mockApollo;
  let mockPipelineQuery;

  const createComponent = (options) => {
    wrapper = shallowMount(BridgeApp, {
      provide: {
        buildId: MOCK_BUILD_ID,
        projectFullPath: MOCK_PROJECT_FULL_PATH,
        pipelineIid: MOCK_PIPELINE_IID,
      },
      mocks: {
        $apollo: {
          queries: {
            pipeline: {
              loading: true,
            },
          },
        },
      },
      ...options,
    });
  };

  const createComponentWithApollo = () => {
    const handlers = [[getPipelineQuery, mockPipelineQuery]];
    Vue.use(VueApollo);
    mockApollo = createMockApollo(handlers);

    createComponent({
      apolloProvider: mockApollo,
      mocks: {},
    });
  };

  const findCiHeader = () => wrapper.findComponent(CiHeader);
  const findEmptyState = () => wrapper.findComponent(BridgeEmptyState);
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findSidebar = () => wrapper.findComponent(BridgeSidebar);

  beforeEach(() => {
    mockPipelineQuery = jest.fn();
  });

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

  describe('while pipeline query is loading', () => {
    beforeEach(() => {
      createComponent();
    });

    it('renders loading icon', () => {
      expect(findLoadingIcon().exists()).toBe(true);
    });
  });

  describe('after pipeline query is loaded', () => {
    beforeEach(async () => {
      mockPipelineQuery.mockResolvedValue(mockPipelineQueryResponse);
      createComponentWithApollo();
      await waitForPromises();
    });

    it('query is called with correct variables', async () => {
      expect(mockPipelineQuery).toHaveBeenCalledTimes(1);
      expect(mockPipelineQuery).toHaveBeenCalledWith({
        fullPath: MOCK_PROJECT_FULL_PATH,
        iid: MOCK_PIPELINE_IID,
      });
    });

    it('renders CI header state', () => {
      expect(findCiHeader().exists()).toBe(true);
    });

    it('renders empty state', () => {
      expect(findEmptyState().exists()).toBe(true);
    });

    it('renders sidebar', () => {
      expect(findSidebar().exists()).toBe(true);
    });
  });

  describe('sidebar expansion', () => {
    beforeEach(async () => {
      mockPipelineQuery.mockResolvedValue(mockPipelineQueryResponse);
      createComponentWithApollo();
      await waitForPromises();
    });

    describe('on resize', () => {
      it.each`
        breakpoint | isSidebarExpanded
        ${'xs'}    | ${false}
        ${'sm'}    | ${false}
        ${'md'}    | ${true}
        ${'lg'}    | ${true}
        ${'xl'}    | ${true}
      `(
        'sets isSidebarExpanded to `$isSidebarExpanded` when the breakpoint is "$breakpoint"',
        async ({ breakpoint, isSidebarExpanded }) => {
          jest.spyOn(GlBreakpointInstance, 'getBreakpointSize').mockReturnValue(breakpoint);

          window.dispatchEvent(new Event('resize'));
          await nextTick();

          expect(findSidebar().exists()).toBe(isSidebarExpanded);
        },
      );
    });

    it('toggles expansion on button click', async () => {
      expect(findSidebar().exists()).toBe(true);

      wrapper.vm.toggleSidebar();
      await nextTick();

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