summaryrefslogtreecommitdiff
path: root/spec/frontend/packages/list/coming_soon/packages_coming_soon_spec.js
blob: c4cdadc45e65b7590e58057461a9ae027e4a2072 (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
import { GlEmptyState, GlSkeletonLoader, GlLabel } from '@gitlab/ui';
import { mount, createLocalVue } from '@vue/test-utils';
import VueApollo, { ApolloQuery } from 'vue-apollo';
import ComingSoon from '~/packages/list/coming_soon/packages_coming_soon.vue';
import { TrackingActions } from '~/packages/shared/constants';
import { asViewModel } from './mock_data';
import Tracking from '~/tracking';

jest.mock('~/packages/list/coming_soon/helpers.js');

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

describe('packages_coming_soon', () => {
  let wrapper;

  const findSkeletonLoader = () => wrapper.find(GlSkeletonLoader);
  const findAllIssues = () => wrapper.findAll('[data-testid="issue-row"]');
  const findIssuesData = () =>
    findAllIssues().wrappers.map(x => {
      const titleLink = x.find('[data-testid="issue-title-link"]');
      const milestone = x.find('[data-testid="milestone"]');
      const issueIdLink = x.find('[data-testid="issue-id-link"]');
      const labels = x.findAll(GlLabel);

      const issueId = Number(issueIdLink.text().substr(1));

      return {
        id: issueId,
        iid: issueId,
        title: titleLink.text(),
        webUrl: titleLink.attributes('href'),
        labels: labels.wrappers.map(label => ({
          color: label.props('backgroundColor'),
          title: label.props('title'),
          scoped: label.props('scoped'),
        })),
        ...(milestone.exists() ? { milestone: { title: milestone.text() } } : {}),
      };
    });
  const findIssueTitleLink = () => wrapper.find('[data-testid="issue-title-link"]');
  const findIssueIdLink = () => wrapper.find('[data-testid="issue-id-link"]');
  const findEmptyState = () => wrapper.find(GlEmptyState);

  const mountComponent = (testParams = {}) => {
    const $apolloData = {
      loading: testParams.isLoading || false,
    };

    wrapper = mount(ComingSoon, {
      localVue,
      propsData: {
        illustration: 'foo',
        projectPath: 'foo',
        suggestedContributionsPath: 'foo',
      },
      stubs: {
        ApolloQuery,
        GlLink: true,
      },
      mocks: {
        $apolloData,
      },
    });

    // Mock the GraphQL query result
    wrapper.find(ApolloQuery).setData({
      result: {
        data: testParams.issues || asViewModel,
      },
    });
  };

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

  describe('when loading', () => {
    beforeEach(() => mountComponent({ isLoading: true }));

    it('renders the skeleton loader', () => {
      expect(findSkeletonLoader().exists()).toBe(true);
    });
  });

  describe('when there are no issues', () => {
    beforeEach(() => mountComponent({ issues: [] }));

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

  describe('when there are issues', () => {
    beforeEach(() => mountComponent());

    it('renders each issue', () => {
      expect(findIssuesData()).toEqual(asViewModel);
    });
  });

  describe('tracking', () => {
    const firstIssue = asViewModel[0];
    let eventSpy;

    beforeEach(() => {
      eventSpy = jest.spyOn(Tracking, 'event');
      mountComponent();
    });

    it('tracks when mounted', () => {
      expect(eventSpy).toHaveBeenCalledWith(undefined, TrackingActions.COMING_SOON_REQUESTED, {});
    });

    it('tracks when an issue title link is clicked', () => {
      eventSpy.mockClear();

      findIssueTitleLink().vm.$emit('click');

      expect(eventSpy).toHaveBeenCalledWith(undefined, TrackingActions.COMING_SOON_LIST, {
        label: firstIssue.title,
        value: firstIssue.iid,
      });
    });

    it('tracks when an issue id link is clicked', () => {
      eventSpy.mockClear();

      findIssueIdLink().vm.$emit('click');

      expect(eventSpy).toHaveBeenCalledWith(undefined, TrackingActions.COMING_SOON_LIST, {
        label: firstIssue.title,
        value: firstIssue.iid,
      });
    });
  });
});