summaryrefslogtreecommitdiff
path: root/spec/frontend/issues/show/components/fields/type_spec.js
blob: 27ac0e1baf3e249c82738f937b139abb2b967b34 (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
import { GlFormGroup, GlListbox, GlIcon } from '@gitlab/ui';
import { mount, 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 IssueTypeField, { i18n } from '~/issues/show/components/fields/type.vue';
import { issuableTypes } from '~/issues/show/constants';
import {
  getIssueStateQueryResponse,
  updateIssueStateQueryResponse,
} from '../../mock_data/apollo_mock';

Vue.use(VueApollo);

describe('Issue type field component', () => {
  let wrapper;
  let fakeApollo;
  let mockIssueStateData;

  const mockResolvers = {
    Query: {
      issueState() {
        return {
          __typename: 'IssueState',
          rawData: mockIssueStateData(),
        };
      },
    },
    Mutation: {
      updateIssueState: jest.fn().mockResolvedValue(updateIssueStateQueryResponse),
    },
  };

  const findListBox = () => wrapper.findComponent(GlListbox);
  const findFormGroup = () => wrapper.findComponent(GlFormGroup);
  const findAllIssueItems = () => wrapper.findAll('[data-testid="issue-type-list-item"]');
  const findIssueItemAt = (at) => findAllIssueItems().at(at);
  const findIssueItemAtIcon = (at) => findAllIssueItems().at(at).findComponent(GlIcon);

  const createComponent = (mountFn = mount, { data } = {}, provide) => {
    fakeApollo = createMockApollo([], mockResolvers);

    wrapper = mountFn(IssueTypeField, {
      apolloProvider: fakeApollo,
      data() {
        return {
          issueState: {},
          ...data,
        };
      },
      provide: {
        canCreateIncident: true,
        ...provide,
      },
    });
  };

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

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

  it.each`
    at   | text                     | icon
    ${0} | ${issuableTypes[0].text} | ${issuableTypes[0].icon}
    ${1} | ${issuableTypes[1].text} | ${issuableTypes[1].icon}
  `(`renders the issue type $text with an icon in the dropdown`, ({ at, text, icon }) => {
    createComponent();

    expect(findIssueItemAtIcon(at).props('name')).toBe(icon);
    expect(findIssueItemAt(at).text()).toBe(text);
  });

  it('renders a form group with the correct label', () => {
    createComponent(shallowMount);

    expect(findFormGroup().attributes('label')).toBe(i18n.label);
  });

  it('renders a form select with the `issue_type` value', () => {
    createComponent();

    expect(findListBox().attributes('value')).toBe(issuableTypes.issue);
  });

  describe('with Apollo cache mock', () => {
    it('renders the selected issueType', async () => {
      createComponent();

      mockIssueStateData.mockResolvedValue(getIssueStateQueryResponse);
      await waitForPromises();
      expect(findListBox().attributes('value')).toBe(issuableTypes.issue);
    });

    it('updates the `issue_type` in the apollo cache when the value is changed', async () => {
      createComponent();

      wrapper.vm.$emit('select', issuableTypes.incident);
      await nextTick();
      expect(findListBox().attributes('value')).toBe(issuableTypes.incident);
    });

    describe('when user is a guest', () => {
      it('hides the incident type from the dropdown', async () => {
        createComponent(mount, {}, { canCreateIncident: false, issueType: 'issue' });

        await waitForPromises();

        expect(findIssueItemAt(0).isVisible()).toBe(true);
        expect(findIssueItemAt(1).isVisible()).toBe(false);
        expect(findListBox().attributes('value')).toBe(issuableTypes.issue);
      });

      it('and incident is selected, includes incident in the dropdown', async () => {
        createComponent(mount, {}, { canCreateIncident: false, issueType: 'incident' });

        await waitForPromises();

        expect(findIssueItemAt(0).isVisible()).toBe(true);
        expect(findIssueItemAt(1).isVisible()).toBe(true);
        expect(findListBox().attributes('value')).toBe(issuableTypes.incident);
      });
    });
  });
});