summaryrefslogtreecommitdiff
path: root/spec/javascripts/frequent_items/utils_spec.js
blob: 2939b46bc31e7a7f6a6d2cfe1c1508d35f6e2ec2 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
import {
  isMobile,
  getTopFrequentItems,
  updateExistingFrequentItem,
  sanitizeItem,
} from '~/frequent_items/utils';
import { HOUR_IN_MS, FREQUENT_ITEMS } from '~/frequent_items/constants';
import { mockProject, unsortedFrequentItems, sortedFrequentItems } from './mock_data';

describe('Frequent Items utils spec', () => {
  describe('isMobile', () => {
    it('returns true when the screen is medium ', () => {
      spyOn(bp, 'getBreakpointSize').and.returnValue('md');

      expect(isMobile()).toBe(true);
    });

    it('returns true when the screen is small ', () => {
      spyOn(bp, 'getBreakpointSize').and.returnValue('sm');

      expect(isMobile()).toBe(true);
    });

    it('returns true when the screen is extra-small ', () => {
      spyOn(bp, 'getBreakpointSize').and.returnValue('xs');

      expect(isMobile()).toBe(true);
    });

    it('returns false when the screen is larger than medium ', () => {
      spyOn(bp, 'getBreakpointSize').and.returnValue('lg');

      expect(isMobile()).toBe(false);
    });
  });

  describe('getTopFrequentItems', () => {
    it('returns empty array if no items provided', () => {
      const result = getTopFrequentItems();

      expect(result.length).toBe(0);
    });

    it('returns correct amount of items for mobile', () => {
      spyOn(bp, 'getBreakpointSize').and.returnValue('md');
      const result = getTopFrequentItems(unsortedFrequentItems);

      expect(result.length).toBe(FREQUENT_ITEMS.LIST_COUNT_MOBILE);
    });

    it('returns correct amount of items for desktop', () => {
      spyOn(bp, 'getBreakpointSize').and.returnValue('xl');
      const result = getTopFrequentItems(unsortedFrequentItems);

      expect(result.length).toBe(FREQUENT_ITEMS.LIST_COUNT_DESKTOP);
    });

    it('sorts frequent items in order of frequency and lastAccessedOn', () => {
      spyOn(bp, 'getBreakpointSize').and.returnValue('xl');
      const result = getTopFrequentItems(unsortedFrequentItems);
      const expectedResult = sortedFrequentItems.slice(0, FREQUENT_ITEMS.LIST_COUNT_DESKTOP);

      expect(result).toEqual(expectedResult);
    });
  });

  describe('updateExistingFrequentItem', () => {
    let mockedProject;

    beforeEach(() => {
      mockedProject = {
        ...mockProject,
        frequency: 1,
        lastAccessedOn: 1497979281815,
      };
    });

    it('updates item if accessed over an hour ago', () => {
      const newTimestamp = Date.now() + HOUR_IN_MS + 1;
      const newItem = {
        ...mockedProject,
        lastAccessedOn: newTimestamp,
      };
      const result = updateExistingFrequentItem(mockedProject, newItem);

      expect(result.frequency).toBe(mockedProject.frequency + 1);
    });

    it('does not update item if accessed within the hour', () => {
      const newItem = {
        ...mockedProject,
        lastAccessedOn: mockedProject.lastAccessedOn + HOUR_IN_MS,
      };
      const result = updateExistingFrequentItem(mockedProject, newItem);

      expect(result.frequency).toBe(mockedProject.frequency);
    });
  });

  describe('sanitizeItem', () => {
    it('strips HTML tags for name and namespace', () => {
      const input = {
        name: '<br><b>test</b>',
        namespace: '<br>test',
        id: 1,
      };

      expect(sanitizeItem(input)).toEqual({ name: 'test', namespace: 'test', id: 1 });
    });

    it("skips `name` key if it doesn't exist on the item", () => {
      const input = {
        namespace: '<br>test',
        id: 1,
      };

      expect(sanitizeItem(input)).toEqual({ namespace: 'test', id: 1 });
    });

    it("skips `namespace` key if it doesn't exist on the item", () => {
      const input = {
        name: '<br><b>test</b>',
        id: 1,
      };

      expect(sanitizeItem(input)).toEqual({ name: 'test', id: 1 });
    });
  });
});