Commit bff40654 authored by Paul Slaughter's avatar Paul Slaughter

Move some utils karma specs to jest

**How?**

Used [migrate-karma-to-jest][1] utility with some manual
intervention

[1]: https://gitlab.com/gitlab-org/frontend/playground/migrate-karma-to-jest
parent 14a16e2e
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import AccessorUtilities from '~/lib/utils/accessor'; import AccessorUtilities from '~/lib/utils/accessor';
describe('AccessorUtilities', () => { describe('AccessorUtilities', () => {
useLocalStorageSpy();
const testError = new Error('test error'); const testError = new Error('test error');
describe('isPropertyAccessSafe', () => { describe('isPropertyAccessSafe', () => {
let base; let base;
it('should return `true` if access is safe', () => { it('should return `true` if access is safe', () => {
base = { testProp: 'testProp' }; base = {
testProp: 'testProp',
};
expect(AccessorUtilities.isPropertyAccessSafe(base, 'testProp')).toBe(true); expect(AccessorUtilities.isPropertyAccessSafe(base, 'testProp')).toBe(true);
}); });
...@@ -54,17 +58,12 @@ describe('AccessorUtilities', () => { ...@@ -54,17 +58,12 @@ describe('AccessorUtilities', () => {
}); });
describe('isLocalStorageAccessSafe', () => { describe('isLocalStorageAccessSafe', () => {
beforeEach(() => {
spyOn(window.localStorage, 'setItem');
spyOn(window.localStorage, 'removeItem');
});
it('should return `true` if access is safe', () => { it('should return `true` if access is safe', () => {
expect(AccessorUtilities.isLocalStorageAccessSafe()).toBe(true); expect(AccessorUtilities.isLocalStorageAccessSafe()).toBe(true);
}); });
it('should return `false` if access to .setItem isnt safe', () => { it('should return `false` if access to .setItem isnt safe', () => {
window.localStorage.setItem.and.callFake(() => { window.localStorage.setItem.mockImplementation(() => {
throw testError; throw testError;
}); });
......
...@@ -25,7 +25,7 @@ describe('DOM Utils', () => { ...@@ -25,7 +25,7 @@ describe('DOM Utils', () => {
addClassIfElementExists(childElement, className); addClassIfElementExists(childElement, className);
expect(childElement.classList).toContain(className); expect(childElement.classList).toContainEqual(className);
}); });
it('does not throw if element does not exist', () => { it('does not throw if element does not exist', () => {
...@@ -40,22 +40,44 @@ describe('DOM Utils', () => { ...@@ -40,22 +40,44 @@ describe('DOM Utils', () => {
describe('canScrollUp', () => { describe('canScrollUp', () => {
[1, 100].forEach(scrollTop => { [1, 100].forEach(scrollTop => {
it(`is true if scrollTop is > 0 (${scrollTop})`, () => { it(`is true if scrollTop is > 0 (${scrollTop})`, () => {
expect(canScrollUp({ scrollTop })).toBe(true); expect(
canScrollUp({
scrollTop,
}),
).toBe(true);
}); });
}); });
[0, -10].forEach(scrollTop => { [0, -10].forEach(scrollTop => {
it(`is false if scrollTop is <= 0 (${scrollTop})`, () => { it(`is false if scrollTop is <= 0 (${scrollTop})`, () => {
expect(canScrollUp({ scrollTop })).toBe(false); expect(
canScrollUp({
scrollTop,
}),
).toBe(false);
}); });
}); });
it('is true if scrollTop is > margin', () => { it('is true if scrollTop is > margin', () => {
expect(canScrollUp({ scrollTop: TEST_MARGIN + 1 }, TEST_MARGIN)).toBe(true); expect(
canScrollUp(
{
scrollTop: TEST_MARGIN + 1,
},
TEST_MARGIN,
),
).toBe(true);
}); });
it('is false if scrollTop is <= margin', () => { it('is false if scrollTop is <= margin', () => {
expect(canScrollUp({ scrollTop: TEST_MARGIN }, TEST_MARGIN)).toBe(false); expect(
canScrollUp(
{
scrollTop: TEST_MARGIN,
},
TEST_MARGIN,
),
).toBe(false);
}); });
}); });
...@@ -63,7 +85,11 @@ describe('DOM Utils', () => { ...@@ -63,7 +85,11 @@ describe('DOM Utils', () => {
let element; let element;
beforeEach(() => { beforeEach(() => {
element = { scrollTop: 7, offsetHeight: 22, scrollHeight: 30 }; element = {
scrollTop: 7,
offsetHeight: 22,
scrollHeight: 30,
};
}); });
it('is true if element can be scrolled down', () => { it('is true if element can be scrolled down', () => {
......
...@@ -20,7 +20,7 @@ describe('File upload', () => { ...@@ -20,7 +20,7 @@ describe('File upload', () => {
const btn = document.querySelector('.js-button'); const btn = document.querySelector('.js-button');
const input = document.querySelector('.js-input'); const input = document.querySelector('.js-input');
spyOn(input, 'click'); jest.spyOn(input, 'click').mockReturnValue();
btn.click(); btn.click();
...@@ -43,7 +43,7 @@ describe('File upload', () => { ...@@ -43,7 +43,7 @@ describe('File upload', () => {
const btn = document.querySelector('.js-button'); const btn = document.querySelector('.js-button');
fileUpload('.js-not-button', '.js-input'); fileUpload('.js-not-button', '.js-input');
spyOn(input, 'click'); jest.spyOn(input, 'click').mockReturnValue();
btn.click(); btn.click();
...@@ -55,7 +55,7 @@ describe('File upload', () => { ...@@ -55,7 +55,7 @@ describe('File upload', () => {
const btn = document.querySelector('.js-button'); const btn = document.querySelector('.js-button');
fileUpload('.js-button', '.js-not-input'); fileUpload('.js-button', '.js-not-input');
spyOn(input, 'click'); jest.spyOn(input, 'click').mockReturnValue();
btn.click(); btn.click();
......
...@@ -17,51 +17,44 @@ describe('Icon utils', () => { ...@@ -17,51 +17,44 @@ describe('Icon utils', () => {
let axiosMock; let axiosMock;
let mockEndpoint; let mockEndpoint;
let getIcon;
const mockName = 'mockIconName'; const mockName = 'mockIconName';
const mockPath = 'mockPath'; const mockPath = 'mockPath';
const getIcon = () => iconUtils.getSvgIconPathContent(mockName);
beforeEach(() => { beforeEach(() => {
axiosMock = new MockAdapter(axios); axiosMock = new MockAdapter(axios);
mockEndpoint = axiosMock.onGet(gon.sprite_icons); mockEndpoint = axiosMock.onGet(gon.sprite_icons);
getIcon = iconUtils.getSvgIconPathContent(mockName);
}); });
afterEach(() => { afterEach(() => {
axiosMock.restore(); axiosMock.restore();
}); });
it('extracts svg icon path content from sprite icons', done => { it('extracts svg icon path content from sprite icons', () => {
mockEndpoint.replyOnce( mockEndpoint.replyOnce(
200, 200,
`<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`, `<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`,
); );
getIcon
.then(path => { return getIcon().then(path => {
expect(path).toBe(mockPath); expect(path).toBe(mockPath);
done(); });
})
.catch(done.fail);
}); });
it('returns null if icon path content does not exist', done => { it('returns null if icon path content does not exist', () => {
mockEndpoint.replyOnce(200, ``); mockEndpoint.replyOnce(200, ``);
getIcon
.then(path => { return getIcon().then(path => {
expect(path).toBe(null); expect(path).toBe(null);
done(); });
})
.catch(done.fail);
}); });
it('returns null if an http error occurs', done => { it('returns null if an http error occurs', () => {
mockEndpoint.replyOnce(500); mockEndpoint.replyOnce(500);
getIcon
.then(path => { return getIcon().then(path => {
expect(path).toBe(null); expect(path).toBe(null);
done(); });
})
.catch(done.fail);
}); });
}); });
}); });
...@@ -243,7 +243,7 @@ describe('init markdown', () => { ...@@ -243,7 +243,7 @@ describe('init markdown', () => {
}); });
it('uses ace editor insert text when editor is passed in', () => { it('uses ace editor insert text when editor is passed in', () => {
spyOn(editor, 'insert'); jest.spyOn(editor, 'insert').mockReturnValue();
insertMarkdownText({ insertMarkdownText({
text: editor.getValue, text: editor.getValue,
...@@ -258,7 +258,7 @@ describe('init markdown', () => { ...@@ -258,7 +258,7 @@ describe('init markdown', () => {
}); });
it('adds block tags on line above and below selection', () => { it('adds block tags on line above and below selection', () => {
spyOn(editor, 'insert'); jest.spyOn(editor, 'insert').mockReturnValue();
const selected = 'this text \n is multiple \n lines'; const selected = 'this text \n is multiple \n lines';
const text = `before \n ${selected} \n after`; const text = `before \n ${selected} \n after`;
...@@ -276,7 +276,7 @@ describe('init markdown', () => { ...@@ -276,7 +276,7 @@ describe('init markdown', () => {
}); });
it('uses ace editor to navigate back tag length when nothing is selected', () => { it('uses ace editor to navigate back tag length when nothing is selected', () => {
spyOn(editor, 'navigateLeft'); jest.spyOn(editor, 'navigateLeft').mockReturnValue();
insertMarkdownText({ insertMarkdownText({
text: editor.getValue, text: editor.getValue,
...@@ -291,7 +291,7 @@ describe('init markdown', () => { ...@@ -291,7 +291,7 @@ describe('init markdown', () => {
}); });
it('ace editor does not navigate back when there is selected text', () => { it('ace editor does not navigate back when there is selected text', () => {
spyOn(editor, 'navigateLeft'); jest.spyOn(editor, 'navigateLeft').mockReturnValue();
insertMarkdownText({ insertMarkdownText({
text: editor.getValue, text: editor.getValue,
......
...@@ -4,7 +4,10 @@ import UsersCache from '~/lib/utils/users_cache'; ...@@ -4,7 +4,10 @@ import UsersCache from '~/lib/utils/users_cache';
describe('UsersCache', () => { describe('UsersCache', () => {
const dummyUsername = 'win'; const dummyUsername = 'win';
const dummyUserId = 123; const dummyUserId = 123;
const dummyUser = { name: 'has a farm', username: 'farmer' }; const dummyUser = {
name: 'has a farm',
username: 'farmer',
};
const dummyUserStatus = 'my status'; const dummyUserStatus = 'my status';
beforeEach(() => { beforeEach(() => {
...@@ -68,7 +71,6 @@ describe('UsersCache', () => { ...@@ -68,7 +71,6 @@ describe('UsersCache', () => {
it('does nothing if cache contains no matching data', () => { it('does nothing if cache contains no matching data', () => {
UsersCache.internalStorage['no body'] = 'no data'; UsersCache.internalStorage['no body'] = 'no data';
UsersCache.remove(dummyUsername); UsersCache.remove(dummyUsername);
expect(UsersCache.internalStorage['no body']).toBe('no data'); expect(UsersCache.internalStorage['no body']).toBe('no data');
...@@ -76,7 +78,6 @@ describe('UsersCache', () => { ...@@ -76,7 +78,6 @@ describe('UsersCache', () => {
it('removes matching data', () => { it('removes matching data', () => {
UsersCache.internalStorage[dummyUsername] = dummyUser; UsersCache.internalStorage[dummyUsername] = dummyUser;
UsersCache.remove(dummyUsername); UsersCache.remove(dummyUsername);
expect(UsersCache.internalStorage).toEqual({}); expect(UsersCache.internalStorage).toEqual({});
...@@ -87,13 +88,16 @@ describe('UsersCache', () => { ...@@ -87,13 +88,16 @@ describe('UsersCache', () => {
let apiSpy; let apiSpy;
beforeEach(() => { beforeEach(() => {
spyOn(Api, 'users').and.callFake((query, options) => apiSpy(query, options)); jest.spyOn(Api, 'users').mockImplementation((query, options) => apiSpy(query, options));
}); });
it('stores and returns data from API call if cache is empty', done => { it('stores and returns data from API call if cache is empty', done => {
apiSpy = (query, options) => { apiSpy = (query, options) => {
expect(query).toBe(''); expect(query).toBe('');
expect(options).toEqual({ username: dummyUsername }); expect(options).toEqual({
username: dummyUsername,
});
return Promise.resolve({ return Promise.resolve({
data: [dummyUser], data: [dummyUser],
}); });
...@@ -110,14 +114,18 @@ describe('UsersCache', () => { ...@@ -110,14 +114,18 @@ describe('UsersCache', () => {
it('returns undefined if Ajax call fails and cache is empty', done => { it('returns undefined if Ajax call fails and cache is empty', done => {
const dummyError = new Error('server exploded'); const dummyError = new Error('server exploded');
apiSpy = (query, options) => { apiSpy = (query, options) => {
expect(query).toBe(''); expect(query).toBe('');
expect(options).toEqual({ username: dummyUsername }); expect(options).toEqual({
username: dummyUsername,
});
return Promise.reject(dummyError); return Promise.reject(dummyError);
}; };
UsersCache.retrieve(dummyUsername) UsersCache.retrieve(dummyUsername)
.then(user => fail(`Received unexpected user: ${JSON.stringify(user)}`)) .then(user => done.fail(`Received unexpected user: ${JSON.stringify(user)}`))
.catch(error => { .catch(error => {
expect(error).toBe(dummyError); expect(error).toBe(dummyError);
}) })
...@@ -127,7 +135,8 @@ describe('UsersCache', () => { ...@@ -127,7 +135,8 @@ describe('UsersCache', () => {
it('makes no Ajax call if matching data exists', done => { it('makes no Ajax call if matching data exists', done => {
UsersCache.internalStorage[dummyUsername] = dummyUser; UsersCache.internalStorage[dummyUsername] = dummyUser;
apiSpy = () => fail(new Error('expected no Ajax call!'));
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
UsersCache.retrieve(dummyUsername) UsersCache.retrieve(dummyUsername)
.then(user => { .then(user => {
...@@ -142,12 +151,13 @@ describe('UsersCache', () => { ...@@ -142,12 +151,13 @@ describe('UsersCache', () => {
let apiSpy; let apiSpy;
beforeEach(() => { beforeEach(() => {
spyOn(Api, 'user').and.callFake(id => apiSpy(id)); jest.spyOn(Api, 'user').mockImplementation(id => apiSpy(id));
}); });
it('stores and returns data from API call if cache is empty', done => { it('stores and returns data from API call if cache is empty', done => {
apiSpy = id => { apiSpy = id => {
expect(id).toBe(dummyUserId); expect(id).toBe(dummyUserId);
return Promise.resolve({ return Promise.resolve({
data: dummyUser, data: dummyUser,
}); });
...@@ -164,13 +174,15 @@ describe('UsersCache', () => { ...@@ -164,13 +174,15 @@ describe('UsersCache', () => {
it('returns undefined if Ajax call fails and cache is empty', done => { it('returns undefined if Ajax call fails and cache is empty', done => {
const dummyError = new Error('server exploded'); const dummyError = new Error('server exploded');
apiSpy = id => { apiSpy = id => {
expect(id).toBe(dummyUserId); expect(id).toBe(dummyUserId);
return Promise.reject(dummyError); return Promise.reject(dummyError);
}; };
UsersCache.retrieveById(dummyUserId) UsersCache.retrieveById(dummyUserId)
.then(user => fail(`Received unexpected user: ${JSON.stringify(user)}`)) .then(user => done.fail(`Received unexpected user: ${JSON.stringify(user)}`))
.catch(error => { .catch(error => {
expect(error).toBe(dummyError); expect(error).toBe(dummyError);
}) })
...@@ -180,7 +192,8 @@ describe('UsersCache', () => { ...@@ -180,7 +192,8 @@ describe('UsersCache', () => {
it('makes no Ajax call if matching data exists', done => { it('makes no Ajax call if matching data exists', done => {
UsersCache.internalStorage[dummyUserId] = dummyUser; UsersCache.internalStorage[dummyUserId] = dummyUser;
apiSpy = () => fail(new Error('expected no Ajax call!'));
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
UsersCache.retrieveById(dummyUserId) UsersCache.retrieveById(dummyUserId)
.then(user => { .then(user => {
...@@ -195,12 +208,13 @@ describe('UsersCache', () => { ...@@ -195,12 +208,13 @@ describe('UsersCache', () => {
let apiSpy; let apiSpy;
beforeEach(() => { beforeEach(() => {
spyOn(Api, 'userStatus').and.callFake(id => apiSpy(id)); jest.spyOn(Api, 'userStatus').mockImplementation(id => apiSpy(id));
}); });
it('stores and returns data from API call if cache is empty', done => { it('stores and returns data from API call if cache is empty', done => {
apiSpy = id => { apiSpy = id => {
expect(id).toBe(dummyUserId); expect(id).toBe(dummyUserId);
return Promise.resolve({ return Promise.resolve({
data: dummyUserStatus, data: dummyUserStatus,
}); });
...@@ -217,13 +231,15 @@ describe('UsersCache', () => { ...@@ -217,13 +231,15 @@ describe('UsersCache', () => {
it('returns undefined if Ajax call fails and cache is empty', done => { it('returns undefined if Ajax call fails and cache is empty', done => {
const dummyError = new Error('server exploded'); const dummyError = new Error('server exploded');
apiSpy = id => { apiSpy = id => {
expect(id).toBe(dummyUserId); expect(id).toBe(dummyUserId);
return Promise.reject(dummyError); return Promise.reject(dummyError);
}; };
UsersCache.retrieveStatusById(dummyUserId) UsersCache.retrieveStatusById(dummyUserId)
.then(userStatus => fail(`Received unexpected user: ${JSON.stringify(userStatus)}`)) .then(userStatus => done.fail(`Received unexpected user: ${JSON.stringify(userStatus)}`))
.catch(error => { .catch(error => {
expect(error).toBe(dummyError); expect(error).toBe(dummyError);
}) })
...@@ -232,8 +248,11 @@ describe('UsersCache', () => { ...@@ -232,8 +248,11 @@ describe('UsersCache', () => {
}); });
it('makes no Ajax call if matching data exists', done => { it('makes no Ajax call if matching data exists', done => {
UsersCache.internalStorage[dummyUserId] = { status: dummyUserStatus }; UsersCache.internalStorage[dummyUserId] = {
apiSpy = () => fail(new Error('expected no Ajax call!')); status: dummyUserStatus,
};
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
UsersCache.retrieveStatusById(dummyUserId) UsersCache.retrieveStatusById(dummyUserId)
.then(userStatus => { .then(userStatus => {
......
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