Welcome to mirror list, hosted at ThFree Co, Russian Federation.

catalog_search_spec.js « list « components « catalog « ci « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 803deeb0d45f2fc704da5b89366950d58bc5496e (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
import { GlSearchBoxByClick, GlSorting } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import CatalogSearch from '~/ci/catalog/components/list/catalog_search.vue';
import { SORT_ASC, SORT_DESC, SORT_OPTION_CREATED } from '~/ci/catalog/constants';

describe('CatalogSearch', () => {
  let wrapper;

  const findSearchBar = () => wrapper.findComponent(GlSearchBoxByClick);
  const findSorting = () => wrapper.findComponent(GlSorting);
  const findAllSortingItems = () => findSorting().props('sortOptions');

  const createComponent = () => {
    wrapper = shallowMountExtended(CatalogSearch, {});
  };

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

  describe('default UI', () => {
    it('renders the search bar', () => {
      expect(findSearchBar().exists()).toBe(true);
    });

    it('sets sorting options', () => {
      const sortOptionsProp = findAllSortingItems();
      expect(sortOptionsProp).toHaveLength(1);
      expect(sortOptionsProp[0].text).toBe('Created at');
    });

    it('renders the `Created at` option as the default', () => {
      expect(findSorting().props('text')).toBe('Created at');
    });
  });

  describe('search', () => {
    it('passes down the search value to the search component', async () => {
      const newSearchTerm = 'cat';

      expect(findSearchBar().props().value).toBe('');

      await findSearchBar().vm.$emit('input', newSearchTerm);

      expect(findSearchBar().props().value).toBe(newSearchTerm);
    });

    it('does not submit only when typing', async () => {
      expect(wrapper.emitted('update-search-term')).toBeUndefined();

      await findSearchBar().vm.$emit('input', 'new');

      expect(wrapper.emitted('update-search-term')).toBeUndefined();
    });

    describe('when submitting the search', () => {
      const newSearchTerm = 'dog';

      beforeEach(async () => {
        await findSearchBar().vm.$emit('input', newSearchTerm);
        await findSearchBar().vm.$emit('submit');
      });

      it('emits the event up with the new payload', () => {
        expect(wrapper.emitted('update-search-term')).toEqual([[newSearchTerm]]);
      });
    });

    describe('when clearing the search', () => {
      beforeEach(async () => {
        await findSearchBar().vm.$emit('input', 'new');
        await findSearchBar().vm.$emit('clear');
      });

      it('emits an update event with an empty string payload', () => {
        expect(wrapper.emitted('update-search-term')).toEqual([['']]);
      });
    });
  });

  describe('sort', () => {
    describe('when changing sort order', () => {
      it('changes the `isAscending` prop to the sorting component', async () => {
        expect(findSorting().props().isAscending).toBe(false);

        await findSorting().vm.$emit('sortDirectionChange');

        expect(findSorting().props().isAscending).toBe(true);
      });

      it('emits an `update-sorting` event with the new direction', async () => {
        expect(wrapper.emitted('update-sorting')).toBeUndefined();

        await findSorting().vm.$emit('sortDirectionChange');
        await findSorting().vm.$emit('sortDirectionChange');

        expect(wrapper.emitted('update-sorting')).toEqual([
          [`${SORT_OPTION_CREATED}_${SORT_ASC}`],
          [`${SORT_OPTION_CREATED}_${SORT_DESC}`],
        ]);
      });
    });
  });
});