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

utils_spec.js « dashboard « issues « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 08d00eee3e3f9436f4443be47364f4f6fa4acd9e (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
import AxiosMockAdapter from 'axios-mock-adapter';
import fuzzaldrinPlus from 'fuzzaldrin-plus';
import { AutocompleteCache } from '~/issues/dashboard/utils';
import { MAX_LIST_SIZE } from '~/issues/list/constants';
import axios from '~/lib/utils/axios_utils';

describe('AutocompleteCache', () => {
  let autocompleteCache;
  let axiosMock;
  const cacheName = 'name';
  const searchProperty = 'property';
  const url = 'url';

  const data = [
    { [searchProperty]: 'one' },
    { [searchProperty]: 'two' },
    { [searchProperty]: 'three' },
    { [searchProperty]: 'four' },
    { [searchProperty]: 'five' },
    { [searchProperty]: 'six' },
    { [searchProperty]: 'seven' },
    { [searchProperty]: 'eight' },
    { [searchProperty]: 'nine' },
    { [searchProperty]: 'ten' },
    { [searchProperty]: 'eleven' },
    { [searchProperty]: 'twelve' },
    { [searchProperty]: 'thirteen' },
    { [searchProperty]: 'fourteen' },
    { [searchProperty]: 'fifteen' },
  ];

  beforeEach(() => {
    autocompleteCache = new AutocompleteCache();
    axiosMock = new AxiosMockAdapter(axios);
  });

  afterEach(() => {
    axiosMock.reset();
  });

  describe('when there is no cached data', () => {
    let response;

    beforeEach(async () => {
      axiosMock.onGet(url).replyOnce(200, data);
      response = await autocompleteCache.fetch({ url, cacheName, searchProperty });
    });

    it('fetches items via the API', () => {
      expect(axiosMock.history.get[0].url).toBe(url);
    });

    it('returns a maximum of 10 items', () => {
      expect(response).toHaveLength(MAX_LIST_SIZE);
    });
  });

  describe('when there is cached data', () => {
    let response;

    beforeEach(async () => {
      axiosMock.onGet(url).replyOnce(200, data);
      jest.spyOn(fuzzaldrinPlus, 'filter');
      // Populate cache
      await autocompleteCache.fetch({ url, cacheName, searchProperty });
      // Execute filtering on cache data
      response = await autocompleteCache.fetch({ url, cacheName, searchProperty, search: 'een' });
    });

    it('returns filtered items based on search characters', () => {
      expect(response).toEqual([
        { [searchProperty]: 'fifteen' },
        { [searchProperty]: 'thirteen' },
        { [searchProperty]: 'fourteen' },
        { [searchProperty]: 'eleven' },
        { [searchProperty]: 'seven' },
      ]);
    });

    it('filters using fuzzaldrinPlus', () => {
      expect(fuzzaldrinPlus.filter).toHaveBeenCalled();
    });

    it('does not call the API', () => {
      expect(axiosMock.history.get[1]).toBeUndefined();
    });
  });
});