Commit 5e9fc348 authored by Vitaly Slobodin's avatar Vitaly Slobodin Committed by David O'Regan

Remove jest test callbacks from specs

Jest 27 does not support the test callback anymore so
the following code is no longer valid:

```
it('test', (done) => { /* code */ })
```
parent 45490c79
......@@ -42,9 +42,9 @@ describe('AddContextCommitsModalStoreActions', () => {
});
describe('setBaseConfig', () => {
it('commits SET_BASE_CONFIG', (done) => {
it('commits SET_BASE_CONFIG', () => {
const options = { contextCommitsPath, mergeRequestIid, projectId };
testAction(
return testAction(
setBaseConfig,
options,
{
......@@ -59,62 +59,54 @@ describe('AddContextCommitsModalStoreActions', () => {
},
],
[],
done,
);
});
});
describe('setTabIndex', () => {
it('commits SET_TABINDEX', (done) => {
testAction(
it('commits SET_TABINDEX', () => {
return testAction(
setTabIndex,
{ tabIndex: 1 },
{ tabIndex: 0 },
[{ type: types.SET_TABINDEX, payload: { tabIndex: 1 } }],
[],
done,
);
});
});
describe('setCommits', () => {
it('commits SET_COMMITS', (done) => {
testAction(
it('commits SET_COMMITS', () => {
return testAction(
setCommits,
{ commits: [], silentAddition: false },
{ isLoadingCommits: false, commits: [] },
[{ type: types.SET_COMMITS, payload: [] }],
[],
done,
);
});
it('commits SET_COMMITS_SILENT', (done) => {
testAction(
it('commits SET_COMMITS_SILENT', () => {
return testAction(
setCommits,
{ commits: [], silentAddition: true },
{ isLoadingCommits: true, commits: [] },
[{ type: types.SET_COMMITS_SILENT, payload: [] }],
[],
done,
);
});
});
describe('createContextCommits', () => {
it('calls API to create context commits', (done) => {
it('calls API to create context commits', async () => {
mock.onPost(contextCommitEndpoint).reply(200, {});
testAction(createContextCommits, { commits: [] }, {}, [], [], done);
await testAction(createContextCommits, { commits: [] }, {}, [], []);
createContextCommits(
await createContextCommits(
{ state: { projectId, mergeRequestIid }, commit: () => null },
{ commits: [] },
)
.then(() => {
done();
})
.catch(done.fail);
);
});
});
......@@ -126,9 +118,9 @@ describe('AddContextCommitsModalStoreActions', () => {
)
.reply(200, [dummyCommit]);
});
it('commits FETCH_CONTEXT_COMMITS', (done) => {
it('commits FETCH_CONTEXT_COMMITS', () => {
const contextCommit = { ...dummyCommit, isSelected: true };
testAction(
return testAction(
fetchContextCommits,
null,
{
......@@ -144,20 +136,18 @@ describe('AddContextCommitsModalStoreActions', () => {
{ type: 'setCommits', payload: { commits: [contextCommit], silentAddition: true } },
{ type: 'setSelectedCommits', payload: [contextCommit] },
],
done,
);
});
});
describe('setContextCommits', () => {
it('commits SET_CONTEXT_COMMITS', (done) => {
testAction(
it('commits SET_CONTEXT_COMMITS', () => {
return testAction(
setContextCommits,
{ data: [] },
{ contextCommits: [], isLoadingContextCommits: false },
[{ type: types.SET_CONTEXT_COMMITS, payload: { data: [] } }],
[],
done,
);
});
});
......@@ -168,71 +158,66 @@ describe('AddContextCommitsModalStoreActions', () => {
.onDelete('/api/v4/projects/gitlab-org%2Fgitlab/merge_requests/1/context_commits')
.reply(204);
});
it('calls API to remove context commits', (done) => {
testAction(
it('calls API to remove context commits', () => {
return testAction(
removeContextCommits,
{ forceReload: false },
{ mergeRequestIid, projectId, toRemoveCommits: [] },
[],
[],
done,
);
});
});
describe('setSelectedCommits', () => {
it('commits SET_SELECTED_COMMITS', (done) => {
testAction(
it('commits SET_SELECTED_COMMITS', () => {
return testAction(
setSelectedCommits,
[dummyCommit],
{ selectedCommits: [] },
[{ type: types.SET_SELECTED_COMMITS, payload: [dummyCommit] }],
[],
done,
);
});
});
describe('setSearchText', () => {
it('commits SET_SEARCH_TEXT', (done) => {
it('commits SET_SEARCH_TEXT', () => {
const searchText = 'Dummy Text';
testAction(
return testAction(
setSearchText,
searchText,
{ searchText: '' },
[{ type: types.SET_SEARCH_TEXT, payload: searchText }],
[],
done,
);
});
});
describe('setToRemoveCommits', () => {
it('commits SET_TO_REMOVE_COMMITS', (done) => {
it('commits SET_TO_REMOVE_COMMITS', () => {
const commitId = 'abcde';
testAction(
return testAction(
setToRemoveCommits,
[commitId],
{ toRemoveCommits: [] },
[{ type: types.SET_TO_REMOVE_COMMITS, payload: [commitId] }],
[],
done,
);
});
});
describe('resetModalState', () => {
it('commits RESET_MODAL_STATE', (done) => {
it('commits RESET_MODAL_STATE', () => {
const commitId = 'abcde';
testAction(
return testAction(
resetModalState,
null,
{ toRemoveCommits: [commitId] },
[{ type: types.RESET_MODAL_STATE }],
[],
done,
);
});
});
......
......@@ -22,8 +22,8 @@ describe('Admin statistics panel actions', () => {
mock.onGet(/api\/(.*)\/application\/statistics/).replyOnce(200, mockStatistics);
});
it('dispatches success with received data', (done) =>
testAction(
it('dispatches success with received data', () => {
return testAction(
actions.fetchStatistics,
null,
state,
......@@ -37,8 +37,8 @@ describe('Admin statistics panel actions', () => {
),
},
],
done,
));
);
});
});
describe('error', () => {
......@@ -46,8 +46,8 @@ describe('Admin statistics panel actions', () => {
mock.onGet(/api\/(.*)\/application\/statistics/).replyOnce(500);
});
it('dispatches error', (done) =>
testAction(
it('dispatches error', () => {
return testAction(
actions.fetchStatistics,
null,
state,
......@@ -61,26 +61,26 @@ describe('Admin statistics panel actions', () => {
payload: new Error('Request failed with status code 500'),
},
],
done,
));
);
});
});
});
describe('requestStatistic', () => {
it('should commit the request mutation', (done) =>
testAction(
it('should commit the request mutation', () => {
return testAction(
actions.requestStatistics,
null,
state,
[{ type: types.REQUEST_STATISTICS }],
[],
done,
));
);
});
});
describe('receiveStatisticsSuccess', () => {
it('should commit received data', (done) =>
testAction(
it('should commit received data', () => {
return testAction(
actions.receiveStatisticsSuccess,
mockStatistics,
state,
......@@ -91,13 +91,13 @@ describe('Admin statistics panel actions', () => {
},
],
[],
done,
));
);
});
});
describe('receiveStatisticsError', () => {
it('should commit error', (done) => {
testAction(
it('should commit error', () => {
return testAction(
actions.receiveStatisticsError,
500,
state,
......@@ -108,7 +108,6 @@ describe('Admin statistics panel actions', () => {
},
],
[],
done,
);
});
});
......
......@@ -36,24 +36,19 @@ describe('U2FAuthenticate', () => {
window.u2f = oldu2f;
});
it('falls back to normal 2fa', (done) => {
component
.start()
.then(() => {
expect(component.switchToFallbackUI).toHaveBeenCalled();
done();
})
.catch(done.fail);
it('falls back to normal 2fa', async () => {
await component.start();
expect(component.switchToFallbackUI).toHaveBeenCalled();
});
});
describe('with u2f available', () => {
beforeEach((done) => {
beforeEach(() => {
// bypass automatic form submission within renderAuthenticated
jest.spyOn(component, 'renderAuthenticated').mockReturnValue(true);
u2fDevice = new MockU2FDevice();
component.start().then(done).catch(done.fail);
return component.start();
});
it('allows authenticating via a U2F device', () => {
......
......@@ -8,12 +8,12 @@ describe('U2FRegister', () => {
let container;
let component;
beforeEach((done) => {
beforeEach(() => {
loadFixtures('u2f/register.html');
u2fDevice = new MockU2FDevice();
container = $('#js-register-token-2fa');
component = new U2FRegister(container, {});
component.start().then(done).catch(done.fail);
return component.start();
});
it('allows registering a U2F device', () => {
......
......@@ -38,35 +38,25 @@ describe('AjaxFilter', () => {
dummyList.list.appendChild(dynamicList);
});
it('calls onLoadingFinished after loading data', (done) => {
it('calls onLoadingFinished after loading data', async () => {
ajaxSpy = (url) => {
expect(url).toBe('dummy endpoint?dummy search key=');
return Promise.resolve(dummyData);
};
AjaxFilter.trigger()
.then(() => {
expect(dummyConfig.onLoadingFinished.mock.calls.length).toBe(1);
})
.then(done)
.catch(done.fail);
await AjaxFilter.trigger();
expect(dummyConfig.onLoadingFinished.mock.calls.length).toBe(1);
});
it('does not call onLoadingFinished if Ajax call fails', (done) => {
it('does not call onLoadingFinished if Ajax call fails', async () => {
const dummyError = new Error('My dummy is sick! :-(');
ajaxSpy = (url) => {
expect(url).toBe('dummy endpoint?dummy search key=');
return Promise.reject(dummyError);
};
AjaxFilter.trigger()
.then(done.fail)
.catch((error) => {
expect(error).toBe(dummyError);
expect(dummyConfig.onLoadingFinished.mock.calls.length).toBe(0);
})
.then(done)
.catch(done.fail);
await expect(AjaxFilter.trigger()).rejects.toEqual(dummyError);
expect(dummyConfig.onLoadingFinished.mock.calls.length).toBe(0);
});
});
});
......@@ -190,43 +190,40 @@ describe('Filtered Search Manager', () => {
const defaultParams = '?scope=all';
const defaultState = '&state=opened';
it('should search with a single word', (done) => {
it('should search with a single word', () => {
initializeManager();
input.value = 'searchTerm';
visitUrl.mockImplementation((url) => {
expect(url).toEqual(`${defaultParams}&search=searchTerm`);
done();
});
manager.search();
});
it('sets default state', (done) => {
it('sets default state', () => {
initializeManager({ useDefaultState: true });
input.value = 'searchTerm';
visitUrl.mockImplementation((url) => {
expect(url).toEqual(`${defaultParams}${defaultState}&search=searchTerm`);
done();
});
manager.search();
});
it('should search with multiple words', (done) => {
it('should search with multiple words', () => {
initializeManager();
input.value = 'awesome search terms';
visitUrl.mockImplementation((url) => {
expect(url).toEqual(`${defaultParams}&search=awesome+search+terms`);
done();
});
manager.search();
});
it('should search with special characters', (done) => {
it('should search with special characters', () => {
initializeManager();
input.value = '~!@#$%^&*()_+{}:<>,.?/';
......@@ -234,13 +231,12 @@ describe('Filtered Search Manager', () => {
expect(url).toEqual(
`${defaultParams}&search=~!%40%23%24%25%5E%26*()_%2B%7B%7D%3A%3C%3E%2C.%3F%2F`,
);
done();
});
manager.search();
});
it('should use replacement URL for condition', (done) => {
it('should use replacement URL for condition', () => {
initializeManager();
tokensContainer.innerHTML = FilteredSearchSpecHelper.createTokensContainerHTML(
FilteredSearchSpecHelper.createFilterVisualTokenHTML('milestone', '=', '13', true),
......@@ -248,7 +244,6 @@ describe('Filtered Search Manager', () => {
visitUrl.mockImplementation((url) => {
expect(url).toEqual(`${defaultParams}&milestone_title=replaced`);
done();
});
manager.filteredSearchTokenKeys.conditions.push({
......@@ -261,7 +256,7 @@ describe('Filtered Search Manager', () => {
manager.search();
});
it('removes duplicated tokens', (done) => {
it('removes duplicated tokens', () => {
initializeManager();
tokensContainer.innerHTML = FilteredSearchSpecHelper.createTokensContainerHTML(`
${FilteredSearchSpecHelper.createFilterVisualTokenHTML('label', '=', '~bug')}
......@@ -270,7 +265,6 @@ describe('Filtered Search Manager', () => {
visitUrl.mockImplementation((url) => {
expect(url).toEqual(`${defaultParams}&label_name[]=bug`);
done();
});
manager.search();
......
......@@ -18,53 +18,47 @@ describe('RecentSearchesService', () => {
jest.spyOn(RecentSearchesService, 'isAvailable').mockReturnValue(true);
});
it('should default to empty array', (done) => {
it('should default to empty array', () => {
const fetchItemsPromise = service.fetch();
fetchItemsPromise
.then((items) => {
expect(items).toEqual([]);
})
.then(done)
.catch(done.fail);
return fetchItemsPromise.then((items) => {
expect(items).toEqual([]);
});
});
it('should reject when unable to parse', (done) => {
it('should reject when unable to parse', () => {
jest.spyOn(localStorage, 'getItem').mockReturnValue('fail');
const fetchItemsPromise = service.fetch();
fetchItemsPromise
.then(done.fail)
return fetchItemsPromise
.then(() => {
throw new Error();
})
.catch((error) => {
expect(error).toEqual(expect.any(SyntaxError));
})
.then(done)
.catch(done.fail);
});
});
it('should reject when service is unavailable', (done) => {
it('should reject when service is unavailable', () => {
RecentSearchesService.isAvailable.mockReturnValue(false);
service
return service
.fetch()
.then(done.fail)
.then(() => {
throw new Error();
})
.catch((error) => {
expect(error).toEqual(expect.any(Error));
})
.then(done)
.catch(done.fail);
});
});
it('should return items from localStorage', (done) => {
it('should return items from localStorage', () => {
jest.spyOn(localStorage, 'getItem').mockReturnValue('["foo", "bar"]');
const fetchItemsPromise = service.fetch();
fetchItemsPromise
.then((items) => {
expect(items).toEqual(['foo', 'bar']);
})
.then(done)
.catch(done.fail);
return fetchItemsPromise.then((items) => {
expect(items).toEqual(['foo', 'bar']);
});
});
describe('if .isAvailable returns `false`', () => {
......@@ -74,16 +68,16 @@ describe('RecentSearchesService', () => {
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {});
});
it('should not call .getItem', (done) => {
RecentSearchesService.prototype
it('should not call .getItem', () => {
return RecentSearchesService.prototype
.fetch()
.then(done.fail)
.then(() => {
throw new Error();
})
.catch((err) => {
expect(err).toEqual(new RecentSearchesServiceError());
expect(localStorage.getItem).not.toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
});
});
});
});
......
......@@ -46,7 +46,7 @@ describe('Filtered Search Visual Tokens', () => {
jest.spyOn(UsersCache, 'retrieve').mockImplementation((username) => usersCacheSpy(username));
});
it('ignores error if UsersCache throws', (done) => {
it('ignores error if UsersCache throws', async () => {
const dummyError = new Error('Earth rotated backwards');
const { subject, tokenValueContainer, tokenValueElement } = findElements(authorToken);
const tokenValue = tokenValueElement.innerText;
......@@ -55,16 +55,11 @@ describe('Filtered Search Visual Tokens', () => {
return Promise.reject(dummyError);
};
subject
.updateUserTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue)
.then(() => {
expect(createFlash.mock.calls.length).toBe(0);
})
.then(done)
.catch(done.fail);
await subject.updateUserTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue);
expect(createFlash.mock.calls.length).toBe(0);
});
it('does nothing if user cannot be found', (done) => {
it('does nothing if user cannot be found', async () => {
const { subject, tokenValueContainer, tokenValueElement } = findElements(authorToken);
const tokenValue = tokenValueElement.innerText;
usersCacheSpy = (username) => {
......@@ -72,16 +67,11 @@ describe('Filtered Search Visual Tokens', () => {
return Promise.resolve(undefined);
};
subject
.updateUserTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue)
.then(() => {
expect(tokenValueElement.innerText).toBe(tokenValue);
})
.then(done)
.catch(done.fail);
await subject.updateUserTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue);
expect(tokenValueElement.innerText).toBe(tokenValue);
});
it('replaces author token with avatar and display name', (done) => {
it('replaces author token with avatar and display name', async () => {
const dummyUser = {
name: 'Important Person',
avatar_url: 'https://host.invalid/mypics/avatar.png',
......@@ -93,21 +83,16 @@ describe('Filtered Search Visual Tokens', () => {
return Promise.resolve(dummyUser);
};
subject
.updateUserTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue)
.then(() => {
expect(tokenValueContainer.dataset.originalValue).toBe(tokenValue);
expect(tokenValueElement.innerText.trim()).toBe(dummyUser.name);
const avatar = tokenValueElement.querySelector('img.avatar');
expect(avatar.getAttribute('src')).toBe(dummyUser.avatar_url);
expect(avatar.getAttribute('alt')).toBe('');
})
.then(done)
.catch(done.fail);
await subject.updateUserTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue);
expect(tokenValueContainer.dataset.originalValue).toBe(tokenValue);
expect(tokenValueElement.innerText.trim()).toBe(dummyUser.name);
const avatar = tokenValueElement.querySelector('img.avatar');
expect(avatar.getAttribute('src')).toBe(dummyUser.avatar_url);
expect(avatar.getAttribute('alt')).toBe('');
});
it('escapes user name when creating token', (done) => {
it('escapes user name when creating token', async () => {
const dummyUser = {
name: '<script>',
avatar_url: `${TEST_HOST}/mypics/avatar.png`,
......@@ -119,16 +104,11 @@ describe('Filtered Search Visual Tokens', () => {
return Promise.resolve(dummyUser);
};
subject
.updateUserTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue)
.then(() => {
expect(tokenValueElement.innerText.trim()).toBe(dummyUser.name);
tokenValueElement.querySelector('.avatar').remove();
await subject.updateUserTokenAppearance(tokenValueContainer, tokenValueElement, tokenValue);
expect(tokenValueElement.innerText.trim()).toBe(dummyUser.name);
tokenValueElement.querySelector('.avatar').remove();
expect(tokenValueElement.innerHTML.trim()).toBe(escape(dummyUser.name));
})
.then(done)
.catch(done.fail);
expect(tokenValueElement.innerHTML.trim()).toBe(escape(dummyUser.name));
});
});
......@@ -177,48 +157,33 @@ describe('Filtered Search Visual Tokens', () => {
const findLabel = (tokenValue) =>
labelData.find((label) => tokenValue === `~${DropdownUtils.getEscapedText(label.title)}`);
it('updates the color of a label token', (done) => {
it('updates the color of a label token', async () => {
const { subject, tokenValueContainer, tokenValueElement } = findElements(bugLabelToken);
const tokenValue = tokenValueElement.innerText;
const matchingLabel = findLabel(tokenValue);
subject
.updateLabelTokenColor(tokenValueContainer, tokenValue)
.then(() => {
expectValueContainerStyle(tokenValueContainer, matchingLabel);
})
.then(done)
.catch(done.fail);
await subject.updateLabelTokenColor(tokenValueContainer, tokenValue);
expectValueContainerStyle(tokenValueContainer, matchingLabel);
});
it('updates the color of a label token with spaces', (done) => {
it('updates the color of a label token with spaces', async () => {
const { subject, tokenValueContainer, tokenValueElement } = findElements(spaceLabelToken);
const tokenValue = tokenValueElement.innerText;
const matchingLabel = findLabel(tokenValue);
subject
.updateLabelTokenColor(tokenValueContainer, tokenValue)
.then(() => {
expectValueContainerStyle(tokenValueContainer, matchingLabel);
})
.then(done)
.catch(done.fail);
await subject.updateLabelTokenColor(tokenValueContainer, tokenValue);
expectValueContainerStyle(tokenValueContainer, matchingLabel);
});
it('does not change color of a missing label', (done) => {
it('does not change color of a missing label', async () => {
const { subject, tokenValueContainer, tokenValueElement } = findElements(missingLabelToken);
const tokenValue = tokenValueElement.innerText;
const matchingLabel = findLabel(tokenValue);
expect(matchingLabel).toBe(undefined);
subject
.updateLabelTokenColor(tokenValueContainer, tokenValue)
.then(() => {
expect(tokenValueContainer.getAttribute('style')).toBe(null);
})
.then(done)
.catch(done.fail);
await subject.updateLabelTokenColor(tokenValueContainer, tokenValue);
expect(tokenValueContainer.getAttribute('style')).toBe(null);
});
});
......
......@@ -29,136 +29,126 @@ describe('Frequent Items Dropdown Store Actions', () => {
});
describe('setNamespace', () => {
it('should set namespace', (done) => {
testAction(
it('should set namespace', () => {
return testAction(
actions.setNamespace,
mockNamespace,
mockedState,
[{ type: types.SET_NAMESPACE, payload: mockNamespace }],
[],
done,
);
});
});
describe('setStorageKey', () => {
it('should set storage key', (done) => {
testAction(
it('should set storage key', () => {
return testAction(
actions.setStorageKey,
mockStorageKey,
mockedState,
[{ type: types.SET_STORAGE_KEY, payload: mockStorageKey }],
[],
done,
);
});
});
describe('requestFrequentItems', () => {
it('should request frequent items', (done) => {
testAction(
it('should request frequent items', () => {
return testAction(
actions.requestFrequentItems,
null,
mockedState,
[{ type: types.REQUEST_FREQUENT_ITEMS }],
[],
done,
);
});
});
describe('receiveFrequentItemsSuccess', () => {
it('should set frequent items', (done) => {
testAction(
it('should set frequent items', () => {
return testAction(
actions.receiveFrequentItemsSuccess,
mockFrequentProjects,
mockedState,
[{ type: types.RECEIVE_FREQUENT_ITEMS_SUCCESS, payload: mockFrequentProjects }],
[],
done,
);
});
});
describe('receiveFrequentItemsError', () => {
it('should set frequent items error state', (done) => {
testAction(
it('should set frequent items error state', () => {
return testAction(
actions.receiveFrequentItemsError,
null,
mockedState,
[{ type: types.RECEIVE_FREQUENT_ITEMS_ERROR }],
[],
done,
);
});
});
describe('fetchFrequentItems', () => {
it('should dispatch `receiveFrequentItemsSuccess`', (done) => {
it('should dispatch `receiveFrequentItemsSuccess`', () => {
mockedState.namespace = mockNamespace;
mockedState.storageKey = mockStorageKey;
testAction(
return testAction(
actions.fetchFrequentItems,
null,
mockedState,
[],
[{ type: 'requestFrequentItems' }, { type: 'receiveFrequentItemsSuccess', payload: [] }],
done,
);
});
it('should dispatch `receiveFrequentItemsError`', (done) => {
it('should dispatch `receiveFrequentItemsError`', () => {
jest.spyOn(AccessorUtilities, 'canUseLocalStorage').mockReturnValue(false);
mockedState.namespace = mockNamespace;
mockedState.storageKey = mockStorageKey;
testAction(
return testAction(
actions.fetchFrequentItems,
null,
mockedState,
[],
[{ type: 'requestFrequentItems' }, { type: 'receiveFrequentItemsError' }],
done,
);
});
});
describe('requestSearchedItems', () => {
it('should request searched items', (done) => {
testAction(
it('should request searched items', () => {
return testAction(
actions.requestSearchedItems,
null,
mockedState,
[{ type: types.REQUEST_SEARCHED_ITEMS }],
[],
done,
);
});
});
describe('receiveSearchedItemsSuccess', () => {
it('should set searched items', (done) => {
testAction(
it('should set searched items', () => {
return testAction(
actions.receiveSearchedItemsSuccess,
mockSearchedProjects,
mockedState,
[{ type: types.RECEIVE_SEARCHED_ITEMS_SUCCESS, payload: mockSearchedProjects }],
[],
done,
);
});
});
describe('receiveSearchedItemsError', () => {
it('should set searched items error state', (done) => {
testAction(
it('should set searched items error state', () => {
return testAction(
actions.receiveSearchedItemsError,
null,
mockedState,
[{ type: types.RECEIVE_SEARCHED_ITEMS_ERROR }],
[],
done,
);
});
});
......@@ -168,10 +158,10 @@ describe('Frequent Items Dropdown Store Actions', () => {
gon.api_version = 'v4';
});
it('should dispatch `receiveSearchedItemsSuccess`', (done) => {
it('should dispatch `receiveSearchedItemsSuccess`', () => {
mock.onGet(/\/api\/v4\/projects.json(.*)$/).replyOnce(200, mockSearchedProjects, {});
testAction(
return testAction(
actions.fetchSearchedItems,
null,
mockedState,
......@@ -183,45 +173,41 @@ describe('Frequent Items Dropdown Store Actions', () => {
payload: { data: mockSearchedProjects, headers: {} },
},
],
done,
);
});
it('should dispatch `receiveSearchedItemsError`', (done) => {
it('should dispatch `receiveSearchedItemsError`', () => {
gon.api_version = 'v4';
mock.onGet(/\/api\/v4\/projects.json(.*)$/).replyOnce(500);
testAction(
return testAction(
actions.fetchSearchedItems,
null,
mockedState,
[],
[{ type: 'requestSearchedItems' }, { type: 'receiveSearchedItemsError' }],
done,
);
});
});
describe('setSearchQuery', () => {
it('should commit query and dispatch `fetchSearchedItems` when query is present', (done) => {
testAction(
it('should commit query and dispatch `fetchSearchedItems` when query is present', () => {
return testAction(
actions.setSearchQuery,
{ query: 'test' },
mockedState,
[{ type: types.SET_SEARCH_QUERY, payload: { query: 'test' } }],
[{ type: 'fetchSearchedItems', payload: { query: 'test' } }],
done,
);
});
it('should commit query and dispatch `fetchFrequentItems` when query is empty', (done) => {
testAction(
it('should commit query and dispatch `fetchFrequentItems` when query is empty', () => {
return testAction(
actions.setSearchQuery,
null,
mockedState,
[{ type: types.SET_SEARCH_QUERY, payload: null }],
[{ type: 'fetchFrequentItems' }],
done,
);
});
});
......
......@@ -11,23 +11,21 @@ describe('initDiscussionTab', () => {
`);
});
it('should pass canCreateNote as false to initImageDiff', (done) => {
it('should pass canCreateNote as false to initImageDiff', () => {
jest
.spyOn(initImageDiffHelper, 'initImageDiff')
.mockImplementation((diffFileEl, canCreateNote) => {
expect(canCreateNote).toEqual(false);
done();
});
initDiscussionTab();
});
it('should pass renderCommentBadge as true to initImageDiff', (done) => {
it('should pass renderCommentBadge as true to initImageDiff', () => {
jest
.spyOn(initImageDiffHelper, 'initImageDiff')
.mockImplementation((diffFileEl, canCreateNote, renderCommentBadge) => {
expect(renderCommentBadge).toEqual(true);
done();
});
initDiscussionTab();
......
......@@ -176,34 +176,36 @@ describe('ReplacedImageDiff', () => {
expect(ImageDiff.prototype.bindEvents).toHaveBeenCalled();
});
it('should register click eventlistener to 2-up view mode', (done) => {
jest.spyOn(ReplacedImageDiff.prototype, 'changeView').mockImplementation((viewMode) => {
expect(viewMode).toEqual(viewTypes.TWO_UP);
done();
});
it('should register click eventlistener to 2-up view mode', () => {
const changeViewSpy = jest
.spyOn(ReplacedImageDiff.prototype, 'changeView')
.mockImplementation(() => {});
replacedImageDiff.bindEvents();
replacedImageDiff.viewModesEls[viewTypes.TWO_UP].click();
expect(changeViewSpy).toHaveBeenCalledWith(viewTypes.TWO_UP, expect.any(Object));
});
it('should register click eventlistener to swipe view mode', (done) => {
jest.spyOn(ReplacedImageDiff.prototype, 'changeView').mockImplementation((viewMode) => {
expect(viewMode).toEqual(viewTypes.SWIPE);
done();
});
it('should register click eventlistener to swipe view mode', () => {
const changeViewSpy = jest
.spyOn(ReplacedImageDiff.prototype, 'changeView')
.mockImplementation(() => {});
replacedImageDiff.bindEvents();
replacedImageDiff.viewModesEls[viewTypes.SWIPE].click();
expect(changeViewSpy).toHaveBeenCalledWith(viewTypes.SWIPE, expect.any(Object));
});
it('should register click eventlistener to onion skin view mode', (done) => {
jest.spyOn(ReplacedImageDiff.prototype, 'changeView').mockImplementation((viewMode) => {
expect(viewMode).toEqual(viewTypes.SWIPE);
done();
});
it('should register click eventlistener to onion skin view mode', () => {
const changeViewSpy = jest
.spyOn(ReplacedImageDiff.prototype, 'changeView')
.mockImplementation(() => {});
replacedImageDiff.bindEvents();
replacedImageDiff.viewModesEls[viewTypes.SWIPE].click();
expect(changeViewSpy).toHaveBeenCalledWith(viewTypes.SWIPE, expect.any(Object));
});
});
......@@ -325,32 +327,34 @@ describe('ReplacedImageDiff', () => {
setupImageFrameEls();
});
it('should pass showCommentIndicator normalized indicator values', (done) => {
it('should pass showCommentIndicator normalized indicator values', () => {
jest.spyOn(imageDiffHelper, 'showCommentIndicator').mockImplementation(() => {});
jest
const resizeCoordinatesToImageElementSpy = jest
.spyOn(imageDiffHelper, 'resizeCoordinatesToImageElement')
.mockImplementation((imageEl, meta) => {
expect(meta.x).toEqual(indicator.x);
expect(meta.y).toEqual(indicator.y);
expect(meta.width).toEqual(indicator.image.width);
expect(meta.height).toEqual(indicator.image.height);
done();
});
.mockImplementation(() => {});
replacedImageDiff.renderNewView(indicator);
expect(resizeCoordinatesToImageElementSpy).toHaveBeenCalledWith(undefined, {
x: indicator.x,
y: indicator.y,
width: indicator.image.width,
height: indicator.image.height,
});
});
it('should call showCommentIndicator', (done) => {
it('should call showCommentIndicator', () => {
const normalized = {
normalized: true,
};
jest.spyOn(imageDiffHelper, 'resizeCoordinatesToImageElement').mockReturnValue(normalized);
jest
const showCommentIndicatorSpy = jest
.spyOn(imageDiffHelper, 'showCommentIndicator')
.mockImplementation((imageFrameEl, normalizedIndicator) => {
expect(normalizedIndicator).toEqual(normalized);
done();
});
.mockImplementation(() => {});
replacedImageDiff.renderNewView(indicator);
expect(showCommentIndicatorSpy).toHaveBeenCalledWith(undefined, normalized);
});
});
});
......
......@@ -38,21 +38,16 @@ describe('CreateMergeRequestDropdown', () => {
});
describe('getRef', () => {
it('escapes branch names correctly', (done) => {
it('escapes branch names correctly', async () => {
const endpoint = `${dropdown.refsPath}contains%23hash`;
jest.spyOn(axios, 'get');
axiosMock.onGet(endpoint).replyOnce({});
dropdown
.getRef('contains#hash')
.then(() => {
expect(axios.get).toHaveBeenCalledWith(
endpoint,
expect.objectContaining({ cancelToken: expect.anything() }),
);
})
.then(done)
.catch(done.fail);
await dropdown.getRef('contains#hash');
expect(axios.get).toHaveBeenCalledWith(
endpoint,
expect.objectContaining({ cancelToken: expect.anything() }),
);
});
});
......
......@@ -23,90 +23,82 @@ describe('RelatedMergeRequest store actions', () => {
});
describe('setInitialState', () => {
it('commits types.SET_INITIAL_STATE with given props', (done) => {
it('commits types.SET_INITIAL_STATE with given props', () => {
const props = { a: 1, b: 2 };
testAction(
return testAction(
actions.setInitialState,
props,
{},
[{ type: types.SET_INITIAL_STATE, payload: props }],
[],
done,
);
});
});
describe('requestData', () => {
it('commits types.REQUEST_DATA', (done) => {
testAction(actions.requestData, null, {}, [{ type: types.REQUEST_DATA }], [], done);
it('commits types.REQUEST_DATA', () => {
return testAction(actions.requestData, null, {}, [{ type: types.REQUEST_DATA }], []);
});
});
describe('receiveDataSuccess', () => {
it('commits types.RECEIVE_DATA_SUCCESS with data', (done) => {
it('commits types.RECEIVE_DATA_SUCCESS with data', () => {
const data = { a: 1, b: 2 };
testAction(
return testAction(
actions.receiveDataSuccess,
data,
{},
[{ type: types.RECEIVE_DATA_SUCCESS, payload: data }],
[],
done,
);
});
});
describe('receiveDataError', () => {
it('commits types.RECEIVE_DATA_ERROR', (done) => {
testAction(
it('commits types.RECEIVE_DATA_ERROR', () => {
return testAction(
actions.receiveDataError,
null,
{},
[{ type: types.RECEIVE_DATA_ERROR }],
[],
done,
);
});
});
describe('fetchMergeRequests', () => {
describe('for a successful request', () => {
it('should dispatch success action', (done) => {
it('should dispatch success action', () => {
const data = { a: 1 };
mock.onGet(`${state.apiEndpoint}?per_page=100`).replyOnce(200, data, { 'x-total': 2 });
testAction(
return testAction(
actions.fetchMergeRequests,
null,
state,
[],
[{ type: 'requestData' }, { type: 'receiveDataSuccess', payload: { data, total: 2 } }],
done,
);
});
});
describe('for a failing request', () => {
it('should dispatch error action', (done) => {
it('should dispatch error action', async () => {
mock.onGet(`${state.apiEndpoint}?per_page=100`).replyOnce(400);
testAction(
await testAction(
actions.fetchMergeRequests,
null,
state,
[],
[{ type: 'requestData' }, { type: 'receiveDataError' }],
() => {
expect(createFlash).toHaveBeenCalledTimes(1);
expect(createFlash).toHaveBeenCalledWith({
message: expect.stringMatching('Something went wrong'),
});
done();
},
);
expect(createFlash).toHaveBeenCalledTimes(1);
expect(createFlash).toHaveBeenCalledWith({
message: expect.stringMatching('Something went wrong'),
});
});
});
});
......
......@@ -375,8 +375,8 @@ describe('Job App', () => {
});
describe('sidebar', () => {
it('has no blank blocks', (done) => {
setupAndMount({
it('has no blank blocks', async () => {
await setupAndMount({
jobData: {
duration: null,
finished_at: null,
......@@ -387,17 +387,14 @@ describe('Job App', () => {
tags: [],
cancel_path: null,
},
})
.then(() => {
const blocks = wrapper.findAll('.blocks-container > *').wrappers;
expect(blocks.length).toBeGreaterThan(0);
blocks.forEach((block) => {
expect(block.text().trim()).not.toBe('');
});
})
.then(done)
.catch(done.fail);
});
const blocks = wrapper.findAll('.blocks-container > *').wrappers;
expect(blocks.length).toBeGreaterThan(0);
blocks.forEach((block) => {
expect(block.text().trim()).not.toBe('');
});
});
});
});
......
This diff is collapsed.
......@@ -50,7 +50,7 @@ describe('Promote label modal', () => {
vm.$destroy();
});
it('redirects when a label is promoted', (done) => {
it('redirects when a label is promoted', () => {
const responseURL = `${TEST_HOST}/dummy/endpoint`;
jest.spyOn(axios, 'post').mockImplementation((url) => {
expect(url).toBe(labelMockData.url);
......@@ -65,39 +65,35 @@ describe('Promote label modal', () => {
});
});
vm.onSubmit()
.then(() => {
expect(eventHub.$emit).toHaveBeenCalledWith('promoteLabelModal.requestFinished', {
labelUrl: labelMockData.url,
successful: true,
});
})
.then(done)
.catch(done.fail);
return vm.onSubmit().then(() => {
expect(eventHub.$emit).toHaveBeenCalledWith('promoteLabelModal.requestFinished', {
labelUrl: labelMockData.url,
successful: true,
});
});
});
it('displays an error if promoting a label failed', (done) => {
it('displays an error if promoting a label failed', () => {
const dummyError = new Error('promoting label failed');
dummyError.response = { status: 500 };
jest.spyOn(axios, 'post').mockImplementation((url) => {
expect(url).toBe(labelMockData.url);
expect(eventHub.$emit).toHaveBeenCalledWith(
'promoteLabelModal.requestStarted',
labelMockData.url,
);
return Promise.reject(dummyError);
});
vm.onSubmit()
.catch((error) => {
expect(error).toBe(dummyError);
expect(eventHub.$emit).toHaveBeenCalledWith('promoteLabelModal.requestFinished', {
labelUrl: labelMockData.url,
successful: false,
});
})
.then(done)
.catch(done.fail);
return vm.onSubmit().catch((error) => {
expect(error).toBe(dummyError);
expect(eventHub.$emit).toHaveBeenCalledWith('promoteLabelModal.requestFinished', {
labelUrl: labelMockData.url,
successful: false,
});
});
});
});
});
......@@ -82,34 +82,39 @@ describe('getSuppressNetworkErrorsDuringNavigationLink', () => {
isNavigatingAway.mockReturnValue(false);
});
it('forwards successful requests', (done) => {
it('forwards successful requests', () => {
createSubscription(makeMockSuccessLink(), {
next({ data }) {
expect(data).toEqual({ foo: { id: 1 } });
},
error: () => done.fail('Should not happen'),
complete: () => done(),
error: () => {
throw new Error('Should not happen');
},
});
});
it('forwards GraphQL errors', (done) => {
it('forwards GraphQL errors', () => {
createSubscription(makeMockGraphQLErrorLink(), {
next({ errors }) {
expect(errors).toEqual([{ message: 'foo' }]);
},
error: () => done.fail('Should not happen'),
complete: () => done(),
error: () => {
throw new Error('Should not happen');
},
});
});
it('forwards network errors', (done) => {
it('forwards network errors', () => {
createSubscription(makeMockNetworkErrorLink(), {
next: () => done.fail('Should not happen'),
next: () => {
throw new Error('Should not happen');
},
error: (error) => {
expect(error.message).toBe('NetworkError');
done();
},
complete: () => done.fail('Should not happen'),
complete: () => {
throw new Error('Should not happen');
},
});
});
});
......@@ -119,23 +124,25 @@ describe('getSuppressNetworkErrorsDuringNavigationLink', () => {
isNavigatingAway.mockReturnValue(true);
});
it('forwards successful requests', (done) => {
it('forwards successful requests', () => {
createSubscription(makeMockSuccessLink(), {
next({ data }) {
expect(data).toEqual({ foo: { id: 1 } });
},
error: () => done.fail('Should not happen'),
complete: () => done(),
error: () => {
throw new Error('Should not happen');
},
});
});
it('forwards GraphQL errors', (done) => {
it('forwards GraphQL errors', () => {
createSubscription(makeMockGraphQLErrorLink(), {
next({ errors }) {
expect(errors).toEqual([{ message: 'foo' }]);
},
error: () => done.fail('Should not happen'),
complete: () => done(),
error: () => {
throw new Error('Should not happen');
},
});
});
});
......
......@@ -58,17 +58,16 @@ describe('StartupJSLink', () => {
link = ApolloLink.from([startupLink, new ApolloLink(() => Observable.of(FORWARDED_RESPONSE))]);
};
it('forwards requests if no calls are set up', (done) => {
it('forwards requests if no calls are set up', () => {
setupLink();
link.request(mockOperation()).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls).toBe(null);
expect(startupLink.request).toEqual(StartupJSLink.noopRequest);
done();
});
});
it('forwards requests if the operation is not pre-loaded', (done) => {
it('forwards requests if the operation is not pre-loaded', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -82,12 +81,11 @@ describe('StartupJSLink', () => {
link.request(mockOperation({ operationName: 'notLoaded' })).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(1);
done();
});
});
describe('variable match errors: ', () => {
it('forwards requests if the variables are not matching', (done) => {
it('forwards requests if the variables are not matching', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -101,11 +99,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation()).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('forwards requests if more variables are set in the operation', (done) => {
it('forwards requests if more variables are set in the operation', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -118,11 +115,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation()).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('forwards requests if less variables are set in the operation', (done) => {
it('forwards requests if less variables are set in the operation', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -136,11 +132,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation({ variables: { id: 3 } })).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('forwards requests if different variables are set', (done) => {
it('forwards requests if different variables are set', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -154,11 +149,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation({ variables: { id: 3 } })).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('forwards requests if array variables have a different order', (done) => {
it('forwards requests if array variables have a different order', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -172,13 +166,12 @@ describe('StartupJSLink', () => {
link.request(mockOperation({ variables: { id: [4, 3] } })).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
});
describe('error handling', () => {
it('forwards the call if the fetchCall is failing with a HTTP Error', (done) => {
it('forwards the call if the fetchCall is failing with a HTTP Error', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -192,11 +185,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation()).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('forwards the call if it errors (e.g. failing JSON)', (done) => {
it('forwards the call if it errors (e.g. failing JSON)', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -210,11 +202,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation()).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('forwards the call if the response contains an error', (done) => {
it('forwards the call if the response contains an error', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -228,11 +219,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation()).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it("forwards the call if the response doesn't contain a data object", (done) => {
it("forwards the call if the response doesn't contain a data object", () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -246,12 +236,11 @@ describe('StartupJSLink', () => {
link.request(mockOperation()).subscribe((result) => {
expect(result).toEqual(FORWARDED_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
});
it('resolves the request if the operation is matching', (done) => {
it('resolves the request if the operation is matching', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -265,11 +254,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation()).subscribe((result) => {
expect(result).toEqual(STARTUP_JS_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('resolves the request exactly once', (done) => {
it('resolves the request exactly once', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -285,12 +273,11 @@ describe('StartupJSLink', () => {
expect(startupLink.startupCalls.size).toBe(0);
link.request(mockOperation()).subscribe((result2) => {
expect(result2).toEqual(FORWARDED_RESPONSE);
done();
});
});
});
it('resolves the request if the variables have a different order', (done) => {
it('resolves the request if the variables have a different order', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -304,11 +291,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation({ variables: { name: 'foo', id: 3 } })).subscribe((result) => {
expect(result).toEqual(STARTUP_JS_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('resolves the request if the variables have undefined values', (done) => {
it('resolves the request if the variables have undefined values', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -324,11 +310,10 @@ describe('StartupJSLink', () => {
.subscribe((result) => {
expect(result).toEqual(STARTUP_JS_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('resolves the request if the variables are of an array format', (done) => {
it('resolves the request if the variables are of an array format', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -342,11 +327,10 @@ describe('StartupJSLink', () => {
link.request(mockOperation({ variables: { id: [3, 4] } })).subscribe((result) => {
expect(result).toEqual(STARTUP_JS_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
it('resolves multiple requests correctly', (done) => {
it('resolves multiple requests correctly', () => {
window.gl = {
startup_graphql_calls: [
{
......@@ -368,7 +352,6 @@ describe('StartupJSLink', () => {
link.request(mockOperation({ operationName: OPERATION_NAME })).subscribe((result2) => {
expect(result2).toEqual(STARTUP_JS_RESPONSE);
expect(startupLink.startupCalls.size).toBe(0);
done();
});
});
});
......
......@@ -266,15 +266,18 @@ describe('common_utils', () => {
});
describe('debounceByAnimationFrame', () => {
it('debounces a function to allow a maximum of one call per animation frame', (done) => {
it('debounces a function to allow a maximum of one call per animation frame', () => {
const spy = jest.fn();
const debouncedSpy = commonUtils.debounceByAnimationFrame(spy);
window.requestAnimationFrame(() => {
debouncedSpy();
debouncedSpy();
return new Promise((resolve) => {
window.requestAnimationFrame(() => {
expect(spy).toHaveBeenCalledTimes(1);
done();
debouncedSpy();
debouncedSpy();
window.requestAnimationFrame(() => {
expect(spy).toHaveBeenCalledTimes(1);
resolve();
});
});
});
});
......@@ -372,28 +375,24 @@ describe('common_utils', () => {
jest.spyOn(window, 'setTimeout');
});
it('solves the promise from the callback', (done) => {
it('solves the promise from the callback', () => {
const expectedResponseValue = 'Success!';
commonUtils
return commonUtils
.backOff((next, stop) =>
new Promise((resolve) => {
resolve(expectedResponseValue);
})
.then((resp) => {
stop(resp);
})
.catch(done.fail),
}).then((resp) => {
stop(resp);
}),
)
.then((respBackoff) => {
expect(respBackoff).toBe(expectedResponseValue);
done();
})
.catch(done.fail);
});
});
it('catches the rejected promise from the callback ', (done) => {
it('catches the rejected promise from the callback ', () => {
const errorMessage = 'Mistakes were made!';
commonUtils
return commonUtils
.backOff((next, stop) => {
new Promise((resolve, reject) => {
reject(new Error(errorMessage));
......@@ -406,39 +405,34 @@ describe('common_utils', () => {
.catch((errBackoffResp) => {
expect(errBackoffResp instanceof Error).toBe(true);
expect(errBackoffResp.message).toBe(errorMessage);
done();
});
});
it('solves the promise correctly after retrying a third time', (done) => {
it('solves the promise correctly after retrying a third time', () => {
let numberOfCalls = 1;
const expectedResponseValue = 'Success!';
commonUtils
return commonUtils
.backOff((next, stop) =>
Promise.resolve(expectedResponseValue)
.then((resp) => {
if (numberOfCalls < 3) {
numberOfCalls += 1;
next();
jest.runOnlyPendingTimers();
} else {
stop(resp);
}
})
.catch(done.fail),
Promise.resolve(expectedResponseValue).then((resp) => {
if (numberOfCalls < 3) {
numberOfCalls += 1;
next();
jest.runOnlyPendingTimers();
} else {
stop(resp);
}
}),
)
.then((respBackoff) => {
const timeouts = window.setTimeout.mock.calls.map(([, timeout]) => timeout);
expect(timeouts).toEqual([2000, 4000]);
expect(respBackoff).toBe(expectedResponseValue);
done();
})
.catch(done.fail);
});
});
it('rejects the backOff promise after timing out', (done) => {
commonUtils
it('rejects the backOff promise after timing out', () => {
return commonUtils
.backOff((next) => {
next();
jest.runOnlyPendingTimers();
......@@ -449,7 +443,6 @@ describe('common_utils', () => {
expect(timeouts).toEqual([2000, 4000, 8000, 16000, 32000, 32000]);
expect(errBackoffResp instanceof Error).toBe(true);
expect(errBackoffResp.message).toBe('BACKOFF_TIMEOUT');
done();
});
});
});
......
......@@ -50,58 +50,48 @@ describe('Poll', () => {
};
});
it('calls the success callback when no header for interval is provided', (done) => {
it('calls the success callback when no header for interval is provided', () => {
mockServiceCall({ status: 200 });
setup();
waitForAllCallsToFinish(1, () => {
return waitForAllCallsToFinish(1, () => {
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
done();
});
});
it('calls the error callback when the http request returns an error', (done) => {
it('calls the error callback when the http request returns an error', () => {
mockServiceCall({ status: 500 }, true);
setup();
waitForAllCallsToFinish(1, () => {
return waitForAllCallsToFinish(1, () => {
expect(callbacks.success).not.toHaveBeenCalled();
expect(callbacks.error).toHaveBeenCalled();
done();
});
});
it('skips the error callback when request is aborted', (done) => {
it('skips the error callback when request is aborted', () => {
mockServiceCall({ status: 0 }, true);
setup();
waitForAllCallsToFinish(1, () => {
return waitForAllCallsToFinish(1, () => {
expect(callbacks.success).not.toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
expect(callbacks.notification).toHaveBeenCalled();
done();
});
});
it('should call the success callback when the interval header is -1', (done) => {
it('should call the success callback when the interval header is -1', () => {
mockServiceCall({ status: 200, headers: { 'poll-interval': -1 } });
setup()
.then(() => {
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
done();
})
.catch(done.fail);
return setup().then(() => {
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
});
});
describe('for 2xx status code', () => {
successCodes.forEach((httpCode) => {
it(`starts polling when http status is ${httpCode} and interval header is provided`, (done) => {
it(`starts polling when http status is ${httpCode} and interval header is provided`, () => {
mockServiceCall({ status: httpCode, headers: { 'poll-interval': 1 } });
const Polling = new Poll({
......@@ -114,22 +104,20 @@ describe('Poll', () => {
Polling.makeRequest();
waitForAllCallsToFinish(2, () => {
return waitForAllCallsToFinish(2, () => {
Polling.stop();
expect(service.fetch.mock.calls).toHaveLength(2);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
done();
});
});
});
});
describe('with delayed initial request', () => {
it('delays the first request', async (done) => {
it('delays the first request', async () => {
mockServiceCall({ status: 200, headers: { 'poll-interval': 1 } });
const Polling = new Poll({
......@@ -144,21 +132,19 @@ describe('Poll', () => {
expect(Polling.timeoutID).toBeTruthy();
waitForAllCallsToFinish(2, () => {
return waitForAllCallsToFinish(2, () => {
Polling.stop();
expect(service.fetch.mock.calls).toHaveLength(2);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled();
done();
});
});
});
describe('stop', () => {
it('stops polling when method is called', (done) => {
it('stops polling when method is called', () => {
mockServiceCall({ status: 200, headers: { 'poll-interval': 1 } });
const Polling = new Poll({
......@@ -175,18 +161,16 @@ describe('Poll', () => {
Polling.makeRequest();
waitForAllCallsToFinish(1, () => {
return waitForAllCallsToFinish(1, () => {
expect(service.fetch.mock.calls).toHaveLength(1);
expect(service.fetch).toHaveBeenCalledWith({ page: 1 });
expect(Polling.stop).toHaveBeenCalled();
done();
});
});
});
describe('enable', () => {
it('should enable polling upon a response', (done) => {
it('should enable polling upon a response', () => {
mockServiceCall({ status: 200 });
const Polling = new Poll({
resource: service,
......@@ -200,19 +184,18 @@ describe('Poll', () => {
response: { status: 200, headers: { 'poll-interval': 1 } },
});
waitForAllCallsToFinish(1, () => {
return waitForAllCallsToFinish(1, () => {
Polling.stop();
expect(service.fetch.mock.calls).toHaveLength(1);
expect(service.fetch).toHaveBeenCalledWith({ page: 4 });
expect(Polling.options.data).toEqual({ page: 4 });
done();
});
});
});
describe('restart', () => {
it('should restart polling when its called', (done) => {
it('should restart polling when its called', () => {
mockServiceCall({ status: 200, headers: { 'poll-interval': 1 } });
const Polling = new Poll({
......@@ -238,7 +221,7 @@ describe('Poll', () => {
Polling.makeRequest();
waitForAllCallsToFinish(2, () => {
return waitForAllCallsToFinish(2, () => {
Polling.stop();
expect(service.fetch.mock.calls).toHaveLength(2);
......@@ -247,7 +230,6 @@ describe('Poll', () => {
expect(Polling.enable).toHaveBeenCalled();
expect(Polling.restart).toHaveBeenCalled();
expect(Polling.options.data).toEqual({ page: 4 });
done();
});
});
});
......
......@@ -93,7 +93,7 @@ describe('UsersCache', () => {
.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', async () => {
apiSpy = (query, options) => {
expect(query).toBe('');
expect(options).toEqual({
......@@ -105,16 +105,12 @@ describe('UsersCache', () => {
});
};
UsersCache.retrieve(dummyUsername)
.then((user) => {
expect(user).toBe(dummyUser);
expect(UsersCache.internalStorage[dummyUsername]).toBe(dummyUser);
})
.then(done)
.catch(done.fail);
const user = await UsersCache.retrieve(dummyUsername);
expect(user).toBe(dummyUser);
expect(UsersCache.internalStorage[dummyUsername]).toBe(dummyUser);
});
it('returns undefined if Ajax call fails and cache is empty', (done) => {
it('returns undefined if Ajax call fails and cache is empty', async () => {
const dummyError = new Error('server exploded');
apiSpy = (query, options) => {
......@@ -126,26 +122,18 @@ describe('UsersCache', () => {
return Promise.reject(dummyError);
};
UsersCache.retrieve(dummyUsername)
.then((user) => done.fail(`Received unexpected user: ${JSON.stringify(user)}`))
.catch((error) => {
expect(error).toBe(dummyError);
})
.then(done)
.catch(done.fail);
await expect(UsersCache.retrieve(dummyUsername)).rejects.toEqual(dummyError);
});
it('makes no Ajax call if matching data exists', (done) => {
it('makes no Ajax call if matching data exists', async () => {
UsersCache.internalStorage[dummyUsername] = dummyUser;
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
apiSpy = () => {
throw new Error('expected no Ajax call!');
};
UsersCache.retrieve(dummyUsername)
.then((user) => {
expect(user).toBe(dummyUser);
})
.then(done)
.catch(done.fail);
const user = await UsersCache.retrieve(dummyUsername);
expect(user).toBe(dummyUser);
});
});
......@@ -156,7 +144,7 @@ describe('UsersCache', () => {
jest.spyOn(UserApi, 'getUser').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', async () => {
apiSpy = (id) => {
expect(id).toBe(dummyUserId);
......@@ -165,16 +153,12 @@ describe('UsersCache', () => {
});
};
UsersCache.retrieveById(dummyUserId)
.then((user) => {
expect(user).toBe(dummyUser);
expect(UsersCache.internalStorage[dummyUserId]).toBe(dummyUser);
})
.then(done)
.catch(done.fail);
const user = await UsersCache.retrieveById(dummyUserId);
expect(user).toBe(dummyUser);
expect(UsersCache.internalStorage[dummyUserId]).toBe(dummyUser);
});
it('returns undefined if Ajax call fails and cache is empty', (done) => {
it('returns undefined if Ajax call fails and cache is empty', async () => {
const dummyError = new Error('server exploded');
apiSpy = (id) => {
......@@ -183,26 +167,18 @@ describe('UsersCache', () => {
return Promise.reject(dummyError);
};
UsersCache.retrieveById(dummyUserId)
.then((user) => done.fail(`Received unexpected user: ${JSON.stringify(user)}`))
.catch((error) => {
expect(error).toBe(dummyError);
})
.then(done)
.catch(done.fail);
await expect(UsersCache.retrieveById(dummyUserId)).rejects.toEqual(dummyError);
});
it('makes no Ajax call if matching data exists', (done) => {
it('makes no Ajax call if matching data exists', async () => {
UsersCache.internalStorage[dummyUserId] = dummyUser;
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
apiSpy = () => {
throw new Error('expected no Ajax call!');
};
UsersCache.retrieveById(dummyUserId)
.then((user) => {
expect(user).toBe(dummyUser);
})
.then(done)
.catch(done.fail);
const user = await UsersCache.retrieveById(dummyUserId);
expect(user).toBe(dummyUser);
});
});
......@@ -213,7 +189,7 @@ describe('UsersCache', () => {
jest.spyOn(UserApi, 'getUserStatus').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', async () => {
apiSpy = (id) => {
expect(id).toBe(dummyUserId);
......@@ -222,16 +198,12 @@ describe('UsersCache', () => {
});
};
UsersCache.retrieveStatusById(dummyUserId)
.then((userStatus) => {
expect(userStatus).toBe(dummyUserStatus);
expect(UsersCache.internalStorage[dummyUserId].status).toBe(dummyUserStatus);
})
.then(done)
.catch(done.fail);
const userStatus = await UsersCache.retrieveStatusById(dummyUserId);
expect(userStatus).toBe(dummyUserStatus);
expect(UsersCache.internalStorage[dummyUserId].status).toBe(dummyUserStatus);
});
it('returns undefined if Ajax call fails and cache is empty', (done) => {
it('returns undefined if Ajax call fails and cache is empty', async () => {
const dummyError = new Error('server exploded');
apiSpy = (id) => {
......@@ -240,28 +212,20 @@ describe('UsersCache', () => {
return Promise.reject(dummyError);
};
UsersCache.retrieveStatusById(dummyUserId)
.then((userStatus) => done.fail(`Received unexpected user: ${JSON.stringify(userStatus)}`))
.catch((error) => {
expect(error).toBe(dummyError);
})
.then(done)
.catch(done.fail);
await expect(UsersCache.retrieveStatusById(dummyUserId)).rejects.toEqual(dummyError);
});
it('makes no Ajax call if matching data exists', (done) => {
it('makes no Ajax call if matching data exists', async () => {
UsersCache.internalStorage[dummyUserId] = {
status: dummyUserStatus,
};
apiSpy = () => done.fail(new Error('expected no Ajax call!'));
apiSpy = () => {
throw new Error('expected no Ajax call!');
};
UsersCache.retrieveStatusById(dummyUserId)
.then((userStatus) => {
expect(userStatus).toBe(dummyUserStatus);
})
.then(done)
.catch(done.fail);
const userStatus = await UsersCache.retrieveStatusById(dummyUserId);
expect(userStatus).toBe(dummyUserStatus);
});
});
});
......@@ -34,9 +34,9 @@ describe('merge conflicts actions', () => {
describe('fetchConflictsData', () => {
const conflictsPath = 'conflicts/path/mock';
it('on success dispatches setConflictsData', (done) => {
it('on success dispatches setConflictsData', () => {
mock.onGet(conflictsPath).reply(200, {});
testAction(
return testAction(
actions.fetchConflictsData,
conflictsPath,
{},
......@@ -45,13 +45,12 @@ describe('merge conflicts actions', () => {
{ type: types.SET_LOADING_STATE, payload: false },
],
[{ type: 'setConflictsData', payload: {} }],
done,
);
});
it('when data has type equal to error ', (done) => {
it('when data has type equal to error ', () => {
mock.onGet(conflictsPath).reply(200, { type: 'error', message: 'error message' });
testAction(
return testAction(
actions.fetchConflictsData,
conflictsPath,
{},
......@@ -61,13 +60,12 @@ describe('merge conflicts actions', () => {
{ type: types.SET_LOADING_STATE, payload: false },
],
[],
done,
);
});
it('when request fails ', (done) => {
it('when request fails ', () => {
mock.onGet(conflictsPath).reply(400);
testAction(
return testAction(
actions.fetchConflictsData,
conflictsPath,
{},
......@@ -77,15 +75,14 @@ describe('merge conflicts actions', () => {
{ type: types.SET_LOADING_STATE, payload: false },
],
[],
done,
);
});
});
describe('setConflictsData', () => {
it('INTERACTIVE_RESOLVE_MODE updates the correct file ', (done) => {
it('INTERACTIVE_RESOLVE_MODE updates the correct file ', () => {
decorateFiles.mockReturnValue([{ bar: 'baz' }]);
testAction(
return testAction(
actions.setConflictsData,
{ files, foo: 'bar' },
{},
......@@ -96,7 +93,6 @@ describe('merge conflicts actions', () => {
},
],
[],
done,
);
});
});
......@@ -105,24 +101,21 @@ describe('merge conflicts actions', () => {
useMockLocationHelper();
const resolveConflictsPath = 'resolve/conflicts/path/mock';
it('on success reloads the page', (done) => {
it('on success reloads the page', async () => {
mock.onPost(resolveConflictsPath).reply(200, { redirect_to: 'hrefPath' });
testAction(
await testAction(
actions.submitResolvedConflicts,
resolveConflictsPath,
{},
[{ type: types.SET_SUBMIT_STATE, payload: true }],
[],
() => {
expect(window.location.assign).toHaveBeenCalledWith('hrefPath');
done();
},
);
expect(window.location.assign).toHaveBeenCalledWith('hrefPath');
});
it('on errors shows flash', (done) => {
it('on errors shows flash', async () => {
mock.onPost(resolveConflictsPath).reply(400);
testAction(
await testAction(
actions.submitResolvedConflicts,
resolveConflictsPath,
{},
......@@ -131,13 +124,10 @@ describe('merge conflicts actions', () => {
{ type: types.SET_SUBMIT_STATE, payload: false },
],
[],
() => {
expect(createFlash).toHaveBeenCalledWith({
message: 'Failed to save merge conflicts resolutions. Please try again!',
});
done();
},
);
expect(createFlash).toHaveBeenCalledWith({
message: 'Failed to save merge conflicts resolutions. Please try again!',
});
});
});
......@@ -193,9 +183,9 @@ describe('merge conflicts actions', () => {
});
describe('setViewType', () => {
it('commits the right mutation', (done) => {
it('commits the right mutation', async () => {
const payload = 'viewType';
testAction(
await testAction(
actions.setViewType,
payload,
{},
......@@ -206,14 +196,11 @@ describe('merge conflicts actions', () => {
},
],
[],
() => {
expect(Cookies.set).toHaveBeenCalledWith('diff_view', payload, {
expires: 365,
secure: false,
});
done();
},
);
expect(Cookies.set).toHaveBeenCalledWith('diff_view', payload, {
expires: 365,
secure: false,
});
});
});
......@@ -252,8 +239,8 @@ describe('merge conflicts actions', () => {
});
describe('setFileResolveMode', () => {
it('INTERACTIVE_RESOLVE_MODE updates the correct file ', (done) => {
testAction(
it('INTERACTIVE_RESOLVE_MODE updates the correct file ', () => {
return testAction(
actions.setFileResolveMode,
{ file: files[0], mode: INTERACTIVE_RESOLVE_MODE },
{ conflictsData: { files }, getFileIndex: () => 0 },
......@@ -267,11 +254,10 @@ describe('merge conflicts actions', () => {
},
],
[],
done,
);
});
it('EDIT_RESOLVE_MODE updates the correct file ', (done) => {
it('EDIT_RESOLVE_MODE updates the correct file ', async () => {
restoreFileLinesState.mockReturnValue([]);
const file = {
...files[0],
......@@ -280,7 +266,7 @@ describe('merge conflicts actions', () => {
resolutionData: {},
resolveMode: EDIT_RESOLVE_MODE,
};
testAction(
await testAction(
actions.setFileResolveMode,
{ file: files[0], mode: EDIT_RESOLVE_MODE },
{ conflictsData: { files }, getFileIndex: () => 0 },
......@@ -294,17 +280,14 @@ describe('merge conflicts actions', () => {
},
],
[],
() => {
expect(restoreFileLinesState).toHaveBeenCalledWith(file);
done();
},
);
expect(restoreFileLinesState).toHaveBeenCalledWith(file);
});
});
describe('setPromptConfirmationState', () => {
it('updates the correct file ', (done) => {
testAction(
it('updates the correct file ', () => {
return testAction(
actions.setPromptConfirmationState,
{ file: files[0], promptDiscardConfirmation: true },
{ conflictsData: { files }, getFileIndex: () => 0 },
......@@ -318,7 +301,6 @@ describe('merge conflicts actions', () => {
},
],
[],
done,
);
});
});
......@@ -333,11 +315,11 @@ describe('merge conflicts actions', () => {
],
};
it('updates the correct file ', (done) => {
it('updates the correct file ', async () => {
const marLikeMockReturn = { foo: 'bar' };
markLine.mockReturnValue(marLikeMockReturn);
testAction(
await testAction(
actions.handleSelected,
{ file, line: { id: 1, section: 'baz' } },
{ conflictsData: { files }, getFileIndex: () => 0 },
......@@ -359,11 +341,8 @@ describe('merge conflicts actions', () => {
},
],
[],
() => {
expect(markLine).toHaveBeenCalledTimes(3);
done();
},
);
expect(markLine).toHaveBeenCalledTimes(3);
});
});
});
......@@ -32,7 +32,7 @@ describe('delete_milestone_modal.vue', () => {
jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
});
it('deletes milestone and redirects to overview page', (done) => {
it('deletes milestone and redirects to overview page', async () => {
const responseURL = `${TEST_HOST}/delete_milestone_modal.vue/milestoneOverview`;
jest.spyOn(axios, 'delete').mockImplementation((url) => {
expect(url).toBe(props.milestoneUrl);
......@@ -48,19 +48,15 @@ describe('delete_milestone_modal.vue', () => {
});
});
vm.onSubmit()
.then(() => {
expect(redirectTo).toHaveBeenCalledWith(responseURL);
expect(eventHub.$emit).toHaveBeenCalledWith('deleteMilestoneModal.requestFinished', {
milestoneUrl: props.milestoneUrl,
successful: true,
});
})
.then(done)
.catch(done.fail);
await vm.onSubmit();
expect(redirectTo).toHaveBeenCalledWith(responseURL);
expect(eventHub.$emit).toHaveBeenCalledWith('deleteMilestoneModal.requestFinished', {
milestoneUrl: props.milestoneUrl,
successful: true,
});
});
it('displays error if deleting milestone failed', (done) => {
it('displays error if deleting milestone failed', async () => {
const dummyError = new Error('deleting milestone failed');
dummyError.response = { status: 418 };
jest.spyOn(axios, 'delete').mockImplementation((url) => {
......@@ -73,17 +69,12 @@ describe('delete_milestone_modal.vue', () => {
return Promise.reject(dummyError);
});
vm.onSubmit()
.catch((error) => {
expect(error).toBe(dummyError);
expect(redirectTo).not.toHaveBeenCalled();
expect(eventHub.$emit).toHaveBeenCalledWith('deleteMilestoneModal.requestFinished', {
milestoneUrl: props.milestoneUrl,
successful: false,
});
})
.then(done)
.catch(done.fail);
await expect(vm.onSubmit()).rejects.toEqual(dummyError);
expect(redirectTo).not.toHaveBeenCalled();
expect(eventHub.$emit).toHaveBeenCalledWith('deleteMilestoneModal.requestFinished', {
milestoneUrl: props.milestoneUrl,
successful: false,
});
});
});
......
......@@ -109,7 +109,7 @@ describe('Actions menu', () => {
describe('adding new metric from modal', () => {
let origPage;
beforeEach((done) => {
beforeEach(() => {
jest.spyOn(Tracking, 'event').mockReturnValue();
createShallowWrapper();
......@@ -118,7 +118,7 @@ describe('Actions menu', () => {
origPage = document.body.dataset.page;
document.body.dataset.page = 'projects:environments:metrics';
nextTick(done);
return nextTick();
});
afterEach(() => {
......
......@@ -263,7 +263,7 @@ describe('NoteHeader component', () => {
});
describe('when author username link is hovered', () => {
it('toggles hover specific CSS classes on author name link', (done) => {
it('toggles hover specific CSS classes on author name link', async () => {
createComponent({ author });
const authorUsernameLink = wrapper.find({ ref: 'authorUsernameLink' });
......@@ -271,19 +271,15 @@ describe('NoteHeader component', () => {
authorUsernameLink.trigger('mouseenter');
nextTick(() => {
expect(authorNameLink.classes()).toContain('hover');
expect(authorNameLink.classes()).toContain('text-underline');
await nextTick();
expect(authorNameLink.classes()).toContain('hover');
expect(authorNameLink.classes()).toContain('text-underline');
authorUsernameLink.trigger('mouseleave');
authorUsernameLink.trigger('mouseleave');
nextTick(() => {
expect(authorNameLink.classes()).not.toContain('hover');
expect(authorNameLink.classes()).not.toContain('text-underline');
done();
});
});
await nextTick();
expect(authorNameLink.classes()).not.toContain('hover');
expect(authorNameLink.classes()).not.toContain('text-underline');
});
});
......
......@@ -561,7 +561,7 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => {
});
describe('postComment', () => {
it('disables the submit button', (done) => {
it('disables the submit button', async () => {
const $submitButton = $form.find('.js-comment-submit-button');
expect($submitButton).not.toBeDisabled();
......@@ -574,13 +574,8 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => {
return [200, note];
});
notes
.postComment(dummyEvent)
.then(() => {
expect($submitButton).not.toBeDisabled();
})
.then(done)
.catch(done.fail);
await notes.postComment(dummyEvent);
expect($submitButton).not.toBeDisabled();
});
});
......
This diff is collapsed.
......@@ -20,10 +20,10 @@ jest.mock('~/api.js');
describe('Actions Package details store', () => {
describe('fetchPackageVersions', () => {
it('should fetch the package versions', (done) => {
it('should fetch the package versions', async () => {
Api.projectPackage = jest.fn().mockResolvedValue({ data: packageEntity });
testAction(
await testAction(
fetchPackageVersions,
undefined,
{ packageEntity },
......@@ -33,20 +33,14 @@ describe('Actions Package details store', () => {
{ type: types.SET_LOADING, payload: false },
],
[],
() => {
expect(Api.projectPackage).toHaveBeenCalledWith(
packageEntity.project_id,
packageEntity.id,
);
done();
},
);
expect(Api.projectPackage).toHaveBeenCalledWith(packageEntity.project_id, packageEntity.id);
});
it("does not set the versions if they don't exist", (done) => {
it("does not set the versions if they don't exist", async () => {
Api.projectPackage = jest.fn().mockResolvedValue({ data: { packageEntity, versions: null } });
testAction(
await testAction(
fetchPackageVersions,
undefined,
{ packageEntity },
......@@ -55,20 +49,14 @@ describe('Actions Package details store', () => {
{ type: types.SET_LOADING, payload: false },
],
[],
() => {
expect(Api.projectPackage).toHaveBeenCalledWith(
packageEntity.project_id,
packageEntity.id,
);
done();
},
);
expect(Api.projectPackage).toHaveBeenCalledWith(packageEntity.project_id, packageEntity.id);
});
it('should create flash on API error', (done) => {
it('should create flash on API error', async () => {
Api.projectPackage = jest.fn().mockRejectedValue();
testAction(
await testAction(
fetchPackageVersions,
undefined,
{ packageEntity },
......@@ -77,41 +65,31 @@ describe('Actions Package details store', () => {
{ type: types.SET_LOADING, payload: false },
],
[],
() => {
expect(Api.projectPackage).toHaveBeenCalledWith(
packageEntity.project_id,
packageEntity.id,
);
expect(createFlash).toHaveBeenCalledWith({
message: FETCH_PACKAGE_VERSIONS_ERROR,
type: 'warning',
});
done();
},
);
expect(Api.projectPackage).toHaveBeenCalledWith(packageEntity.project_id, packageEntity.id);
expect(createFlash).toHaveBeenCalledWith({
message: FETCH_PACKAGE_VERSIONS_ERROR,
type: 'warning',
});
});
});
describe('deletePackage', () => {
it('should call Api.deleteProjectPackage', (done) => {
it('should call Api.deleteProjectPackage', async () => {
Api.deleteProjectPackage = jest.fn().mockResolvedValue();
testAction(deletePackage, undefined, { packageEntity }, [], [], () => {
expect(Api.deleteProjectPackage).toHaveBeenCalledWith(
packageEntity.project_id,
packageEntity.id,
);
done();
});
await testAction(deletePackage, undefined, { packageEntity }, [], []);
expect(Api.deleteProjectPackage).toHaveBeenCalledWith(
packageEntity.project_id,
packageEntity.id,
);
});
it('should create flash on API error', (done) => {
it('should create flash on API error', async () => {
Api.deleteProjectPackage = jest.fn().mockRejectedValue();
testAction(deletePackage, undefined, { packageEntity }, [], [], () => {
expect(createFlash).toHaveBeenCalledWith({
message: DELETE_PACKAGE_ERROR_MESSAGE,
type: 'warning',
});
done();
await testAction(deletePackage, undefined, { packageEntity }, [], []);
expect(createFlash).toHaveBeenCalledWith({
message: DELETE_PACKAGE_ERROR_MESSAGE,
type: 'warning',
});
});
});
......@@ -119,37 +97,33 @@ describe('Actions Package details store', () => {
describe('deletePackageFile', () => {
const fileId = 'a_file_id';
it('should call Api.deleteProjectPackageFile and commit the right data', (done) => {
it('should call Api.deleteProjectPackageFile and commit the right data', async () => {
const packageFiles = [{ id: 'foo' }, { id: fileId }];
Api.deleteProjectPackageFile = jest.fn().mockResolvedValue();
testAction(
await testAction(
deletePackageFile,
fileId,
{ packageEntity, packageFiles },
[{ type: types.UPDATE_PACKAGE_FILES, payload: [{ id: 'foo' }] }],
[],
() => {
expect(Api.deleteProjectPackageFile).toHaveBeenCalledWith(
packageEntity.project_id,
packageEntity.id,
fileId,
);
expect(createFlash).toHaveBeenCalledWith({
message: DELETE_PACKAGE_FILE_SUCCESS_MESSAGE,
type: 'success',
});
done();
},
);
expect(Api.deleteProjectPackageFile).toHaveBeenCalledWith(
packageEntity.project_id,
packageEntity.id,
fileId,
);
expect(createFlash).toHaveBeenCalledWith({
message: DELETE_PACKAGE_FILE_SUCCESS_MESSAGE,
type: 'success',
});
});
it('should create flash on API error', (done) => {
it('should create flash on API error', async () => {
Api.deleteProjectPackageFile = jest.fn().mockRejectedValue();
testAction(deletePackageFile, fileId, { packageEntity }, [], [], () => {
expect(createFlash).toHaveBeenCalledWith({
message: DELETE_PACKAGE_FILE_ERROR_MESSAGE,
type: 'warning',
});
done();
await testAction(deletePackageFile, fileId, { packageEntity }, [], []);
expect(createFlash).toHaveBeenCalledWith({
message: DELETE_PACKAGE_FILE_ERROR_MESSAGE,
type: 'warning',
});
});
});
......
......@@ -32,8 +32,8 @@ describe('Actions Package list store', () => {
};
const filter = [];
it('should fetch the project packages list when isGroupPage is false', (done) => {
testAction(
it('should fetch the project packages list when isGroupPage is false', async () => {
await testAction(
actions.requestPackagesList,
undefined,
{ config: { isGroupPage: false, resourceId: 1 }, sorting, filter },
......@@ -43,17 +43,14 @@ describe('Actions Package list store', () => {
{ type: 'receivePackagesListSuccess', payload: { data: 'foo', headers } },
{ type: 'setLoading', payload: false },
],
() => {
expect(Api.projectPackages).toHaveBeenCalledWith(1, {
params: { page: 1, per_page: 20, sort: sorting.sort, order_by: sorting.orderBy },
});
done();
},
);
expect(Api.projectPackages).toHaveBeenCalledWith(1, {
params: { page: 1, per_page: 20, sort: sorting.sort, order_by: sorting.orderBy },
});
});
it('should fetch the group packages list when isGroupPage is true', (done) => {
testAction(
it('should fetch the group packages list when isGroupPage is true', async () => {
await testAction(
actions.requestPackagesList,
undefined,
{ config: { isGroupPage: true, resourceId: 2 }, sorting, filter },
......@@ -63,19 +60,16 @@ describe('Actions Package list store', () => {
{ type: 'receivePackagesListSuccess', payload: { data: 'baz', headers } },
{ type: 'setLoading', payload: false },
],
() => {
expect(Api.groupPackages).toHaveBeenCalledWith(2, {
params: { page: 1, per_page: 20, sort: sorting.sort, order_by: sorting.orderBy },
});
done();
},
);
expect(Api.groupPackages).toHaveBeenCalledWith(2, {
params: { page: 1, per_page: 20, sort: sorting.sort, order_by: sorting.orderBy },
});
});
it('should fetch packages of a certain type when a filter with a type is present', (done) => {
it('should fetch packages of a certain type when a filter with a type is present', async () => {
const packageType = 'maven';
testAction(
await testAction(
actions.requestPackagesList,
undefined,
{
......@@ -89,24 +83,21 @@ describe('Actions Package list store', () => {
{ type: 'receivePackagesListSuccess', payload: { data: 'foo', headers } },
{ type: 'setLoading', payload: false },
],
() => {
expect(Api.projectPackages).toHaveBeenCalledWith(1, {
params: {
page: 1,
per_page: 20,
sort: sorting.sort,
order_by: sorting.orderBy,
package_type: packageType,
},
});
done();
},
);
expect(Api.projectPackages).toHaveBeenCalledWith(1, {
params: {
page: 1,
per_page: 20,
sort: sorting.sort,
order_by: sorting.orderBy,
package_type: packageType,
},
});
});
it('should create flash on API error', (done) => {
it('should create flash on API error', async () => {
Api.projectPackages = jest.fn().mockRejectedValue();
testAction(
await testAction(
actions.requestPackagesList,
undefined,
{ config: { isGroupPage: false, resourceId: 2 }, sorting, filter },
......@@ -115,15 +106,12 @@ describe('Actions Package list store', () => {
{ type: 'setLoading', payload: true },
{ type: 'setLoading', payload: false },
],
() => {
expect(createFlash).toHaveBeenCalled();
done();
},
);
expect(createFlash).toHaveBeenCalled();
});
it('should force the terraform_module type when forceTerraform is true', (done) => {
testAction(
it('should force the terraform_module type when forceTerraform is true', async () => {
await testAction(
actions.requestPackagesList,
undefined,
{ config: { isGroupPage: false, resourceId: 1, forceTerraform: true }, sorting, filter },
......@@ -133,27 +121,24 @@ describe('Actions Package list store', () => {
{ type: 'receivePackagesListSuccess', payload: { data: 'foo', headers } },
{ type: 'setLoading', payload: false },
],
() => {
expect(Api.projectPackages).toHaveBeenCalledWith(1, {
params: {
page: 1,
per_page: 20,
sort: sorting.sort,
order_by: sorting.orderBy,
package_type: 'terraform_module',
},
});
done();
},
);
expect(Api.projectPackages).toHaveBeenCalledWith(1, {
params: {
page: 1,
per_page: 20,
sort: sorting.sort,
order_by: sorting.orderBy,
package_type: 'terraform_module',
},
});
});
});
describe('receivePackagesListSuccess', () => {
it('should set received packages', (done) => {
it('should set received packages', () => {
const data = 'foo';
testAction(
return testAction(
actions.receivePackagesListSuccess,
{ data, headers },
null,
......@@ -162,33 +147,30 @@ describe('Actions Package list store', () => {
{ type: types.SET_PAGINATION, payload: headers },
],
[],
done,
);
});
});
describe('setInitialState', () => {
it('should commit setInitialState', (done) => {
testAction(
it('should commit setInitialState', () => {
return testAction(
actions.setInitialState,
'1',
null,
[{ type: types.SET_INITIAL_STATE, payload: '1' }],
[],
done,
);
});
});
describe('setLoading', () => {
it('should commit set main loading', (done) => {
testAction(
it('should commit set main loading', () => {
return testAction(
actions.setLoading,
true,
null,
[{ type: types.SET_MAIN_LOADING, payload: true }],
[],
done,
);
});
});
......@@ -199,11 +181,11 @@ describe('Actions Package list store', () => {
delete_api_path: 'foo',
},
};
it('should perform a delete operation on _links.delete_api_path', (done) => {
it('should perform a delete operation on _links.delete_api_path', () => {
mock.onDelete(payload._links.delete_api_path).replyOnce(200);
Api.projectPackages = jest.fn().mockResolvedValue({ data: 'foo' });
testAction(
return testAction(
actions.requestDeletePackage,
payload,
{ pagination: { page: 1 } },
......@@ -212,13 +194,12 @@ describe('Actions Package list store', () => {
{ type: 'setLoading', payload: true },
{ type: 'requestPackagesList', payload: { page: 1 } },
],
done,
);
});
it('should stop the loading and call create flash on api error', (done) => {
it('should stop the loading and call create flash on api error', async () => {
mock.onDelete(payload._links.delete_api_path).replyOnce(400);
testAction(
await testAction(
actions.requestDeletePackage,
payload,
null,
......@@ -227,50 +208,44 @@ describe('Actions Package list store', () => {
{ type: 'setLoading', payload: true },
{ type: 'setLoading', payload: false },
],
() => {
expect(createFlash).toHaveBeenCalled();
done();
},
);
expect(createFlash).toHaveBeenCalled();
});
it.each`
property | actionPayload
${'_links'} | ${{}}
${'delete_api_path'} | ${{ _links: {} }}
`('should reject and createFlash when $property is missing', ({ actionPayload }, done) => {
testAction(actions.requestDeletePackage, actionPayload, null, [], []).catch((e) => {
`('should reject and createFlash when $property is missing', ({ actionPayload }) => {
return testAction(actions.requestDeletePackage, actionPayload, null, [], []).catch((e) => {
expect(e).toEqual(new Error(MISSING_DELETE_PATH_ERROR));
expect(createFlash).toHaveBeenCalledWith({
message: DELETE_PACKAGE_ERROR_MESSAGE,
});
done();
});
});
});
describe('setSorting', () => {
it('should commit SET_SORTING', (done) => {
testAction(
it('should commit SET_SORTING', () => {
return testAction(
actions.setSorting,
'foo',
null,
[{ type: types.SET_SORTING, payload: 'foo' }],
[],
done,
);
});
});
describe('setFilter', () => {
it('should commit SET_FILTER', (done) => {
testAction(
it('should commit SET_FILTER', () => {
return testAction(
actions.setFilter,
'foo',
null,
[{ type: types.SET_FILTER, payload: 'foo' }],
[],
done,
);
});
});
......
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