2021-06-30 02:07:17 -04:00
|
|
|
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
|
|
|
|
import { MAX_FREQUENCY } from '~/search/store/constants';
|
2021-07-11 23:09:27 -04:00
|
|
|
import { loadDataFromLS, setFrequentItemToLS, mergeById } from '~/search/store/utils';
|
|
|
|
import {
|
|
|
|
MOCK_LS_KEY,
|
|
|
|
MOCK_GROUPS,
|
|
|
|
MOCK_INFLATED_DATA,
|
|
|
|
FRESH_STORED_DATA,
|
|
|
|
STALE_STORED_DATA,
|
|
|
|
} from '../mock_data';
|
2021-06-30 02:07:17 -04:00
|
|
|
|
2021-07-14 17:09:44 -04:00
|
|
|
const PREV_TIME = new Date().getTime() - 1;
|
|
|
|
const CURRENT_TIME = new Date().getTime();
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
useLocalStorageSpy();
|
|
|
|
jest.mock('~/lib/utils/accessor', () => ({
|
|
|
|
isLocalStorageAccessSafe: jest.fn().mockReturnValue(true),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('Global Search Store Utils', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
localStorage.clear();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('loadDataFromLS', () => {
|
|
|
|
let res;
|
|
|
|
|
|
|
|
describe('with valid data', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
localStorage.setItem(MOCK_LS_KEY, JSON.stringify(MOCK_GROUPS));
|
|
|
|
res = loadDataFromLS(MOCK_LS_KEY);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns parsed array', () => {
|
|
|
|
expect(res).toStrictEqual(MOCK_GROUPS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with invalid data', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
localStorage.setItem(MOCK_LS_KEY, '[}');
|
|
|
|
res = loadDataFromLS(MOCK_LS_KEY);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('wipes local storage and returns an empty array', () => {
|
|
|
|
expect(localStorage.removeItem).toHaveBeenCalledWith(MOCK_LS_KEY);
|
|
|
|
expect(res).toStrictEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setFrequentItemToLS', () => {
|
|
|
|
const frequentItems = {};
|
2021-08-05 02:09:25 -04:00
|
|
|
let res;
|
2021-06-30 02:07:17 -04:00
|
|
|
|
|
|
|
describe('with existing data', () => {
|
|
|
|
describe(`when frequency is less than ${MAX_FREQUENCY}`, () => {
|
|
|
|
beforeEach(() => {
|
2021-07-14 17:09:44 -04:00
|
|
|
frequentItems[MOCK_LS_KEY] = [{ ...MOCK_GROUPS[0], frequency: 1, lastUsed: PREV_TIME }];
|
2021-08-05 02:09:25 -04:00
|
|
|
res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it('adds 1 to the frequency, tracks lastUsed, calls localStorage.setItem and returns the array', () => {
|
|
|
|
const updatedFrequentItems = [
|
|
|
|
{ ...MOCK_GROUPS[0], frequency: 2, lastUsed: CURRENT_TIME },
|
|
|
|
];
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(
|
|
|
|
MOCK_LS_KEY,
|
2021-08-05 02:09:25 -04:00
|
|
|
JSON.stringify(updatedFrequentItems),
|
2021-06-30 02:07:17 -04:00
|
|
|
);
|
2021-08-05 02:09:25 -04:00
|
|
|
expect(res).toEqual(updatedFrequentItems);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(`when frequency is equal to ${MAX_FREQUENCY}`, () => {
|
|
|
|
beforeEach(() => {
|
2021-07-14 17:09:44 -04:00
|
|
|
frequentItems[MOCK_LS_KEY] = [
|
|
|
|
{ ...MOCK_GROUPS[0], frequency: MAX_FREQUENCY, lastUsed: PREV_TIME },
|
|
|
|
];
|
2021-08-05 02:09:25 -04:00
|
|
|
res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it(`does not further increase frequency past ${MAX_FREQUENCY}, tracks lastUsed, calls localStorage.setItem, and returns the array`, () => {
|
|
|
|
const updatedFrequentItems = [
|
|
|
|
{ ...MOCK_GROUPS[0], frequency: MAX_FREQUENCY, lastUsed: CURRENT_TIME },
|
|
|
|
];
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(
|
|
|
|
MOCK_LS_KEY,
|
2021-08-05 02:09:25 -04:00
|
|
|
JSON.stringify(updatedFrequentItems),
|
2021-06-30 02:07:17 -04:00
|
|
|
);
|
2021-08-05 02:09:25 -04:00
|
|
|
expect(res).toEqual(updatedFrequentItems);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with no existing data', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
frequentItems[MOCK_LS_KEY] = [];
|
2021-08-05 02:09:25 -04:00
|
|
|
res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it('adds a new entry with frequency 1, tracks lastUsed, calls localStorage.setItem, and returns the array', () => {
|
|
|
|
const updatedFrequentItems = [{ ...MOCK_GROUPS[0], frequency: 1, lastUsed: CURRENT_TIME }];
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(
|
|
|
|
MOCK_LS_KEY,
|
2021-08-05 02:09:25 -04:00
|
|
|
JSON.stringify(updatedFrequentItems),
|
2021-06-30 02:07:17 -04:00
|
|
|
);
|
2021-08-05 02:09:25 -04:00
|
|
|
expect(res).toEqual(updatedFrequentItems);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with multiple entries', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
frequentItems[MOCK_LS_KEY] = [
|
2021-07-14 17:09:44 -04:00
|
|
|
{ id: 1, frequency: 2, lastUsed: PREV_TIME },
|
|
|
|
{ id: 2, frequency: 1, lastUsed: PREV_TIME },
|
|
|
|
{ id: 3, frequency: 1, lastUsed: PREV_TIME },
|
2021-06-30 02:07:17 -04:00
|
|
|
];
|
2021-08-05 02:09:25 -04:00
|
|
|
res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, { id: 3 });
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it('sorts the array by most frequent and lastUsed and returns the array', () => {
|
|
|
|
const updatedFrequentItems = [
|
|
|
|
{ id: 3, frequency: 2, lastUsed: CURRENT_TIME },
|
|
|
|
{ id: 1, frequency: 2, lastUsed: PREV_TIME },
|
|
|
|
{ id: 2, frequency: 1, lastUsed: PREV_TIME },
|
|
|
|
];
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(
|
|
|
|
MOCK_LS_KEY,
|
2021-08-05 02:09:25 -04:00
|
|
|
JSON.stringify(updatedFrequentItems),
|
2021-06-30 02:07:17 -04:00
|
|
|
);
|
2021-08-05 02:09:25 -04:00
|
|
|
expect(res).toEqual(updatedFrequentItems);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with max entries', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
frequentItems[MOCK_LS_KEY] = [
|
2021-07-14 17:09:44 -04:00
|
|
|
{ id: 1, frequency: 5, lastUsed: PREV_TIME },
|
|
|
|
{ id: 2, frequency: 4, lastUsed: PREV_TIME },
|
|
|
|
{ id: 3, frequency: 3, lastUsed: PREV_TIME },
|
|
|
|
{ id: 4, frequency: 2, lastUsed: PREV_TIME },
|
|
|
|
{ id: 5, frequency: 1, lastUsed: PREV_TIME },
|
2021-06-30 02:07:17 -04:00
|
|
|
];
|
2021-08-05 02:09:25 -04:00
|
|
|
res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, { id: 6 });
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it('removes the last item in the array and returns the array', () => {
|
|
|
|
const updatedFrequentItems = [
|
|
|
|
{ id: 1, frequency: 5, lastUsed: PREV_TIME },
|
|
|
|
{ id: 2, frequency: 4, lastUsed: PREV_TIME },
|
|
|
|
{ id: 3, frequency: 3, lastUsed: PREV_TIME },
|
|
|
|
{ id: 4, frequency: 2, lastUsed: PREV_TIME },
|
|
|
|
{ id: 6, frequency: 1, lastUsed: CURRENT_TIME },
|
|
|
|
];
|
|
|
|
|
2021-06-30 02:07:17 -04:00
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(
|
|
|
|
MOCK_LS_KEY,
|
2021-08-05 02:09:25 -04:00
|
|
|
JSON.stringify(updatedFrequentItems),
|
2021-06-30 02:07:17 -04:00
|
|
|
);
|
2021-08-05 02:09:25 -04:00
|
|
|
expect(res).toEqual(updatedFrequentItems);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with null data loaded in', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
frequentItems[MOCK_LS_KEY] = null;
|
2021-08-05 02:09:25 -04:00
|
|
|
res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_GROUPS[0]);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it('wipes local storage and returns empty array', () => {
|
2021-06-30 02:07:17 -04:00
|
|
|
expect(localStorage.removeItem).toHaveBeenCalledWith(MOCK_LS_KEY);
|
2021-08-05 02:09:25 -04:00
|
|
|
expect(res).toEqual([]);
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
});
|
2021-07-11 23:09:27 -04:00
|
|
|
|
|
|
|
describe('with additional data', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const MOCK_ADDITIONAL_DATA_GROUP = { ...MOCK_GROUPS[0], extraData: 'test' };
|
|
|
|
frequentItems[MOCK_LS_KEY] = [];
|
2021-08-05 02:09:25 -04:00
|
|
|
res = setFrequentItemToLS(MOCK_LS_KEY, frequentItems, MOCK_ADDITIONAL_DATA_GROUP);
|
2021-07-11 23:09:27 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 02:09:25 -04:00
|
|
|
it('parses out extra data for LS and returns the array', () => {
|
|
|
|
const updatedFrequentItems = [{ ...MOCK_GROUPS[0], frequency: 1, lastUsed: CURRENT_TIME }];
|
|
|
|
|
2021-07-11 23:09:27 -04:00
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(
|
|
|
|
MOCK_LS_KEY,
|
2021-08-05 02:09:25 -04:00
|
|
|
JSON.stringify(updatedFrequentItems),
|
2021-07-11 23:09:27 -04:00
|
|
|
);
|
2021-08-05 02:09:25 -04:00
|
|
|
expect(res).toEqual(updatedFrequentItems);
|
2021-07-11 23:09:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
|
|
|
description | inflatedData | storedData | response
|
|
|
|
${'identical'} | ${MOCK_INFLATED_DATA} | ${FRESH_STORED_DATA} | ${FRESH_STORED_DATA}
|
|
|
|
${'stale'} | ${MOCK_INFLATED_DATA} | ${STALE_STORED_DATA} | ${FRESH_STORED_DATA}
|
|
|
|
${'empty'} | ${MOCK_INFLATED_DATA} | ${[]} | ${MOCK_INFLATED_DATA}
|
|
|
|
${'null'} | ${MOCK_INFLATED_DATA} | ${null} | ${MOCK_INFLATED_DATA}
|
|
|
|
`('mergeById', ({ description, inflatedData, storedData, response }) => {
|
|
|
|
describe(`with ${description} storedData`, () => {
|
|
|
|
let res;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
res = mergeById(inflatedData, storedData);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prioritizes inflatedData and preserves frequency count', () => {
|
|
|
|
expect(response).toStrictEqual(res);
|
|
|
|
});
|
|
|
|
});
|
2021-06-30 02:07:17 -04:00
|
|
|
});
|
|
|
|
});
|