Commit 999f71f3 authored by winh's avatar winh

Handle Promise rejections in RecentSearchesService spec (!11206)

parent 1daa133c
/* eslint-disable promise/catch-or-return */
import RecentSearchesService from '~/filtered_search/services/recent_searches_service'; import RecentSearchesService from '~/filtered_search/services/recent_searches_service';
import RecentSearchesServiceError from '~/filtered_search/services/recent_searches_service_error';
import AccessorUtilities from '~/lib/utils/accessor'; import AccessorUtilities from '~/lib/utils/accessor';
describe('RecentSearchesService', () => { describe('RecentSearchesService', () => {
...@@ -22,11 +21,9 @@ describe('RecentSearchesService', () => { ...@@ -22,11 +21,9 @@ describe('RecentSearchesService', () => {
fetchItemsPromise fetchItemsPromise
.then((items) => { .then((items) => {
expect(items).toEqual([]); expect(items).toEqual([]);
done();
}) })
.catch((err) => { .then(done)
done.fail('Shouldn\'t reject with empty localStorage key', err); .catch(done.fail);
});
}); });
it('should reject when unable to parse', (done) => { it('should reject when unable to parse', (done) => {
...@@ -34,19 +31,24 @@ describe('RecentSearchesService', () => { ...@@ -34,19 +31,24 @@ describe('RecentSearchesService', () => {
const fetchItemsPromise = service.fetch(); const fetchItemsPromise = service.fetch();
fetchItemsPromise fetchItemsPromise
.then(done.fail)
.catch((error) => { .catch((error) => {
expect(error).toEqual(jasmine.any(SyntaxError)); expect(error).toEqual(jasmine.any(SyntaxError));
done(); })
}); .then(done)
.catch(done.fail);
}); });
it('should reject when service is unavailable', (done) => { it('should reject when service is unavailable', (done) => {
RecentSearchesService.isAvailable.and.returnValue(false); RecentSearchesService.isAvailable.and.returnValue(false);
service.fetch().catch((error) => { service.fetch()
.then(done.fail)
.catch((error) => {
expect(error).toEqual(jasmine.any(Error)); expect(error).toEqual(jasmine.any(Error));
done(); })
}); .then(done)
.catch(done.fail);
}); });
it('should return items from localStorage', (done) => { it('should return items from localStorage', (done) => {
...@@ -56,8 +58,9 @@ describe('RecentSearchesService', () => { ...@@ -56,8 +58,9 @@ describe('RecentSearchesService', () => {
fetchItemsPromise fetchItemsPromise
.then((items) => { .then((items) => {
expect(items).toEqual(['foo', 'bar']); expect(items).toEqual(['foo', 'bar']);
done(); })
}); .then(done)
.catch(done.fail);
}); });
describe('if .isAvailable returns `false`', () => { describe('if .isAvailable returns `false`', () => {
...@@ -65,12 +68,17 @@ describe('RecentSearchesService', () => { ...@@ -65,12 +68,17 @@ describe('RecentSearchesService', () => {
RecentSearchesService.isAvailable.and.returnValue(false); RecentSearchesService.isAvailable.and.returnValue(false);
spyOn(window.localStorage, 'getItem'); spyOn(window.localStorage, 'getItem');
RecentSearchesService.prototype.fetch();
}); });
it('should not call .getItem', () => { it('should not call .getItem', (done) => {
RecentSearchesService.prototype.fetch()
.then(done.fail)
.catch((err) => {
expect(err).toEqual(new RecentSearchesServiceError());
expect(window.localStorage.getItem).not.toHaveBeenCalled(); expect(window.localStorage.getItem).not.toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
}); });
}); });
}); });
...@@ -105,11 +113,11 @@ describe('RecentSearchesService', () => { ...@@ -105,11 +113,11 @@ describe('RecentSearchesService', () => {
RecentSearchesService.isAvailable.and.returnValue(true); RecentSearchesService.isAvailable.and.returnValue(true);
spyOn(JSON, 'stringify').and.returnValue(searchesString); spyOn(JSON, 'stringify').and.returnValue(searchesString);
RecentSearchesService.prototype.save.call(recentSearchesService);
}); });
it('should call .setItem', () => { it('should call .setItem', () => {
RecentSearchesService.prototype.save.call(recentSearchesService);
expect(window.localStorage.setItem).toHaveBeenCalledWith(localStorageKey, searchesString); expect(window.localStorage.setItem).toHaveBeenCalledWith(localStorageKey, searchesString);
}); });
}); });
...@@ -117,11 +125,11 @@ describe('RecentSearchesService', () => { ...@@ -117,11 +125,11 @@ describe('RecentSearchesService', () => {
describe('if .isAvailable returns `false`', () => { describe('if .isAvailable returns `false`', () => {
beforeEach(() => { beforeEach(() => {
RecentSearchesService.isAvailable.and.returnValue(false); RecentSearchesService.isAvailable.and.returnValue(false);
RecentSearchesService.prototype.save();
}); });
it('should not call .setItem', () => { it('should not call .setItem', () => {
RecentSearchesService.prototype.save();
expect(window.localStorage.setItem).not.toHaveBeenCalled(); expect(window.localStorage.setItem).not.toHaveBeenCalled();
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment