summaryrefslogtreecommitdiff
path: root/spec/frontend/frequent_items/components/frequent_items_list_spec.js
blob: 9f08a432a3dda6016fc048984b6523c42af3c236 (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
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import frequentItemsListComponent from '~/frequent_items/components/frequent_items_list.vue';
import frequentItemsListItemComponent from '~/frequent_items/components/frequent_items_list_item.vue';
import { createStore } from '~/frequent_items/store';
import { mockFrequentProjects } from '../mock_data';

Vue.use(Vuex);

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

  const createComponent = (props = {}) => {
    wrapper = mountExtended(frequentItemsListComponent, {
      store: createStore(),
      propsData: {
        namespace: 'projects',
        items: mockFrequentProjects,
        isFetchFailed: false,
        hasSearchQuery: false,
        matcher: 'lab',
        ...props,
      },
      provide: {
        vuexModule: 'frequentProjects',
      },
    });
  };

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

  describe('computed', () => {
    describe('isListEmpty', () => {
      it('should return `true` or `false` representing whether if `items` is empty or not with projects', async () => {
        createComponent({
          items: [],
        });

        expect(wrapper.vm.isListEmpty).toBe(true);

        wrapper.setProps({
          items: mockFrequentProjects,
        });
        await nextTick();

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

    describe('fetched item messages', () => {
      it('should return appropriate empty list message based on value of `localStorageFailed` prop with projects', async () => {
        createComponent({
          isFetchFailed: true,
        });

        expect(wrapper.vm.listEmptyMessage).toBe(
          'This feature requires browser localStorage support',
        );

        wrapper.setProps({
          isFetchFailed: false,
        });
        await nextTick();

        expect(wrapper.vm.listEmptyMessage).toBe('Projects you visit often will appear here');
      });
    });

    describe('searched item messages', () => {
      it('should return appropriate empty list message based on value of `searchFailed` prop with projects', async () => {
        createComponent({
          hasSearchQuery: true,
          isFetchFailed: true,
        });

        expect(wrapper.vm.listEmptyMessage).toBe('Something went wrong on our end.');

        wrapper.setProps({
          isFetchFailed: false,
        });
        await nextTick();

        expect(wrapper.vm.listEmptyMessage).toBe('Sorry, no projects matched your search');
      });
    });
  });

  describe('template', () => {
    it('should render component element with list of projects', async () => {
      createComponent();

      await nextTick();
      expect(wrapper.classes('frequent-items-list-container')).toBe(true);
      expect(wrapper.findAllByTestId('frequent-items-list')).toHaveLength(1);
      expect(wrapper.findAllComponents(frequentItemsListItemComponent)).toHaveLength(5);
    });

    it('should render component element with empty message', async () => {
      createComponent({
        items: [],
      });

      await nextTick();
      expect(wrapper.vm.$el.querySelectorAll('li.section-empty')).toHaveLength(1);
      expect(wrapper.findAllComponents(frequentItemsListItemComponent)).toHaveLength(0);
    });
  });
});