summaryrefslogtreecommitdiff
path: root/spec/frontend/projects/commit/components/branches_dropdown_spec.js
blob: 7334e007e18d84b2c07989d8ead3be4fa788918b (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
import { GlCollapsibleListbox } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import BranchesDropdown from '~/projects/commit/components/branches_dropdown.vue';

Vue.use(Vuex);

describe('BranchesDropdown', () => {
  let wrapper;
  let store;
  const spyFetchBranches = jest.fn();

  const createComponent = (props, state = { isFetching: false }) => {
    store = new Vuex.Store({
      getters: {
        joinedBranches: () => ['_main_', '_branch_1_', '_branch_2_'],
      },
      actions: {
        fetchBranches: spyFetchBranches,
      },
      state,
    });

    wrapper = extendedWrapper(
      shallowMount(BranchesDropdown, {
        store,
        propsData: {
          value: props.value,
          blanked: props.blanked || false,
        },
      }),
    );
  };
  const findDropdown = () => wrapper.findComponent(GlCollapsibleListbox);

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

  describe('On mount', () => {
    beforeEach(() => {
      createComponent({ value: '' });
    });

    it('invokes fetchBranches', () => {
      expect(spyFetchBranches).toHaveBeenCalled();
    });
  });

  describe('When searching', () => {
    beforeEach(() => {
      createComponent({ value: '' });
    });

    it('invokes fetchBranches', async () => {
      const spy = jest.spyOn(wrapper.vm, 'fetchBranches');

      findDropdown().vm.$emit('search', '_anything_');

      await nextTick();

      expect(spy).toHaveBeenCalledWith('_anything_');
      expect(wrapper.vm.searchTerm).toBe('_anything_');
    });
  });

  describe('Case insensitive for search term', () => {
    beforeEach(() => {
      createComponent({ value: '_BrAnCh_1_' });
    });

    it('returns only the branch searched for', () => {
      expect(findDropdown().props('items')).toEqual([{ text: '_branch_1_', value: '_branch_1_' }]);
    });
  });
});