summaryrefslogtreecommitdiff
path: root/spec/javascripts/filtered_search/stores/recent_searches_store_spec.js
blob: 1eebc6f236777fe06c23dcafdc91d10c5bfeb1a1 (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
import RecentSearchesStore from '~/filtered_search/stores/recent_searches_store';

describe('RecentSearchesStore', () => {
  let store;

  beforeEach(() => {
    store = new RecentSearchesStore();
  });

  describe('addRecentSearch', () => {
    it('should add to the front of the list', () => {
      store.addRecentSearch('foo');
      store.addRecentSearch('bar');

      expect(store.state.recentSearches).toEqual(['bar', 'foo']);
    });

    it('should deduplicate', () => {
      store.addRecentSearch('foo');
      store.addRecentSearch('bar');
      store.addRecentSearch('foo');

      expect(store.state.recentSearches).toEqual(['foo', 'bar']);
    });

    it('only keeps track of 5 items', () => {
      store.addRecentSearch('1');
      store.addRecentSearch('2');
      store.addRecentSearch('3');
      store.addRecentSearch('4');
      store.addRecentSearch('5');
      store.addRecentSearch('6');
      store.addRecentSearch('7');

      expect(store.state.recentSearches).toEqual(['7', '6', '5', '4', '3']);
    });
  });

  describe('setRecentSearches', () => {
    it('should override list', () => {
      store.setRecentSearches([
        'foo',
        'bar',
      ]);
      store.setRecentSearches([
        'baz',
        'qux',
      ]);

      expect(store.state.recentSearches).toEqual(['baz', 'qux']);
    });

    it('only keeps track of 5 items', () => {
      store.setRecentSearches(['1', '2', '3', '4', '5', '6', '7']);

      expect(store.state.recentSearches).toEqual(['1', '2', '3', '4', '5']);
    });
  });
});