debian-mirror-gitlab/spec/frontend/frequent_items/utils_spec.js

131 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
2021-03-11 19:13:27 +05:30
import { HOUR_IN_MS, FREQUENT_ITEMS } from '~/frequent_items/constants';
2020-02-01 01:16:34 +05:30
import {
isMobile,
getTopFrequentItems,
updateExistingFrequentItem,
sanitizeItem,
} from '~/frequent_items/utils';
2018-11-08 19:23:39 +05:30
import { mockProject, unsortedFrequentItems, sortedFrequentItems } from './mock_data';
describe('Frequent Items utils spec', () => {
describe('isMobile', () => {
2020-03-13 15:44:24 +05:30
it('returns true when the screen is medium ', () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('md');
2020-03-13 15:44:24 +05:30
expect(isMobile()).toBe(true);
});
2018-11-08 19:23:39 +05:30
it('returns true when the screen is small ', () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('sm');
2018-11-08 19:23:39 +05:30
expect(isMobile()).toBe(true);
});
it('returns true when the screen is extra-small ', () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('xs');
2018-11-08 19:23:39 +05:30
expect(isMobile()).toBe(true);
});
2020-03-13 15:44:24 +05:30
it('returns false when the screen is larger than medium ', () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('lg');
2018-11-08 19:23:39 +05:30
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', () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('md');
2018-11-08 19:23:39 +05:30
const result = getTopFrequentItems(unsortedFrequentItems);
expect(result.length).toBe(FREQUENT_ITEMS.LIST_COUNT_MOBILE);
});
it('returns correct amount of items for desktop', () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('xl');
2018-11-08 19:23:39 +05:30
const result = getTopFrequentItems(unsortedFrequentItems);
expect(result.length).toBe(FREQUENT_ITEMS.LIST_COUNT_DESKTOP);
});
it('sorts frequent items in order of frequency and lastAccessedOn', () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(bp, 'getBreakpointSize').mockReturnValue('xl');
2018-11-08 19:23:39 +05:30
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);
});
});
2020-02-01 01:16:34 +05:30
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 });
});
2020-03-28 13:19:24 +05:30
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 });
});
2020-02-01 01:16:34 +05:30
});
2018-11-08 19:23:39 +05:30
});