2020-10-24 23:57:45 +05:30
|
|
|
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
|
2017-08-17 22:00:37 +05:30
|
|
|
import RecentSearchesService from '~/filtered_search/services/recent_searches_service';
|
2017-09-10 17:25:29 +05:30
|
|
|
import RecentSearchesServiceError from '~/filtered_search/services/recent_searches_service_error';
|
2017-08-17 22:00:37 +05:30
|
|
|
import AccessorUtilities from '~/lib/utils/accessor';
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
useLocalStorageSpy();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe('RecentSearchesService', () => {
|
|
|
|
let service;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
service = new RecentSearchesService();
|
2020-05-24 23:13:21 +05:30
|
|
|
localStorage.removeItem(service.localStorageKey);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetch', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(RecentSearchesService, 'isAvailable').mockReturnValue(true);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('should default to empty array', (done) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
const fetchItemsPromise = service.fetch();
|
|
|
|
|
|
|
|
fetchItemsPromise
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((items) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(items).toEqual([]);
|
|
|
|
})
|
2017-09-10 17:25:29 +05:30
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('should reject when unable to parse', (done) => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(localStorage, 'getItem').mockReturnValue('fail');
|
2017-08-17 22:00:37 +05:30
|
|
|
const fetchItemsPromise = service.fetch();
|
|
|
|
|
|
|
|
fetchItemsPromise
|
2017-09-10 17:25:29 +05:30
|
|
|
.then(done.fail)
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((error) => {
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(error).toEqual(expect.any(SyntaxError));
|
2017-09-10 17:25:29 +05:30
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('should reject when service is unavailable', (done) => {
|
2020-05-24 23:13:21 +05:30
|
|
|
RecentSearchesService.isAvailable.mockReturnValue(false);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
service
|
|
|
|
.fetch()
|
2017-09-10 17:25:29 +05:30
|
|
|
.then(done.fail)
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((error) => {
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(error).toEqual(expect.any(Error));
|
2017-09-10 17:25:29 +05:30
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('should return items from localStorage', (done) => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(localStorage, 'getItem').mockReturnValue('["foo", "bar"]');
|
2017-08-17 22:00:37 +05:30
|
|
|
const fetchItemsPromise = service.fetch();
|
|
|
|
|
|
|
|
fetchItemsPromise
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((items) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(items).toEqual(['foo', 'bar']);
|
2017-09-10 17:25:29 +05:30
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isAvailable returns `false`', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
RecentSearchesService.isAvailable.mockReturnValue(false);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
it('should not call .getItem', (done) => {
|
2018-12-13 13:39:08 +05:30
|
|
|
RecentSearchesService.prototype
|
|
|
|
.fetch()
|
2017-09-10 17:25:29 +05:30
|
|
|
.then(done.fail)
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((err) => {
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(err).toEqual(new RecentSearchesServiceError());
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(localStorage.getItem).not.toHaveBeenCalled();
|
2017-09-10 17:25:29 +05:30
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setRecentSearches', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(RecentSearchesService, 'isAvailable').mockReturnValue(true);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should save things in localStorage', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(localStorage, 'setItem');
|
2017-08-17 22:00:37 +05:30
|
|
|
const items = ['foo', 'bar'];
|
|
|
|
service.save(items);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(expect.any(String), JSON.stringify(items));
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('save', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(localStorage, 'setItem');
|
|
|
|
jest.spyOn(RecentSearchesService, 'isAvailable').mockImplementation(() => {});
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isAvailable returns `true`', () => {
|
|
|
|
const searchesString = 'searchesString';
|
|
|
|
const localStorageKey = 'localStorageKey';
|
|
|
|
const recentSearchesService = {
|
|
|
|
localStorageKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
RecentSearchesService.isAvailable.mockReturnValue(true);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
jest.spyOn(JSON, 'stringify').mockReturnValue(searchesString);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .setItem', () => {
|
2017-09-10 17:25:29 +05:30
|
|
|
RecentSearchesService.prototype.save.call(recentSearchesService);
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(localStorage.setItem).toHaveBeenCalledWith(localStorageKey, searchesString);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isAvailable returns `false`', () => {
|
|
|
|
beforeEach(() => {
|
2020-05-24 23:13:21 +05:30
|
|
|
RecentSearchesService.isAvailable.mockReturnValue(false);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call .setItem', () => {
|
2017-09-10 17:25:29 +05:30
|
|
|
RecentSearchesService.prototype.save();
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(localStorage.setItem).not.toHaveBeenCalled();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isAvailable', () => {
|
|
|
|
let isAvailable;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-11-11 11:23:49 +05:30
|
|
|
jest.spyOn(AccessorUtilities, 'canUseLocalStorage');
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
isAvailable = RecentSearchesService.isAvailable();
|
|
|
|
});
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
it('should call .canUseLocalStorage', () => {
|
|
|
|
expect(AccessorUtilities.canUseLocalStorage).toHaveBeenCalled();
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a boolean', () => {
|
|
|
|
expect(typeof isAvailable).toBe('boolean');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|