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

app_spec.js « components « whats_new « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ad062d04140a094ab1c81fe1d6634effd7ce54cb (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { GlDrawer, GlInfiniteScroll, GlTabs } from '@gitlab/ui';
import { createLocalVue, mount } from '@vue/test-utils';
import Vuex from 'vuex';
import { mockTracking, unmockTracking, triggerEvent } from 'helpers/tracking_helper';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import App from '~/whats_new/components/app.vue';
import { getDrawerBodyHeight } from '~/whats_new/utils/get_drawer_body_height';

const MOCK_DRAWER_BODY_HEIGHT = 42;

jest.mock('~/whats_new/utils/get_drawer_body_height', () => ({
  getDrawerBodyHeight: jest.fn().mockImplementation(() => MOCK_DRAWER_BODY_HEIGHT),
}));

const localVue = createLocalVue();
localVue.use(Vuex);

describe('App', () => {
  let wrapper;
  let store;
  let actions;
  let state;
  let trackingSpy;
  let gitlabDotCom = true;

  const buildProps = () => ({
    storageKey: 'storage-key',
    versions: ['3.11', '3.10'],
    gitlabDotCom,
  });

  const buildWrapper = () => {
    actions = {
      openDrawer: jest.fn(),
      closeDrawer: jest.fn(),
      fetchItems: jest.fn(),
      setDrawerBodyHeight: jest.fn(),
    };

    state = {
      open: true,
      features: [],
      drawerBodyHeight: null,
    };

    store = new Vuex.Store({
      actions,
      state,
    });

    wrapper = mount(App, {
      localVue,
      store,
      propsData: buildProps(),
      directives: {
        GlResizeObserver: createMockDirective(),
      },
    });
  };

  const findInfiniteScroll = () => wrapper.find(GlInfiniteScroll);

  const setup = async () => {
    document.body.dataset.page = 'test-page';
    document.body.dataset.namespaceId = 'namespace-840';

    trackingSpy = mockTracking('_category_', null, jest.spyOn);
    buildWrapper();

    wrapper.vm.$store.state.features = [
      { title: 'Whats New Drawer', url: 'www.url.com', release: 3.11 },
    ];
    wrapper.vm.$store.state.drawerBodyHeight = MOCK_DRAWER_BODY_HEIGHT;
    await wrapper.vm.$nextTick();
  };

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

  describe('gitlab.com', () => {
    beforeEach(() => {
      setup();
    });

    const getDrawer = () => wrapper.find(GlDrawer);

    it('contains a drawer', () => {
      expect(getDrawer().exists()).toBe(true);
    });

    it('dispatches openDrawer and tracking calls when mounted', () => {
      expect(actions.openDrawer).toHaveBeenCalledWith(expect.any(Object), 'storage-key');
      expect(trackingSpy).toHaveBeenCalledWith(undefined, 'click_whats_new_drawer', {
        label: 'namespace_id',
        value: 'namespace-840',
      });
    });

    it('dispatches closeDrawer when clicking close', () => {
      getDrawer().vm.$emit('close');
      expect(actions.closeDrawer).toHaveBeenCalled();
    });

    it.each([true, false])('passes open property', async (openState) => {
      wrapper.vm.$store.state.open = openState;

      await wrapper.vm.$nextTick();

      expect(getDrawer().props('open')).toBe(openState);
    });

    it('renders features when provided via ajax', () => {
      expect(actions.fetchItems).toHaveBeenCalled();
      expect(wrapper.find('[data-test-id="feature-title"]').text()).toBe('Whats New Drawer');
    });

    it('send an event when feature item is clicked', () => {
      trackingSpy = mockTracking('_category_', wrapper.element, jest.spyOn);

      const link = wrapper.find('.whats-new-item-title-link');
      triggerEvent(link.element);

      expect(trackingSpy.mock.calls[1]).toMatchObject([
        '_category_',
        'click_whats_new_item',
        {
          label: 'Whats New Drawer',
          property: 'www.url.com',
        },
      ]);
    });

    it('renders infinite scroll', () => {
      const scroll = findInfiniteScroll();

      expect(scroll.props()).toMatchObject({
        fetchedItems: wrapper.vm.$store.state.features.length,
        maxListHeight: MOCK_DRAWER_BODY_HEIGHT,
      });
    });

    describe('bottomReached', () => {
      const emitBottomReached = () => findInfiniteScroll().vm.$emit('bottomReached');

      beforeEach(() => {
        actions.fetchItems.mockClear();
      });

      it('when nextPage exists it calls fetchItems', () => {
        wrapper.vm.$store.state.pageInfo = { nextPage: 840 };
        emitBottomReached();

        expect(actions.fetchItems).toHaveBeenCalledWith(expect.anything(), { page: 840 });
      });

      it('when nextPage does not exist it does not call fetchItems', () => {
        wrapper.vm.$store.state.pageInfo = { nextPage: null };
        emitBottomReached();

        expect(actions.fetchItems).not.toHaveBeenCalled();
      });
    });

    it('calls getDrawerBodyHeight and setDrawerBodyHeight when resize directive is triggered', () => {
      const { value } = getBinding(getDrawer().element, 'gl-resize-observer');

      value();

      expect(getDrawerBodyHeight).toHaveBeenCalledWith(wrapper.find(GlDrawer).element);

      expect(actions.setDrawerBodyHeight).toHaveBeenCalledWith(
        expect.any(Object),
        MOCK_DRAWER_BODY_HEIGHT,
      );
    });
  });

  describe('self managed', () => {
    const findTabs = () => wrapper.find(GlTabs);

    const clickSecondTab = async () => {
      const secondTab = wrapper.findAll('.nav-link').at(1);
      await secondTab.trigger('click');
      await new Promise((resolve) => requestAnimationFrame(resolve));
    };

    beforeEach(() => {
      gitlabDotCom = false;
      setup();
    });

    it('renders tabs with drawer body height and content', () => {
      const scroll = findInfiniteScroll();
      const tabs = findTabs();

      expect(scroll.exists()).toBe(false);
      expect(tabs.attributes().style).toBe(`height: ${MOCK_DRAWER_BODY_HEIGHT}px;`);
      expect(wrapper.find('h5').text()).toBe('Whats New Drawer');
    });

    describe('fetchVersion', () => {
      beforeEach(() => {
        actions.fetchItems.mockClear();
      });

      it('when version isnt fetched, clicking a tab calls fetchItems', async () => {
        const fetchVersionSpy = jest.spyOn(wrapper.vm, 'fetchVersion');
        await clickSecondTab();

        expect(fetchVersionSpy).toHaveBeenCalledWith('3.10');
        expect(actions.fetchItems).toHaveBeenCalledWith(expect.anything(), { version: '3.10' });
      });

      it('when version has been fetched, clicking a tab calls fetchItems', async () => {
        wrapper.vm.$store.state.features.push({ title: 'GitLab Stories', release: 3.1 });
        await wrapper.vm.$nextTick();

        const fetchVersionSpy = jest.spyOn(wrapper.vm, 'fetchVersion');
        await clickSecondTab();

        expect(fetchVersionSpy).toHaveBeenCalledWith('3.10');
        expect(actions.fetchItems).not.toHaveBeenCalled();
        expect(wrapper.find('.tab-pane.active h5').text()).toBe('GitLab Stories');
      });
    });
  });
});