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';
|
|
|
|
|
|
|
|
describe('RecentSearchesService', () => {
|
|
|
|
let service;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
service = new RecentSearchesService();
|
|
|
|
window.localStorage.removeItem(service.localStorageKey);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetch', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(RecentSearchesService, 'isAvailable').and.returnValue(true);
|
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should default to empty array', done => {
|
2017-08-17 22:00:37 +05:30
|
|
|
const fetchItemsPromise = service.fetch();
|
|
|
|
|
|
|
|
fetchItemsPromise
|
2018-12-13 13:39:08 +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
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should reject when unable to parse', done => {
|
2017-08-17 22:00:37 +05:30
|
|
|
window.localStorage.setItem(service.localStorageKey, 'fail');
|
|
|
|
const fetchItemsPromise = service.fetch();
|
|
|
|
|
|
|
|
fetchItemsPromise
|
2017-09-10 17:25:29 +05:30
|
|
|
.then(done.fail)
|
2018-12-13 13:39:08 +05:30
|
|
|
.catch(error => {
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(error).toEqual(jasmine.any(SyntaxError));
|
2017-09-10 17:25:29 +05:30
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should reject when service is unavailable', done => {
|
2017-08-17 22:00:37 +05:30
|
|
|
RecentSearchesService.isAvailable.and.returnValue(false);
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
service
|
|
|
|
.fetch()
|
2017-09-10 17:25:29 +05:30
|
|
|
.then(done.fail)
|
2018-12-13 13:39:08 +05:30
|
|
|
.catch(error => {
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(error).toEqual(jasmine.any(Error));
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should return items from localStorage', done => {
|
2017-08-17 22:00:37 +05:30
|
|
|
window.localStorage.setItem(service.localStorageKey, '["foo", "bar"]');
|
|
|
|
const fetchItemsPromise = service.fetch();
|
|
|
|
|
|
|
|
fetchItemsPromise
|
2018-12-13 13:39:08 +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(() => {
|
|
|
|
RecentSearchesService.isAvailable.and.returnValue(false);
|
|
|
|
|
|
|
|
spyOn(window.localStorage, 'getItem');
|
|
|
|
});
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it('should not call .getItem', done => {
|
|
|
|
RecentSearchesService.prototype
|
|
|
|
.fetch()
|
2017-09-10 17:25:29 +05:30
|
|
|
.then(done.fail)
|
2018-12-13 13:39:08 +05:30
|
|
|
.catch(err => {
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(err).toEqual(new RecentSearchesServiceError());
|
|
|
|
expect(window.localStorage.getItem).not.toHaveBeenCalled();
|
|
|
|
})
|
|
|
|
.then(done)
|
|
|
|
.catch(done.fail);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setRecentSearches', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(RecentSearchesService, 'isAvailable').and.returnValue(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should save things in localStorage', () => {
|
|
|
|
const items = ['foo', 'bar'];
|
|
|
|
service.save(items);
|
|
|
|
const newLocalStorageValue = window.localStorage.getItem(service.localStorageKey);
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(JSON.parse(newLocalStorageValue)).toEqual(items);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('save', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(window.localStorage, 'setItem');
|
|
|
|
spyOn(RecentSearchesService, 'isAvailable');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isAvailable returns `true`', () => {
|
|
|
|
const searchesString = 'searchesString';
|
|
|
|
const localStorageKey = 'localStorageKey';
|
|
|
|
const recentSearchesService = {
|
|
|
|
localStorageKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
RecentSearchesService.isAvailable.and.returnValue(true);
|
|
|
|
|
|
|
|
spyOn(JSON, 'stringify').and.returnValue(searchesString);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .setItem', () => {
|
2017-09-10 17:25:29 +05:30
|
|
|
RecentSearchesService.prototype.save.call(recentSearchesService);
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(window.localStorage.setItem).toHaveBeenCalledWith(localStorageKey, searchesString);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if .isAvailable returns `false`', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
RecentSearchesService.isAvailable.and.returnValue(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not call .setItem', () => {
|
2017-09-10 17:25:29 +05:30
|
|
|
RecentSearchesService.prototype.save();
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(window.localStorage.setItem).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isAvailable', () => {
|
|
|
|
let isAvailable;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
spyOn(AccessorUtilities, 'isLocalStorageAccessSafe').and.callThrough();
|
|
|
|
|
|
|
|
isAvailable = RecentSearchesService.isAvailable();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call .isLocalStorageAccessSafe', () => {
|
|
|
|
expect(AccessorUtilities.isLocalStorageAccessSafe).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a boolean', () => {
|
|
|
|
expect(typeof isAvailable).toBe('boolean');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|