summaryrefslogtreecommitdiff
path: root/spec/frontend/boards/components/filtered_search_spec.js
blob: 7f238aa671fb87be1341d70b313f1d8c1dd735a3 (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
import { createLocalVue, shallowMount } from '@vue/test-utils';
import Vuex from 'vuex';
import FilteredSearch from '~/boards/components/filtered_search.vue';
import { createStore } from '~/boards/stores';
import * as commonUtils from '~/lib/utils/common_utils';
import FilteredSearchBarRoot from '~/vue_shared/components/filtered_search_bar/filtered_search_bar_root.vue';

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

describe('FilteredSearch', () => {
  let wrapper;
  let store;

  const createComponent = () => {
    wrapper = shallowMount(FilteredSearch, {
      localVue,
      propsData: { search: '' },
      store,
      attachTo: document.body,
    });
  };

  beforeEach(() => {
    // this needed for actions call for performSearch
    window.gon = { features: {} };
  });

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

  describe('default', () => {
    beforeEach(() => {
      store = createStore();

      jest.spyOn(store, 'dispatch');

      createComponent();
    });

    it('finds FilteredSearch', () => {
      expect(wrapper.find(FilteredSearchBarRoot).exists()).toBe(true);
    });

    describe('when onFilter is emitted', () => {
      it('calls performSearch', () => {
        wrapper.find(FilteredSearchBarRoot).vm.$emit('onFilter', [{ value: { data: '' } }]);

        expect(store.dispatch).toHaveBeenCalledWith('performSearch');
      });

      it('calls historyPushState', () => {
        commonUtils.historyPushState = jest.fn();
        wrapper
          .find(FilteredSearchBarRoot)
          .vm.$emit('onFilter', [{ value: { data: 'searchQuery' } }]);

        expect(commonUtils.historyPushState).toHaveBeenCalledWith(
          'http://test.host/?search=searchQuery',
        );
      });
    });
  });
});