summaryrefslogtreecommitdiff
path: root/spec/javascripts/frequent_items/components/frequent_items_search_input_spec.js
blob: ddbbc5c2d29572a5f6b873a17e7bdb398510629d (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
import Vue from 'vue';
import searchComponent from '~/frequent_items/components/frequent_items_search_input.vue';
import eventHub from '~/frequent_items/event_hub';
import { shallowMount } from '@vue/test-utils';

const createComponent = (namespace = 'projects') => {
  const Component = Vue.extend(searchComponent);

  return shallowMount(Component, { propsData: { namespace } });
};

describe('FrequentItemsSearchInputComponent', () => {
  let wrapper;
  let vm;

  beforeEach(() => {
    wrapper = createComponent();

    ({ vm } = wrapper);
  });

  afterEach(() => {
    vm.$destroy();
  });

  describe('methods', () => {
    describe('setFocus', () => {
      it('should set focus to search input', () => {
        spyOn(vm.$refs.search, 'focus');

        vm.setFocus();

        expect(vm.$refs.search.focus).toHaveBeenCalled();
      });
    });
  });

  describe('mounted', () => {
    it('should listen `dropdownOpen` event', done => {
      spyOn(eventHub, '$on');
      const vmX = createComponent().vm;

      Vue.nextTick(() => {
        expect(eventHub.$on).toHaveBeenCalledWith(
          `${vmX.namespace}-dropdownOpen`,
          jasmine.any(Function),
        );
        done();
      });
    });
  });

  describe('beforeDestroy', () => {
    it('should unbind event listeners on eventHub', done => {
      const vmX = createComponent().vm;
      spyOn(eventHub, '$off');

      vmX.$mount();
      vmX.$destroy();

      Vue.nextTick(() => {
        expect(eventHub.$off).toHaveBeenCalledWith(
          `${vmX.namespace}-dropdownOpen`,
          jasmine.any(Function),
        );
        done();
      });
    });
  });

  describe('template', () => {
    it('should render component element', () => {
      expect(wrapper.classes()).toContain('search-input-container');
      expect(wrapper.contains('input.form-control')).toBe(true);
      expect(wrapper.contains('.search-icon')).toBe(true);
      expect(wrapper.find('input.form-control').attributes('placeholder')).toBe(
        'Search your projects',
      );
    });
  });
});