Merge pull request #211 from acelaya-forks/feature/jest-each

Feature/jest each
This commit is contained in:
Alejandro Celaya 2020-02-17 18:32:52 +01:00 committed by GitHub
commit 3b0e282a52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 53 additions and 45 deletions

View file

@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org). The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org).
## [Unreleased]
#### Added
* *Nothing*
#### Changed
* [#205](https://github.com/shlinkio/shlink-web-client/issues/205) Replaced `jest-each` package by jet's native `test.each` function.
#### Deprecated
* *Nothing*
#### Removed
* *Nothing*
#### Fixed
* *Nothing*
## 2.3.1 - 2020-02-08 ## 2.3.1 - 2020-02-08
#### Added #### Added

View file

@ -104,7 +104,6 @@
"html-webpack-plugin": "^4.0.0-beta.8", "html-webpack-plugin": "^4.0.0-beta.8",
"identity-obj-proxy": "^3.0.0", "identity-obj-proxy": "^3.0.0",
"jest": "^24.9.0", "jest": "^24.9.0",
"jest-each": "^24.9.0",
"jest-pnp-resolver": "^1.2.1", "jest-pnp-resolver": "^1.2.1",
"jest-resolve": "^24.9.0", "jest-resolve": "^24.9.0",
"mini-css-extract-plugin": "^0.8.0", "mini-css-extract-plugin": "^0.8.0",

View file

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { identity } from 'ramda'; import { identity } from 'ramda';
import each from 'jest-each';
import { PaginationItem } from 'reactstrap'; import { PaginationItem } from 'reactstrap';
import SimplePaginator, { ellipsis } from '../../src/common/SimplePaginator'; import SimplePaginator, { ellipsis } from '../../src/common/SimplePaginator';
@ -15,7 +14,7 @@ describe('<SimplePaginator />', () => {
afterEach(() => wrapper && wrapper.unmount()); afterEach(() => wrapper && wrapper.unmount());
each([ -3, -2, 0, 1 ]).it('renders empty when the amount of pages is smaller than 2', (pagesCount) => { it.each([ -3, -2, 0, 1 ])('renders empty when the amount of pages is smaller than 2', (pagesCount) => {
expect(createWrapper(pagesCount).text()).toEqual(''); expect(createWrapper(pagesCount).text()).toEqual('');
}); });

View file

@ -1,6 +1,5 @@
import React from 'react'; import React from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import each from 'jest-each';
import ForServerVersion from '../../../src/servers/helpers/ForServerVersion'; import ForServerVersion from '../../../src/servers/helpers/ForServerVersion';
describe('<ForServerVersion />', () => { describe('<ForServerVersion />', () => {
@ -24,23 +23,23 @@ describe('<ForServerVersion />', () => {
expect(wrapped.html()).toBeNull(); expect(wrapped.html()).toBeNull();
}); });
each([ it.each([
[ '2.0.0', undefined, '1.8.3' ], [ '2.0.0', undefined, '1.8.3' ],
[ undefined, '1.8.0', '1.8.3' ], [ undefined, '1.8.0', '1.8.3' ],
[ '1.7.0', '1.8.0', '1.8.3' ], [ '1.7.0', '1.8.0', '1.8.3' ],
]).it('does not render children when current version does not match requirements', (min, max, version) => { ])('does not render children when current version does not match requirements', (min, max, version) => {
const wrapped = renderComponent(min, max, { version }); const wrapped = renderComponent(min, max, { version });
expect(wrapped.html()).toBeNull(); expect(wrapped.html()).toBeNull();
}); });
each([ it.each([
[ '2.0.0', undefined, '2.8.3' ], [ '2.0.0', undefined, '2.8.3' ],
[ '2.0.0', undefined, '2.0.0' ], [ '2.0.0', undefined, '2.0.0' ],
[ undefined, '1.8.0', '1.8.0' ], [ undefined, '1.8.0', '1.8.0' ],
[ undefined, '1.8.0', '1.7.1' ], [ undefined, '1.8.0', '1.7.1' ],
[ '1.7.0', '1.8.0', '1.7.3' ], [ '1.7.0', '1.8.0', '1.7.3' ],
]).it('renders children when current version matches requirements', (min, max, version) => { ])('renders children when current version matches requirements', (min, max, version) => {
const wrapped = renderComponent(min, max, { version }); const wrapped = renderComponent(min, max, { version });
expect(wrapped.html()).toContain('<span>Hello</span>'); expect(wrapped.html()).toContain('<span>Hello</span>');

View file

@ -1,4 +1,3 @@
import each from 'jest-each';
import reducer, { import reducer, {
selectServer, selectServer,
resetSelectedServer, resetSelectedServer,
@ -44,11 +43,11 @@ describe('selectedServerReducer', () => {
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
each([ it.each([
[ version, version ], [ version, version ],
[ 'latest', MAX_FALLBACK_VERSION ], [ 'latest', MAX_FALLBACK_VERSION ],
[ '%invalid_semver%', MIN_FALLBACK_VERSION ], [ '%invalid_semver%', MIN_FALLBACK_VERSION ],
]).it('dispatches proper actions', async (serverVersion, expectedVersion) => { ])('dispatches proper actions', async (serverVersion, expectedVersion) => {
const expectedSelectedServer = { const expectedSelectedServer = {
...selectedServer, ...selectedServer,
version: expectedVersion, version: expectedVersion,

View file

@ -1,5 +1,4 @@
import { values } from 'ramda'; import { values } from 'ramda';
import each from 'jest-each';
import reducer, { import reducer, {
createServer, createServer,
deleteServer, deleteServer,
@ -47,7 +46,7 @@ describe('serverReducer', () => {
expect(axios.get).not.toHaveBeenCalled(); expect(axios.get).not.toHaveBeenCalled();
}); });
each([ it.each([
[ [
Promise.resolve({ Promise.resolve({
data: [ data: [
@ -82,7 +81,7 @@ describe('serverReducer', () => {
], ],
[ Promise.resolve('<html></html>'), {}], [ Promise.resolve('<html></html>'), {}],
[ Promise.reject({}), {}], [ Promise.reject({}), {}],
]).it('tries to fetch servers from remote when not found locally', async (mockedValue, expectedList) => { ])('tries to fetch servers from remote when not found locally', async (mockedValue, expectedList) => {
axios.get.mockReturnValue(mockedValue); axios.get.mockReturnValue(mockedValue);
await listServers(NoListServersServiceMock, axios)()(dispatch); await listServers(NoListServersServiceMock, axios)()(dispatch);

View file

@ -1,6 +1,5 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import each from 'jest-each';
import searchBarCreator from '../../src/short-urls/SearchBar'; import searchBarCreator from '../../src/short-urls/SearchBar';
import SearchField from '../../src/utils/SearchField'; import SearchField from '../../src/utils/SearchField';
import Tag from '../../src/tags/helpers/Tag'; import Tag from '../../src/tags/helpers/Tag';
@ -62,7 +61,7 @@ describe('<SearchBar />', () => {
expect(listShortUrlsMock).toHaveBeenCalledTimes(1); expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
}); });
each([ 'startDateChange', 'endDateChange' ]).it('updates short URLs list when date range changes', (event) => { it.each([ 'startDateChange', 'endDateChange' ])('updates short URLs list when date range changes', (event) => {
wrapper = shallow( wrapper = shallow(
<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} /> <SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />
); );

View file

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { identity } from 'ramda'; import { identity } from 'ramda';
import each from 'jest-each';
import DeleteShortUrlModal from '../../../src/short-urls/helpers/DeleteShortUrlModal'; import DeleteShortUrlModal from '../../../src/short-urls/helpers/DeleteShortUrlModal';
describe('<DeleteShortUrlModal />', () => { describe('<DeleteShortUrlModal />', () => {
@ -32,7 +31,7 @@ describe('<DeleteShortUrlModal />', () => {
deleteShortUrl.mockClear(); deleteShortUrl.mockClear();
}); });
each([ it.each([
[ [
{ error: 'INVALID_SHORTCODE_DELETION' }, { error: 'INVALID_SHORTCODE_DELETION' },
'This short URL has received too many visits, and therefore, it cannot be deleted.', 'This short URL has received too many visits, and therefore, it cannot be deleted.',
@ -49,7 +48,7 @@ describe('<DeleteShortUrlModal />', () => {
{ type: 'INVALID_SHORTCODE_DELETION', threshold: 8 }, { type: 'INVALID_SHORTCODE_DELETION', threshold: 8 },
'This short URL has received more than 8 visits, and therefore, it cannot be deleted.', 'This short URL has received more than 8 visits, and therefore, it cannot be deleted.',
], ],
]).it('shows threshold error message when threshold error occurs', (errorData, expectedMessage) => { ])('shows threshold error message when threshold error occurs', (errorData, expectedMessage) => {
const wrapper = createWrapper({ const wrapper = createWrapper({
loading: false, loading: false,
error: true, error: true,

View file

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { FormGroup, Modal, ModalHeader } from 'reactstrap'; import { FormGroup, Modal, ModalHeader } from 'reactstrap';
import each from 'jest-each';
import EditMetaModal from '../../../src/short-urls/helpers/EditMetaModal'; import EditMetaModal from '../../../src/short-urls/helpers/EditMetaModal';
describe('<EditMetaModal />', () => { describe('<EditMetaModal />', () => {
@ -40,10 +39,10 @@ describe('<EditMetaModal />', () => {
expect(error).toHaveLength(0); expect(error).toHaveLength(0);
}); });
each([ it.each([
[ true, 'Saving...' ], [ true, 'Saving...' ],
[ false, 'Save' ], [ false, 'Save' ],
]).it('renders submit button on expected state', (saving, expectedText) => { ])('renders submit button on expected state', (saving, expectedText) => {
const wrapper = createWrapper({}, { saving, error: false, meta: {} }); const wrapper = createWrapper({}, { saving, error: false, meta: {} });
const button = wrapper.find('[type="submit"]'); const button = wrapper.find('[type="submit"]');
@ -69,11 +68,11 @@ describe('<EditMetaModal />', () => {
expect(editShortUrlMeta).toHaveBeenCalled(); expect(editShortUrlMeta).toHaveBeenCalled();
}); });
each([ it.each([
[ '.btn-link', 'onClick' ], [ '.btn-link', 'onClick' ],
[ Modal, 'toggle' ], [ Modal, 'toggle' ],
[ ModalHeader, 'toggle' ], [ ModalHeader, 'toggle' ],
]).it('resets meta when modal is toggled in any way', (componentToFind, propToCall) => { ])('resets meta when modal is toggled in any way', (componentToFind, propToCall) => {
const wrapper = createWrapper({}, { saving: false, error: false, meta: {} }); const wrapper = createWrapper({}, { saving: false, error: false, meta: {} });
const component = wrapper.find(componentToFind); const component = wrapper.find(componentToFind);

View file

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { Modal } from 'reactstrap'; import { Modal } from 'reactstrap';
import each from 'jest-each';
import createEditTagsModal from '../../../src/short-urls/helpers/EditTagsModal'; import createEditTagsModal from '../../../src/short-urls/helpers/EditTagsModal';
describe('<EditTagsModal />', () => { describe('<EditTagsModal />', () => {
@ -76,7 +75,7 @@ describe('<EditTagsModal />', () => {
expect(saveBtn.text()).toEqual('Saving tags...'); expect(saveBtn.text()).toEqual('Saving tags...');
}); });
each([[ undefined ], [ null ], [ 'example.com' ]]).it('saves tags when save button is clicked', (domain, done) => { it.each([[ undefined ], [ null ], [ 'example.com' ]])('saves tags when save button is clicked', (domain, done) => {
const wrapper = createWrapper({ const wrapper = createWrapper({
shortCode, shortCode,
tags: [], tags: [],

View file

@ -1,7 +1,6 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import { UncontrolledTooltip } from 'reactstrap'; import { UncontrolledTooltip } from 'reactstrap';
import each from 'jest-each';
import ShortUrlVisitsCount from '../../../src/short-urls/helpers/ShortUrlVisitsCount'; import ShortUrlVisitsCount from '../../../src/short-urls/helpers/ShortUrlVisitsCount';
describe('<ShortUrlVisitsCount />', () => { describe('<ShortUrlVisitsCount />', () => {
@ -15,7 +14,7 @@ describe('<ShortUrlVisitsCount />', () => {
afterEach(() => wrapper && wrapper.unmount()); afterEach(() => wrapper && wrapper.unmount());
each([ undefined, {}]).it('just returns visits when no maxVisits is provided', (meta) => { it.each([ undefined, {}])('just returns visits when no maxVisits is provided', (meta) => {
const visitsCount = 45; const visitsCount = 45;
const wrapper = createWrapper(visitsCount, { meta }); const wrapper = createWrapper(visitsCount, { meta });
const maxVisitsHelper = wrapper.find('.short-urls-visits-count__max-visits-control'); const maxVisitsHelper = wrapper.find('.short-urls-visits-count__max-visits-control');

View file

@ -1,6 +1,5 @@
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
import each from 'jest-each';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import VisitStatsLink from '../../../src/short-urls/helpers/VisitStatsLink'; import VisitStatsLink from '../../../src/short-urls/helpers/VisitStatsLink';
@ -9,14 +8,14 @@ describe('<VisitStatsLink />', () => {
afterEach(() => wrapper && wrapper.unmount()); afterEach(() => wrapper && wrapper.unmount());
each([ it.each([
[ undefined, undefined ], [ undefined, undefined ],
[ null, null ], [ null, null ],
[{}, null ], [{}, null ],
[{}, undefined ], [{}, undefined ],
[ null, {}], [ null, {}],
[ undefined, {}], [ undefined, {}],
]).it('only renders a plan span when either server or short URL are not set', (selectedServer, shortUrl) => { ])('only renders a plan span when either server or short URL are not set', (selectedServer, shortUrl) => {
wrapper = shallow(<VisitStatsLink selectedServer={selectedServer} shortUrl={shortUrl}>Something</VisitStatsLink>); wrapper = shallow(<VisitStatsLink selectedServer={selectedServer} shortUrl={shortUrl}>Something</VisitStatsLink>);
const link = wrapper.find(Link); const link = wrapper.find(Link);
@ -24,14 +23,14 @@ describe('<VisitStatsLink />', () => {
expect(wrapper.html()).toEqual('<span>Something</span>'); expect(wrapper.html()).toEqual('<span>Something</span>');
}); });
each([ it.each([
[{ id: '1' }, { shortCode: 'abc123' }, '/server/1/short-code/abc123/visits' ], [{ id: '1' }, { shortCode: 'abc123' }, '/server/1/short-code/abc123/visits' ],
[ [
{ id: '3' }, { id: '3' },
{ shortCode: 'def456', domain: 'example.com' }, { shortCode: 'def456', domain: 'example.com' },
'/server/3/short-code/def456/visits?domain=example.com', '/server/3/short-code/def456/visits?domain=example.com',
], ],
]).it('renders link with expected query when', (selectedServer, shortUrl, expectedLink) => { ])('renders link with expected query when', (selectedServer, shortUrl, expectedLink) => {
wrapper = shallow(<VisitStatsLink selectedServer={selectedServer} shortUrl={shortUrl}>Something</VisitStatsLink>); wrapper = shallow(<VisitStatsLink selectedServer={selectedServer} shortUrl={shortUrl}>Something</VisitStatsLink>);
const link = wrapper.find(Link); const link = wrapper.find(Link);
const to = link.prop('to'); const to = link.prop('to');

View file

@ -1,4 +1,3 @@
import each from 'jest-each';
import reducer, { import reducer, {
DELETE_SHORT_URL_ERROR, DELETE_SHORT_URL_ERROR,
DELETE_SHORT_URL_START, DELETE_SHORT_URL_START,
@ -60,9 +59,9 @@ describe('shortUrlDeletionReducer', () => {
getState.mockClear(); getState.mockClear();
}); });
each( it.each(
[[ undefined ], [ null ], [ 'example.com' ]] [[ undefined ], [ null ], [ 'example.com' ]]
).it('dispatches proper actions if API client request succeeds', async (domain) => { )('dispatches proper actions if API client request succeeds', async (domain) => {
const apiClientMock = { const apiClientMock = {
deleteShortUrl: jest.fn(() => ''), deleteShortUrl: jest.fn(() => ''),
}; };

View file

@ -1,5 +1,4 @@
import moment from 'moment'; import moment from 'moment';
import each from 'jest-each';
import reducer, { import reducer, {
EDIT_SHORT_URL_META_START, EDIT_SHORT_URL_META_START,
EDIT_SHORT_URL_META_ERROR, EDIT_SHORT_URL_META_ERROR,
@ -57,7 +56,7 @@ describe('shortUrlMetaReducer', () => {
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
each([[ undefined ], [ null ], [ 'example.com' ]]).it('dispatches metadata on success', async (domain) => { it.each([[ undefined ], [ null ], [ 'example.com' ]])('dispatches metadata on success', async (domain) => {
await editShortUrlMeta(buildShlinkApiClient)(shortCode, domain, meta)(dispatch); await editShortUrlMeta(buildShlinkApiClient)(shortCode, domain, meta)(dispatch);
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1); expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);

View file

@ -1,4 +1,3 @@
import each from 'jest-each';
import reducer, { import reducer, {
EDIT_SHORT_URL_TAGS_ERROR, EDIT_SHORT_URL_TAGS_ERROR,
EDIT_SHORT_URL_TAGS_START, EDIT_SHORT_URL_TAGS_START,
@ -61,7 +60,7 @@ describe('shortUrlTagsReducer', () => {
dispatch.mockReset(); dispatch.mockReset();
}); });
each([[ undefined ], [ null ], [ 'example.com' ]]).it('dispatches normalized tags on success', async (domain) => { it.each([[ undefined ], [ null ], [ 'example.com' ]])('dispatches normalized tags on success', async (domain) => {
const normalizedTags = [ 'bar', 'foo' ]; const normalizedTags = [ 'bar', 'foo' ];
updateShortUrlTags.mockResolvedValue(normalizedTags); updateShortUrlTags.mockResolvedValue(normalizedTags);

View file

@ -1,4 +1,3 @@
import each from 'jest-each';
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient'; import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
describe('ShlinkApiClient', () => { describe('ShlinkApiClient', () => {
@ -73,7 +72,7 @@ describe('ShlinkApiClient', () => {
}); });
describe('getShortUrl', () => { describe('getShortUrl', () => {
each(shortCodesWithDomainCombinations).it('properly returns short URL', async (shortCode, domain) => { it.each(shortCodesWithDomainCombinations)('properly returns short URL', async (shortCode, domain) => {
const expectedShortUrl = { foo: 'bar' }; const expectedShortUrl = { foo: 'bar' };
const axiosSpy = jest.fn(createAxiosMock({ const axiosSpy = jest.fn(createAxiosMock({
data: expectedShortUrl, data: expectedShortUrl,
@ -92,7 +91,7 @@ describe('ShlinkApiClient', () => {
}); });
describe('updateShortUrlTags', () => { describe('updateShortUrlTags', () => {
each(shortCodesWithDomainCombinations).it('properly updates short URL tags', async (shortCode, domain) => { it.each(shortCodesWithDomainCombinations)('properly updates short URL tags', async (shortCode, domain) => {
const expectedTags = [ 'foo', 'bar' ]; const expectedTags = [ 'foo', 'bar' ];
const axiosSpy = jest.fn(createAxiosMock({ const axiosSpy = jest.fn(createAxiosMock({
data: { tags: expectedTags }, data: { tags: expectedTags },
@ -111,7 +110,7 @@ describe('ShlinkApiClient', () => {
}); });
describe('updateShortUrlMeta', () => { describe('updateShortUrlMeta', () => {
each(shortCodesWithDomainCombinations).it('properly updates short URL meta', async (shortCode, domain) => { it.each(shortCodesWithDomainCombinations)('properly updates short URL meta', async (shortCode, domain) => {
const expectedMeta = { const expectedMeta = {
maxVisits: 50, maxVisits: 50,
validSince: '2025-01-01T10:00:00+01:00', validSince: '2025-01-01T10:00:00+01:00',
@ -181,7 +180,7 @@ describe('ShlinkApiClient', () => {
}); });
describe('deleteShortUrl', () => { describe('deleteShortUrl', () => {
each(shortCodesWithDomainCombinations).it('properly deletes provided short URL', async (shortCode, domain) => { it.each(shortCodesWithDomainCombinations)('properly deletes provided short URL', async (shortCode, domain) => {
const axiosSpy = jest.fn(createAxiosMock({})); const axiosSpy = jest.fn(createAxiosMock({}));
const { deleteShortUrl } = new ShlinkApiClient(axiosSpy); const { deleteShortUrl } = new ShlinkApiClient(axiosSpy);