summaryrefslogtreecommitdiff
path: root/spec/frontend/super_sidebar/components/frequent_items_list_spec.js
blob: 63dd941974a04f1f4b18805844912ff109cd0b19 (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
import { shallowMountExtended, mountExtended } from 'helpers/vue_test_utils_helper';
import { s__ } from '~/locale';
import FrequentItemsList from '~/super_sidebar/components//frequent_items_list.vue';
import ItemsList from '~/super_sidebar/components/items_list.vue';
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import { cachedFrequentProjects } from '../mock_data';

const title = s__('Navigation|FREQUENT PROJECTS');
const pristineText = s__('Navigation|Projects you visit often will appear here.');
const storageKey = 'storageKey';
const maxItems = 5;

describe('FrequentItemsList component', () => {
  useLocalStorageSpy();

  let wrapper;

  const findListTitle = () => wrapper.findByTestId('list-title');
  const findItemsList = () => wrapper.findComponent(ItemsList);
  const findEmptyText = () => wrapper.findByTestId('empty-text');
  const findRemoveItemButton = () => wrapper.findByTestId('item-remove');

  const createWrapperFactory = (mountFn = shallowMountExtended) => () => {
    wrapper = mountFn(FrequentItemsList, {
      propsData: {
        title,
        pristineText,
        storageKey,
        maxItems,
      },
    });
  };
  const createWrapper = createWrapperFactory();
  const createFullWrapper = createWrapperFactory(mountExtended);

  describe('default', () => {
    beforeEach(() => {
      createWrapper();
    });

    it("renders the list's title", () => {
      expect(findListTitle().text()).toBe(title);
    });

    it('renders the empty text', () => {
      expect(findEmptyText().exists()).toBe(true);
      expect(findEmptyText().text()).toBe(pristineText);
    });
  });

  describe('when there are cached frequent items', () => {
    beforeEach(() => {
      window.localStorage.setItem(storageKey, cachedFrequentProjects);
      createWrapper();
    });

    it('attempts to retrieve the items from the local storage', () => {
      expect(window.localStorage.getItem).toHaveBeenCalledTimes(1);
      expect(window.localStorage.getItem).toHaveBeenCalledWith(storageKey);
    });

    it('renders the maximum amount of items', () => {
      expect(findItemsList().props('items').length).toBe(maxItems);
    });

    it('does not render the empty text slot', () => {
      expect(findEmptyText().exists()).toBe(false);
    });
  });

  describe('items editing', () => {
    beforeEach(() => {
      window.localStorage.setItem(storageKey, cachedFrequentProjects);
      createFullWrapper();
    });

    it('remove-item event emission from items-list causes list item to be removed', async () => {
      const localStorageProjects = findItemsList().props('items');
      await findRemoveItemButton().trigger('click');

      expect(findItemsList().props('items')).toHaveLength(maxItems - 1);
      expect(findItemsList().props('items')).not.toContain(localStorageProjects[0]);
    });
  });
});