summaryrefslogtreecommitdiff
path: root/spec/javascripts/frequent_items/components/frequent_items_list_spec.js
blob: 3003b7ee0008bb329fde617b84f60c2c4cd0d275 (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
import Vue from 'vue';
import frequentItemsListComponent from '~/frequent_items/components/frequent_items_list.vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper';
import { mockFrequentProjects } from '../mock_data';

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

  return mountComponent(Component, {
    namespace,
    items: mockFrequentProjects,
    isFetchFailed: false,
    hasSearchQuery: false,
    matcher: 'lab',
  });
};

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

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

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

  describe('computed', () => {
    describe('isListEmpty', () => {
      it('should return `true` or `false` representing whether if `items` is empty or not with projects', () => {
        vm.items = [];
        expect(vm.isListEmpty).toBe(true);

        vm.items = mockFrequentProjects;
        expect(vm.isListEmpty).toBe(false);
      });
    });

    describe('fetched item messages', () => {
      it('should return appropriate empty list message based on value of `localStorageFailed` prop with projects', () => {
        vm.isFetchFailed = true;
        expect(vm.listEmptyMessage).toBe('This feature requires browser localStorage support');

        vm.isFetchFailed = false;
        expect(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', () => {
        vm.hasSearchQuery = true;
        vm.isFetchFailed = true;
        expect(vm.listEmptyMessage).toBe('Something went wrong on our end.');

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

  describe('template', () => {
    it('should render component element with list of projects', done => {
      vm.items = mockFrequentProjects;

      Vue.nextTick(() => {
        expect(vm.$el.classList.contains('frequent-items-list-container')).toBe(true);
        expect(vm.$el.querySelectorAll('ul.list-unstyled').length).toBe(1);
        expect(vm.$el.querySelectorAll('li.frequent-items-list-item-container').length).toBe(5);
        done();
      });
    });

    it('should render component element with empty message', done => {
      vm.items = [];

      Vue.nextTick(() => {
        expect(vm.$el.querySelectorAll('li.section-empty').length).toBe(1);
        expect(vm.$el.querySelectorAll('li.frequent-items-list-item-container').length).toBe(0);
        done();
      });
    });
  });
});