summaryrefslogtreecommitdiff
path: root/spec/frontend/whats_new/components/app_spec.js
blob: 157faa90efa3a726a542baf4c70e0e325bfaea8e (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
import { createLocalVue, mount } from '@vue/test-utils';
import Vuex from 'vuex';
import { GlDrawer } from '@gitlab/ui';
import { mockTracking, unmockTracking, triggerEvent } from 'helpers/tracking_helper';
import App from '~/whats_new/components/app.vue';

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

describe('App', () => {
  let wrapper;
  let store;
  let actions;
  let state;
  let propsData = { features: '[ {"title":"Whats New Drawer"} ]', storageKey: 'storage-key' };
  let trackingSpy;

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

    state = {
      open: true,
    };

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

    wrapper = mount(App, {
      localVue,
      store,
      propsData,
    });
  };

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

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

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

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

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

  it('dispatches openDrawer 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 as props', () => {
    expect(wrapper.find('h5').text()).toBe('Whats New Drawer');
  });

  it('handles bad json argument gracefully', () => {
    propsData = { features: 'this is not json', storageKey: 'storage-key' };
    buildWrapper();

    expect(getDrawer().exists()).toBe(true);
  });

  it('send an event when feature item is clicked', () => {
    propsData = {
      features: '[ {"title":"Whats New Drawer", "url": "www.url.com"} ]',
      storageKey: 'storage-key',
    };
    buildWrapper();
    trackingSpy = mockTracking('_category_', wrapper.element, jest.spyOn);

    const link = wrapper.find('[data-testid="whats-new-title-link"]');
    triggerEvent(link.element);

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