Commit 97ab838b authored by Himanshu Kapoor's avatar Himanshu Kapoor

Migrate spec/javascripts/releases to Jest

Migrate all tests in spec/javascripts/releases to Jest.
parent 59122560
import { range as rge } from 'lodash'; import { range as rge } from 'lodash';
import Vue from 'vue'; import Vue from 'vue';
import { mountComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { mountComponentWithStore } from 'helpers/vue_mount_component_helper';
import app from '~/releases/components/app_index.vue'; import app from '~/releases/components/app_index.vue';
import createStore from '~/releases/stores'; import createStore from '~/releases/stores';
import listModule from '~/releases/stores/modules/list'; import listModule from '~/releases/stores/modules/list';
...@@ -9,11 +9,11 @@ import { resetStore } from '../stores/modules/list/helpers'; ...@@ -9,11 +9,11 @@ import { resetStore } from '../stores/modules/list/helpers';
import { import {
pageInfoHeadersWithoutPagination, pageInfoHeadersWithoutPagination,
pageInfoHeadersWithPagination, pageInfoHeadersWithPagination,
release, release2 as release,
releases, releases,
} from '../mock_data'; } from '../mock_data';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils'; import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import waitForPromises from 'spec/helpers/wait_for_promises'; import waitForPromises from 'helpers/wait_for_promises';
describe('Releases App ', () => { describe('Releases App ', () => {
const Component = Vue.extend(app); const Component = Vue.extend(app);
...@@ -42,83 +42,67 @@ describe('Releases App ', () => { ...@@ -42,83 +42,67 @@ describe('Releases App ', () => {
describe('while loading', () => { describe('while loading', () => {
beforeEach(() => { beforeEach(() => {
spyOn(api, 'releases').and.returnValue(Promise.resolve({ data: [], headers: {} })); jest
.spyOn(api, 'releases')
// Need to defer the return value here to the next stack,
// otherwise the loading state disappears before our test even starts.
.mockImplementation(() => waitForPromises().then(() => ({ data: [], headers: {} })));
vm = mountComponentWithStore(Component, { props, store }); vm = mountComponentWithStore(Component, { props, store });
}); });
it('renders loading icon', done => { it('renders loading icon', () => {
expect(vm.$el.querySelector('.js-loading')).not.toBeNull(); expect(vm.$el.querySelector('.js-loading')).not.toBeNull();
expect(vm.$el.querySelector('.js-empty-state')).toBeNull(); expect(vm.$el.querySelector('.js-empty-state')).toBeNull();
expect(vm.$el.querySelector('.js-success-state')).toBeNull(); expect(vm.$el.querySelector('.js-success-state')).toBeNull();
expect(vm.$el.querySelector('.gl-pagination')).toBeNull(); expect(vm.$el.querySelector('.gl-pagination')).toBeNull();
waitForPromises() return waitForPromises();
.then(done)
.catch(done.fail);
}); });
}); });
describe('with successful request', () => { describe('with successful request', () => {
beforeEach(() => { beforeEach(() => {
spyOn(api, 'releases').and.returnValue( jest
Promise.resolve({ data: releases, headers: pageInfoHeadersWithoutPagination }), .spyOn(api, 'releases')
); .mockResolvedValue({ data: releases, headers: pageInfoHeadersWithoutPagination });
vm = mountComponentWithStore(Component, { props, store }); vm = mountComponentWithStore(Component, { props, store });
}); });
it('renders success state', done => { it('renders success state', () => {
waitForPromises() expect(vm.$el.querySelector('.js-loading')).toBeNull();
.then(() => { expect(vm.$el.querySelector('.js-empty-state')).toBeNull();
expect(vm.$el.querySelector('.js-loading')).toBeNull(); expect(vm.$el.querySelector('.js-success-state')).not.toBeNull();
expect(vm.$el.querySelector('.js-empty-state')).toBeNull(); expect(vm.$el.querySelector('.gl-pagination')).toBeNull();
expect(vm.$el.querySelector('.js-success-state')).not.toBeNull();
expect(vm.$el.querySelector('.gl-pagination')).toBeNull();
done();
})
.catch(done.fail);
}); });
}); });
describe('with successful request and pagination', () => { describe('with successful request and pagination', () => {
beforeEach(() => { beforeEach(() => {
spyOn(api, 'releases').and.returnValue( jest
Promise.resolve({ data: releasesPagination, headers: pageInfoHeadersWithPagination }), .spyOn(api, 'releases')
); .mockResolvedValue({ data: releasesPagination, headers: pageInfoHeadersWithPagination });
vm = mountComponentWithStore(Component, { props, store }); vm = mountComponentWithStore(Component, { props, store });
}); });
it('renders success state', done => { it('renders success state', () => {
waitForPromises() expect(vm.$el.querySelector('.js-loading')).toBeNull();
.then(() => { expect(vm.$el.querySelector('.js-empty-state')).toBeNull();
expect(vm.$el.querySelector('.js-loading')).toBeNull(); expect(vm.$el.querySelector('.js-success-state')).not.toBeNull();
expect(vm.$el.querySelector('.js-empty-state')).toBeNull(); expect(vm.$el.querySelector('.gl-pagination')).not.toBeNull();
expect(vm.$el.querySelector('.js-success-state')).not.toBeNull();
expect(vm.$el.querySelector('.gl-pagination')).not.toBeNull();
done();
})
.catch(done.fail);
}); });
}); });
describe('with empty request', () => { describe('with empty request', () => {
beforeEach(() => { beforeEach(() => {
spyOn(api, 'releases').and.returnValue(Promise.resolve({ data: [], headers: {} })); jest.spyOn(api, 'releases').mockResolvedValue({ data: [], headers: {} });
vm = mountComponentWithStore(Component, { props, store }); vm = mountComponentWithStore(Component, { props, store });
}); });
it('renders empty state', done => { it('renders empty state', () => {
waitForPromises() expect(vm.$el.querySelector('.js-loading')).toBeNull();
.then(() => { expect(vm.$el.querySelector('.js-empty-state')).not.toBeNull();
expect(vm.$el.querySelector('.js-loading')).toBeNull(); expect(vm.$el.querySelector('.js-success-state')).toBeNull();
expect(vm.$el.querySelector('.js-empty-state')).not.toBeNull(); expect(vm.$el.querySelector('.gl-pagination')).toBeNull();
expect(vm.$el.querySelector('.js-success-state')).toBeNull();
expect(vm.$el.querySelector('.gl-pagination')).toBeNull();
done();
})
.catch(done.fail);
}); });
}); });
...@@ -126,7 +110,7 @@ describe('Releases App ', () => { ...@@ -126,7 +110,7 @@ describe('Releases App ', () => {
const findNewReleaseButton = () => vm.$el.querySelector('.js-new-release-btn'); const findNewReleaseButton = () => vm.$el.querySelector('.js-new-release-btn');
beforeEach(() => { beforeEach(() => {
spyOn(api, 'releases').and.returnValue(Promise.resolve({ data: [], headers: {} })); jest.spyOn(api, 'releases').mockResolvedValue({ data: [], headers: {} });
}); });
const factory = additionalProps => { const factory = additionalProps => {
...@@ -146,38 +130,20 @@ describe('Releases App ', () => { ...@@ -146,38 +130,20 @@ describe('Releases App ', () => {
factory({ newReleasePath }); factory({ newReleasePath });
}); });
it('renders the "New release" button', done => { it('renders the "New release" button', () => {
waitForPromises() expect(findNewReleaseButton()).not.toBeNull();
.then(() => {
expect(findNewReleaseButton()).not.toBeNull();
done();
})
.catch(done.fail);
}); });
it('renders the "New release" button with the correct href', done => { it('renders the "New release" button with the correct href', () => {
waitForPromises() expect(findNewReleaseButton().getAttribute('href')).toBe(newReleasePath);
.then(() => {
expect(findNewReleaseButton().getAttribute('href')).toBe(newReleasePath);
done();
})
.catch(done.fail);
}); });
}); });
describe('when the user is not allowed to create a new Release', () => { describe('when the user is not allowed to create a new Release', () => {
beforeEach(() => factory()); beforeEach(() => factory());
it('does not render the "New release" button', done => { it('does not render the "New release" button', () => {
waitForPromises() expect(findNewReleaseButton()).toBeNull();
.then(() => {
expect(findNewReleaseButton()).toBeNull();
done();
})
.catch(done.fail);
}); });
}); });
}); });
......
...@@ -131,3 +131,91 @@ export const release = { ...@@ -131,3 +131,91 @@ export const release = {
edit_url: 'http://0.0.0.0:3001/root/release-test/-/releases/v0.3/edit', edit_url: 'http://0.0.0.0:3001/root/release-test/-/releases/v0.3/edit',
}, },
}; };
export const pageInfoHeadersWithoutPagination = {
'X-NEXT-PAGE': '',
'X-PAGE': '1',
'X-PER-PAGE': '20',
'X-PREV-PAGE': '',
'X-TOTAL': '19',
'X-TOTAL-PAGES': '1',
};
export const pageInfoHeadersWithPagination = {
'X-NEXT-PAGE': '2',
'X-PAGE': '1',
'X-PER-PAGE': '20',
'X-PREV-PAGE': '',
'X-TOTAL': '21',
'X-TOTAL-PAGES': '2',
};
export const release2 = {
name: 'Bionic Beaver',
tag_name: '18.04',
description: '## changelog\n\n* line 1\n* line2',
description_html: '<div><h2>changelog</h2><ul><li>line1</li<li>line 2</li></ul></div>',
author_name: 'Release bot',
author_email: 'release-bot@example.com',
created_at: '2012-05-28T05:00:00-07:00',
commit: {
id: '2695effb5807a22ff3d138d593fd856244e155e7',
short_id: '2695effb',
title: 'Initial commit',
created_at: '2017-07-26T11:08:53.000+02:00',
parent_ids: ['2a4b78934375d7f53875269ffd4f45fd83a84ebe'],
message: 'Initial commit',
author: {
avatar_url: 'uploads/-/system/user/avatar/johndoe/avatar.png',
id: 482476,
name: 'John Doe',
path: '/johndoe',
state: 'active',
status_tooltip_html: null,
username: 'johndoe',
web_url: 'https://gitlab.com/johndoe',
},
authored_date: '2012-05-28T04:42:42-07:00',
committer_name: 'Jack Smith',
committer_email: 'jack@example.com',
committed_date: '2012-05-28T04:42:42-07:00',
},
assets: {
count: 6,
sources: [
{
format: 'zip',
url: 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.zip',
},
{
format: 'tar.gz',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar.gz',
},
{
format: 'tar.bz2',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar.bz2',
},
{
format: 'tar',
url: 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar',
},
],
links: [
{
name: 'release-18.04.dmg',
url: 'https://my-external-hosting.example.com/scrambled-url/',
external: true,
},
{
name: 'binary-linux-amd64',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/artifacts/v11.6.0-rc4/download?job=rspec-mysql+41%2F50',
external: false,
},
],
},
};
export const releases = [release, release2];
import testAction from 'spec/helpers/vuex_action_helper'; import testAction from 'helpers/vuex_action_helper';
import { import {
requestReleases, requestReleases,
fetchReleases, fetchReleases,
...@@ -31,7 +31,7 @@ describe('Releases State actions', () => { ...@@ -31,7 +31,7 @@ describe('Releases State actions', () => {
describe('fetchReleases', () => { describe('fetchReleases', () => {
describe('success', () => { describe('success', () => {
it('dispatches requestReleases and receiveReleasesSuccess', done => { it('dispatches requestReleases and receiveReleasesSuccess', done => {
spyOn(api, 'releases').and.callFake((id, options) => { jest.spyOn(api, 'releases').mockImplementation((id, options) => {
expect(id).toEqual(1); expect(id).toEqual(1);
expect(options.page).toEqual('1'); expect(options.page).toEqual('1');
return Promise.resolve({ data: releases, headers: pageInfoHeadersWithoutPagination }); return Promise.resolve({ data: releases, headers: pageInfoHeadersWithoutPagination });
...@@ -56,7 +56,7 @@ describe('Releases State actions', () => { ...@@ -56,7 +56,7 @@ describe('Releases State actions', () => {
}); });
it('dispatches requestReleases and receiveReleasesSuccess on page two', done => { it('dispatches requestReleases and receiveReleasesSuccess on page two', done => {
spyOn(api, 'releases').and.callFake((_, options) => { jest.spyOn(api, 'releases').mockImplementation((_, options) => {
expect(options.page).toEqual('2'); expect(options.page).toEqual('2');
return Promise.resolve({ data: releases, headers: pageInfoHeadersWithoutPagination }); return Promise.resolve({ data: releases, headers: pageInfoHeadersWithoutPagination });
}); });
...@@ -82,7 +82,7 @@ describe('Releases State actions', () => { ...@@ -82,7 +82,7 @@ describe('Releases State actions', () => {
describe('error', () => { describe('error', () => {
it('dispatches requestReleases and receiveReleasesError', done => { it('dispatches requestReleases and receiveReleasesError', done => {
spyOn(api, 'releases').and.returnValue(Promise.reject()); jest.spyOn(api, 'releases').mockReturnValue(Promise.reject());
testAction( testAction(
fetchReleases, fetchReleases,
......
export const pageInfoHeadersWithoutPagination = {
'X-NEXT-PAGE': '',
'X-PAGE': '1',
'X-PER-PAGE': '20',
'X-PREV-PAGE': '',
'X-TOTAL': '19',
'X-TOTAL-PAGES': '1',
};
export const pageInfoHeadersWithPagination = {
'X-NEXT-PAGE': '2',
'X-PAGE': '1',
'X-PER-PAGE': '20',
'X-PREV-PAGE': '',
'X-TOTAL': '21',
'X-TOTAL-PAGES': '2',
};
export const release = {
name: 'Bionic Beaver',
tag_name: '18.04',
description: '## changelog\n\n* line 1\n* line2',
description_html: '<div><h2>changelog</h2><ul><li>line1</li<li>line 2</li></ul></div>',
author_name: 'Release bot',
author_email: 'release-bot@example.com',
created_at: '2012-05-28T05:00:00-07:00',
commit: {
id: '2695effb5807a22ff3d138d593fd856244e155e7',
short_id: '2695effb',
title: 'Initial commit',
created_at: '2017-07-26T11:08:53.000+02:00',
parent_ids: ['2a4b78934375d7f53875269ffd4f45fd83a84ebe'],
message: 'Initial commit',
author: {
avatar_url: 'uploads/-/system/user/avatar/johndoe/avatar.png',
id: 482476,
name: 'John Doe',
path: '/johndoe',
state: 'active',
status_tooltip_html: null,
username: 'johndoe',
web_url: 'https://gitlab.com/johndoe',
},
authored_date: '2012-05-28T04:42:42-07:00',
committer_name: 'Jack Smith',
committer_email: 'jack@example.com',
committed_date: '2012-05-28T04:42:42-07:00',
},
assets: {
count: 6,
sources: [
{
format: 'zip',
url: 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.zip',
},
{
format: 'tar.gz',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar.gz',
},
{
format: 'tar.bz2',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar.bz2',
},
{
format: 'tar',
url: 'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar',
},
],
links: [
{
name: 'release-18.04.dmg',
url: 'https://my-external-hosting.example.com/scrambled-url/',
external: true,
},
{
name: 'binary-linux-amd64',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/artifacts/v11.6.0-rc4/download?job=rspec-mysql+41%2F50',
external: false,
},
],
},
};
export const releases = [
release,
{
name: 'JoJos Bizarre Adventure',
tag_name: '19.00',
description: '## changelog\n\n* line 1\n* line2',
description_html: '<div><h2>changelog</h2><ul><li>line1</li<li>line 2</li></ul></div>',
author_name: 'Release bot',
author_email: 'release-bot@example.com',
created_at: '2012-05-28T05:00:00-07:00',
commit: {
id: '2695effb5807a22ff3d138d593fd856244e155e7',
short_id: '2695effb',
title: 'Initial commit',
created_at: '2017-07-26T11:08:53.000+02:00',
parent_ids: ['2a4b78934375d7f53875269ffd4f45fd83a84ebe'],
message: 'Initial commit',
author: {
avatar_url: 'uploads/-/system/user/avatar/johndoe/avatar.png',
id: 482476,
name: 'John Doe',
path: '/johndoe',
state: 'active',
status_tooltip_html: null,
username: 'johndoe',
web_url: 'https://gitlab.com/johndoe',
},
authored_date: '2012-05-28T04:42:42-07:00',
committer_name: 'Jack Smith',
committer_email: 'jack@example.com',
committed_date: '2012-05-28T04:42:42-07:00',
},
assets: {
count: 4,
sources: [
{
format: 'tar.gz',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar.gz',
},
{
format: 'tar.bz2',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar.bz2',
},
{
format: 'tar',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/archive/v11.3.12/gitlab-ce-v11.3.12.tar',
},
],
links: [
{
name: 'binary-linux-amd64',
url:
'https://gitlab.com/gitlab-org/gitlab-foss/-/jobs/artifacts/v11.6.0-rc4/download?job=rspec-mysql+41%2F50',
external: false,
},
],
},
},
];
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