summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/filtered_search_bar/tokens/epic_token_spec.js
blob: addc058f65810fc2f4976a0407cf3980116bdd4d (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
import { GlFilteredSearchToken, GlFilteredSearchTokenSegment } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import waitForPromises from 'helpers/wait_for_promises';
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';

import EpicToken from '~/vue_shared/components/filtered_search_bar/tokens/epic_token.vue';

import { mockEpicToken, mockEpics } from '../mock_data';

jest.mock('~/flash');

const defaultStubs = {
  Portal: true,
  GlFilteredSearchSuggestionList: {
    template: '<div></div>',
    methods: {
      getValue: () => '=',
    },
  },
};

function createComponent(options = {}) {
  const {
    config = mockEpicToken,
    value = { data: '' },
    active = false,
    stubs = defaultStubs,
  } = options;
  return mount(EpicToken, {
    propsData: {
      config,
      value,
      active,
    },
    provide: {
      portalName: 'fake target',
      alignSuggestions: function fakeAlignSuggestions() {},
      suggestionsListClass: 'custom-class',
    },
    stubs,
  });
}

describe('EpicToken', () => {
  let mock;
  let wrapper;

  beforeEach(() => {
    mock = new MockAdapter(axios);
    wrapper = createComponent();
  });

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

  describe('computed', () => {
    beforeEach(async () => {
      wrapper = createComponent({
        data: {
          epics: mockEpics,
        },
      });

      await wrapper.vm.$nextTick();
    });

    describe('activeEpic', () => {
      it('returns object for currently present `value.data`', async () => {
        wrapper.setProps({
          value: { data: `${mockEpics[0].iid}` },
        });

        await wrapper.vm.$nextTick();

        expect(wrapper.vm.activeEpic).toEqual(mockEpics[0]);
      });
    });
  });

  describe('methods', () => {
    describe('fetchEpicsBySearchTerm', () => {
      it('calls `config.fetchEpics` with provided searchTerm param', () => {
        jest.spyOn(wrapper.vm.config, 'fetchEpics');

        wrapper.vm.fetchEpicsBySearchTerm('foo');

        expect(wrapper.vm.config.fetchEpics).toHaveBeenCalledWith('foo');
      });

      it('sets response to `epics` when request is successful', async () => {
        jest.spyOn(wrapper.vm.config, 'fetchEpics').mockResolvedValue({
          data: mockEpics,
        });

        wrapper.vm.fetchEpicsBySearchTerm();

        await waitForPromises();

        expect(wrapper.vm.epics).toEqual(mockEpics);
      });

      it('calls `createFlash` with flash error message when request fails', async () => {
        jest.spyOn(wrapper.vm.config, 'fetchEpics').mockRejectedValue({});

        wrapper.vm.fetchEpicsBySearchTerm('foo');

        await waitForPromises();

        expect(createFlash).toHaveBeenCalledWith({
          message: 'There was a problem fetching epics.',
        });
      });

      it('sets `loading` to false when request completes', async () => {
        jest.spyOn(wrapper.vm.config, 'fetchEpics').mockRejectedValue({});

        wrapper.vm.fetchEpicsBySearchTerm('foo');

        await waitForPromises();

        expect(wrapper.vm.loading).toBe(false);
      });
    });
  });

  describe('template', () => {
    beforeEach(async () => {
      wrapper = createComponent({
        value: { data: `${mockEpics[0].iid}` },
        data: { epics: mockEpics },
      });

      await wrapper.vm.$nextTick();
    });

    it('renders gl-filtered-search-token component', () => {
      expect(wrapper.find(GlFilteredSearchToken).exists()).toBe(true);
    });

    it('renders token item when value is selected', () => {
      const tokenSegments = wrapper.findAll(GlFilteredSearchTokenSegment);

      expect(tokenSegments).toHaveLength(3);
      expect(tokenSegments.at(2).text()).toBe(`${mockEpics[0].title}::&${mockEpics[0].iid}`);
    });
  });
});