mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-11 01:53:51 +00:00
Introduce shoehorn as a possible replacement for ts-mockery
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkDomainRedirects } from '../../src/api/types';
|
||||
import type { Domain } from '../../src/domains/data';
|
||||
import { DomainRow } from '../../src/domains/DomainRow';
|
||||
import type { SelectedServer } from '../../src/servers/data';
|
||||
|
||||
describe('<DomainRow />', () => {
|
||||
const redirectsCombinations = [
|
||||
[Mock.of<ShlinkDomainRedirects>({ baseUrlRedirect: 'foo' })],
|
||||
[Mock.of<ShlinkDomainRedirects>({ invalidShortUrlRedirect: 'bar' })],
|
||||
[Mock.of<ShlinkDomainRedirects>({ baseUrlRedirect: 'baz', regular404Redirect: 'foo' })],
|
||||
[fromPartial<ShlinkDomainRedirects>({ baseUrlRedirect: 'foo' })],
|
||||
[fromPartial<ShlinkDomainRedirects>({ invalidShortUrlRedirect: 'bar' })],
|
||||
[fromPartial<ShlinkDomainRedirects>({ baseUrlRedirect: 'baz', regular404Redirect: 'foo' })],
|
||||
[
|
||||
Mock.of<ShlinkDomainRedirects>(
|
||||
fromPartial<ShlinkDomainRedirects>(
|
||||
{ baseUrlRedirect: 'baz', regular404Redirect: 'bar', invalidShortUrlRedirect: 'foo' },
|
||||
),
|
||||
],
|
||||
@@ -22,7 +21,7 @@ describe('<DomainRow />', () => {
|
||||
<DomainRow
|
||||
domain={domain}
|
||||
defaultRedirects={defaultRedirects}
|
||||
selectedServer={Mock.all<SelectedServer>()}
|
||||
selectedServer={fromPartial({})}
|
||||
editDomainRedirects={jest.fn()}
|
||||
checkDomainHealth={jest.fn()}
|
||||
/>
|
||||
@@ -31,7 +30,7 @@ describe('<DomainRow />', () => {
|
||||
);
|
||||
|
||||
it.each(redirectsCombinations)('shows expected redirects', (redirects) => {
|
||||
setUp(Mock.of<Domain>({ domain: '', isDefault: true, redirects }));
|
||||
setUp(fromPartial({ domain: '', isDefault: true, redirects }));
|
||||
const cells = screen.getAllByRole('cell');
|
||||
|
||||
redirects?.baseUrlRedirect && expect(cells[1]).toHaveTextContent(redirects.baseUrlRedirect);
|
||||
@@ -42,9 +41,9 @@ describe('<DomainRow />', () => {
|
||||
|
||||
it.each([
|
||||
[undefined],
|
||||
[Mock.of<ShlinkDomainRedirects>()],
|
||||
[fromPartial<ShlinkDomainRedirects>({})],
|
||||
])('shows expected "no redirects"', (redirects) => {
|
||||
setUp(Mock.of<Domain>({ domain: '', isDefault: true, redirects }));
|
||||
setUp(fromPartial({ domain: '', isDefault: true, redirects }));
|
||||
const cells = screen.getAllByRole('cell');
|
||||
|
||||
expect(cells[1]).toHaveTextContent('No redirect');
|
||||
@@ -54,7 +53,7 @@ describe('<DomainRow />', () => {
|
||||
});
|
||||
|
||||
it.each(redirectsCombinations)('shows expected fallback redirects', (fallbackRedirects) => {
|
||||
setUp(Mock.of<Domain>({ domain: '', isDefault: true }), fallbackRedirects);
|
||||
setUp(fromPartial({ domain: '', isDefault: true }), fallbackRedirects);
|
||||
const cells = screen.getAllByRole('cell');
|
||||
|
||||
fallbackRedirects?.baseUrlRedirect && expect(cells[1]).toHaveTextContent(
|
||||
@@ -69,7 +68,7 @@ describe('<DomainRow />', () => {
|
||||
});
|
||||
|
||||
it.each([[true], [false]])('shows icon on default domain only', (isDefault) => {
|
||||
const { container } = setUp(Mock.of<Domain>({ domain: '', isDefault }));
|
||||
const { container } = setUp(fromPartial({ domain: '', isDefault }));
|
||||
|
||||
if (isDefault) {
|
||||
expect(container.querySelector('#defaultDomainIcon')).toBeInTheDocument();
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import { screen, waitFor } from '@testing-library/react';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import type { ShlinkDomain } from '../../src/api/types';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import { DomainSelector } from '../../src/domains/DomainSelector';
|
||||
import type { DomainsList } from '../../src/domains/reducers/domainsList';
|
||||
import { renderWithEvents } from '../__helpers__/setUpTest';
|
||||
|
||||
describe('<DomainSelector />', () => {
|
||||
const domainsList = Mock.of<DomainsList>({
|
||||
const domainsList = fromPartial<DomainsList>({
|
||||
domains: [
|
||||
Mock.of<ShlinkDomain>({ domain: 'default.com', isDefault: true }),
|
||||
Mock.of<ShlinkDomain>({ domain: 'foo.com' }),
|
||||
Mock.of<ShlinkDomain>({ domain: 'bar.com' }),
|
||||
fromPartial({ domain: 'default.com', isDefault: true }),
|
||||
fromPartial({ domain: 'foo.com' }),
|
||||
fromPartial({ domain: 'bar.com' }),
|
||||
],
|
||||
});
|
||||
const setUp = (value = '') => renderWithEvents(
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { screen, waitFor } from '@testing-library/react';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkDomain } from '../../src/api/types';
|
||||
import type { ProblemDetailsError } from '../../src/api/types/errors';
|
||||
import { ManageDomains } from '../../src/domains/ManageDomains';
|
||||
import type { DomainsList } from '../../src/domains/reducers/domainsList';
|
||||
import type { SelectedServer } from '../../src/servers/data';
|
||||
import { renderWithEvents } from '../__helpers__/setUpTest';
|
||||
|
||||
describe('<ManageDomains />', () => {
|
||||
@@ -17,14 +16,14 @@ describe('<ManageDomains />', () => {
|
||||
editDomainRedirects={jest.fn()}
|
||||
checkDomainHealth={jest.fn()}
|
||||
domainsList={domainsList}
|
||||
selectedServer={Mock.all<SelectedServer>()}
|
||||
selectedServer={fromPartial({})}
|
||||
/>,
|
||||
);
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
it('shows loading message while domains are loading', () => {
|
||||
setUp(Mock.of<DomainsList>({ loading: true, filteredDomains: [] }));
|
||||
setUp(fromPartial({ loading: true, filteredDomains: [] }));
|
||||
|
||||
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Error loading domains :(')).not.toBeInTheDocument();
|
||||
@@ -32,17 +31,17 @@ describe('<ManageDomains />', () => {
|
||||
|
||||
it.each([
|
||||
[undefined, 'Error loading domains :('],
|
||||
[Mock.of<ProblemDetailsError>(), 'Error loading domains :('],
|
||||
[Mock.of<ProblemDetailsError>({ detail: 'Foo error!!' }), 'Foo error!!'],
|
||||
[fromPartial<ProblemDetailsError>({}), 'Error loading domains :('],
|
||||
[fromPartial<ProblemDetailsError>({ detail: 'Foo error!!' }), 'Foo error!!'],
|
||||
])('shows error result when domains loading fails', (errorData, expectedErrorMessage) => {
|
||||
setUp(Mock.of<DomainsList>({ loading: false, error: true, errorData, filteredDomains: [] }));
|
||||
setUp(fromPartial({ loading: false, error: true, errorData, filteredDomains: [] }));
|
||||
|
||||
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
|
||||
expect(screen.getByText(expectedErrorMessage)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('filters domains when SearchField changes', async () => {
|
||||
const { user } = setUp(Mock.of<DomainsList>({ loading: false, error: false, filteredDomains: [] }));
|
||||
const { user } = setUp(fromPartial({ loading: false, error: false, filteredDomains: [] }));
|
||||
|
||||
expect(filterDomains).not.toHaveBeenCalled();
|
||||
await user.type(screen.getByPlaceholderText('Search...'), 'Foo');
|
||||
@@ -50,19 +49,19 @@ describe('<ManageDomains />', () => {
|
||||
});
|
||||
|
||||
it('shows expected headers and one row when list of domains is empty', () => {
|
||||
setUp(Mock.of<DomainsList>({ loading: false, error: false, filteredDomains: [] }));
|
||||
setUp(fromPartial({ loading: false, error: false, filteredDomains: [] }));
|
||||
|
||||
expect(screen.getAllByRole('columnheader')).toHaveLength(7);
|
||||
expect(screen.getByText('No results found')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('has many rows if multiple domains are provided', () => {
|
||||
const filteredDomains = [
|
||||
Mock.of<ShlinkDomain>({ domain: 'foo' }),
|
||||
Mock.of<ShlinkDomain>({ domain: 'bar' }),
|
||||
Mock.of<ShlinkDomain>({ domain: 'baz' }),
|
||||
const filteredDomains: ShlinkDomain[] = [
|
||||
fromPartial({ domain: 'foo' }),
|
||||
fromPartial({ domain: 'bar' }),
|
||||
fromPartial({ domain: 'baz' }),
|
||||
];
|
||||
setUp(Mock.of<DomainsList>({ loading: false, error: false, filteredDomains }));
|
||||
setUp(fromPartial({ loading: false, error: false, filteredDomains }));
|
||||
|
||||
expect(screen.getAllByRole('row')).toHaveLength(filteredDomains.length + 1);
|
||||
expect(screen.getByText('foo')).toBeInTheDocument();
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { screen, waitForElementToBeRemoved } from '@testing-library/react';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import type { Domain } from '../../../src/domains/data';
|
||||
import { DomainDropdown } from '../../../src/domains/helpers/DomainDropdown';
|
||||
import type { ReachableServer, SelectedServer } from '../../../src/servers/data';
|
||||
import type { SelectedServer } from '../../../src/servers/data';
|
||||
import type { SemVer } from '../../../src/utils/helpers/version';
|
||||
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
||||
|
||||
@@ -12,8 +12,8 @@ describe('<DomainDropdown />', () => {
|
||||
const setUp = (domain?: Domain, selectedServer?: SelectedServer) => renderWithEvents(
|
||||
<MemoryRouter>
|
||||
<DomainDropdown
|
||||
domain={domain ?? Mock.all<Domain>()}
|
||||
selectedServer={selectedServer ?? Mock.all<SelectedServer>()}
|
||||
domain={domain ?? fromPartial({})}
|
||||
selectedServer={selectedServer ?? fromPartial({})}
|
||||
editDomainRedirects={editDomainRedirects}
|
||||
/>
|
||||
</MemoryRouter>,
|
||||
@@ -33,8 +33,8 @@ describe('<DomainDropdown />', () => {
|
||||
[false, ''],
|
||||
])('points first link to the proper section', (isDefault, expectedLink) => {
|
||||
setUp(
|
||||
Mock.of<Domain>({ domain: 'foo.com', isDefault }),
|
||||
Mock.of<ReachableServer>({ version: '3.1.0', id: '123' }),
|
||||
fromPartial({ domain: 'foo.com', isDefault }),
|
||||
fromPartial({ version: '3.1.0', id: '123' }),
|
||||
);
|
||||
|
||||
expect(screen.getByText('Visit stats')).toHaveAttribute('href', `/server/123/domain/foo.com${expectedLink}/visits`);
|
||||
@@ -46,8 +46,8 @@ describe('<DomainDropdown />', () => {
|
||||
[false, '2.9.0' as SemVer, true],
|
||||
])('allows editing certain the domains', (isDefault, serverVersion, canBeEdited) => {
|
||||
setUp(
|
||||
Mock.of<Domain>({ domain: 'foo.com', isDefault }),
|
||||
Mock.of<ReachableServer>({ version: serverVersion, id: '123' }),
|
||||
fromPartial({ domain: 'foo.com', isDefault }),
|
||||
fromPartial({ version: serverVersion, id: '123' }),
|
||||
);
|
||||
|
||||
if (canBeEdited) {
|
||||
@@ -62,7 +62,7 @@ describe('<DomainDropdown />', () => {
|
||||
['bar.org'],
|
||||
['baz.net'],
|
||||
])('displays modal when editing redirects', async (domain) => {
|
||||
const { user } = setUp(Mock.of<Domain>({ domain, isDefault: false }));
|
||||
const { user } = setUp(fromPartial({ domain, isDefault: false }));
|
||||
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole('form')).not.toBeInTheDocument();
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { screen } from '@testing-library/react';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { DomainStatus } from '../../../src/domains/data';
|
||||
import { DomainStatusIcon } from '../../../src/domains/helpers/DomainStatusIcon';
|
||||
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
||||
|
||||
describe('<DomainStatusIcon />', () => {
|
||||
const matchMedia = jest.fn().mockReturnValue(Mock.of<MediaQueryList>({ matches: false }));
|
||||
const matchMedia = jest.fn().mockReturnValue(fromPartial<MediaQueryList>({ matches: false }));
|
||||
const setUp = (status: DomainStatus) => renderWithEvents(
|
||||
<DomainStatusIcon status={status} matchMedia={matchMedia} />,
|
||||
);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { fireEvent, screen, waitFor } from '@testing-library/react';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkDomain } from '../../../src/api/types';
|
||||
import { EditDomainRedirectsModal } from '../../../src/domains/helpers/EditDomainRedirectsModal';
|
||||
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
||||
@@ -7,7 +7,7 @@ import { renderWithEvents } from '../../__helpers__/setUpTest';
|
||||
describe('<EditDomainRedirectsModal />', () => {
|
||||
const editDomainRedirects = jest.fn().mockResolvedValue(undefined);
|
||||
const toggle = jest.fn();
|
||||
const domain = Mock.of<ShlinkDomain>({
|
||||
const domain = fromPartial<ShlinkDomain>({
|
||||
domain: 'foo.com',
|
||||
redirects: {
|
||||
baseUrlRedirect: 'baz',
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
||||
import type { ShlinkDomainRedirects } from '../../../src/api/types';
|
||||
import type { EditDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
||||
import { editDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
||||
|
||||
describe('domainRedirectsReducer', () => {
|
||||
@@ -9,17 +8,17 @@ describe('domainRedirectsReducer', () => {
|
||||
|
||||
describe('editDomainRedirects', () => {
|
||||
const domain = 'example.com';
|
||||
const redirects = Mock.all<ShlinkDomainRedirects>();
|
||||
const redirects = fromPartial<ShlinkDomainRedirects>({});
|
||||
const dispatch = jest.fn();
|
||||
const getState = jest.fn();
|
||||
const editDomainRedirectsCall = jest.fn();
|
||||
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ editDomainRedirects: editDomainRedirectsCall });
|
||||
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ editDomainRedirects: editDomainRedirectsCall });
|
||||
const editDomainRedirectsAction = editDomainRedirects(buildShlinkApiClient);
|
||||
|
||||
it('dispatches domain and redirects once loaded', async () => {
|
||||
editDomainRedirectsCall.mockResolvedValue(redirects);
|
||||
|
||||
await editDomainRedirectsAction(Mock.of<EditDomainRedirects>({ domain }))(dispatch, getState, {});
|
||||
await editDomainRedirectsAction(fromPartial({ domain }))(dispatch, getState, {});
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
||||
import type { ShlinkDomainRedirects } from '../../../src/api/types';
|
||||
import { parseApiError } from '../../../src/api/utils';
|
||||
@@ -6,26 +6,23 @@ import type { ShlinkState } from '../../../src/container/types';
|
||||
import type { Domain } from '../../../src/domains/data';
|
||||
import type { EditDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
||||
import { editDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
||||
import type {
|
||||
DomainsList } from '../../../src/domains/reducers/domainsList';
|
||||
import {
|
||||
domainsListReducerCreator,
|
||||
replaceRedirectsOnDomain,
|
||||
replaceStatusOnDomain,
|
||||
} from '../../../src/domains/reducers/domainsList';
|
||||
import type { SelectedServer, ServerData } from '../../../src/servers/data';
|
||||
|
||||
describe('domainsListReducer', () => {
|
||||
const dispatch = jest.fn();
|
||||
const getState = jest.fn();
|
||||
const listDomains = jest.fn();
|
||||
const health = jest.fn();
|
||||
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ listDomains, health });
|
||||
const filteredDomains = [
|
||||
Mock.of<Domain>({ domain: 'foo', status: 'validating' }),
|
||||
Mock.of<Domain>({ domain: 'Boo', status: 'validating' }),
|
||||
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ listDomains, health });
|
||||
const filteredDomains: Domain[] = [
|
||||
fromPartial({ domain: 'foo', status: 'validating' }),
|
||||
fromPartial({ domain: 'Boo', status: 'validating' }),
|
||||
];
|
||||
const domains = [...filteredDomains, Mock.of<Domain>({ domain: 'bar', status: 'validating' })];
|
||||
const domains: Domain[] = [...filteredDomains, fromPartial({ domain: 'bar', status: 'validating' })];
|
||||
const error = { type: 'NOT_FOUND', status: 404 } as unknown as Error;
|
||||
const editDomainRedirectsThunk = editDomainRedirects(buildShlinkApiClient);
|
||||
const { reducer, listDomains: listDomainsAction, checkDomainHealth, filterDomains } = domainsListReducerCreator(
|
||||
@@ -55,7 +52,7 @@ describe('domainsListReducer', () => {
|
||||
});
|
||||
|
||||
it('filters domains on FILTER_DOMAINS', () => {
|
||||
expect(reducer(Mock.of<DomainsList>({ domains }), filterDomains('oO'))).toEqual({ domains, filteredDomains });
|
||||
expect(reducer(fromPartial({ domains }), filterDomains('oO'))).toEqual({ domains, filteredDomains });
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -71,7 +68,7 @@ describe('domainsListReducer', () => {
|
||||
const editDomainRedirects: EditDomainRedirects = { domain, redirects };
|
||||
|
||||
expect(reducer(
|
||||
Mock.of<DomainsList>({ domains, filteredDomains }),
|
||||
fromPartial({ domains, filteredDomains }),
|
||||
editDomainRedirectsThunk.fulfilled(editDomainRedirects, '', editDomainRedirects),
|
||||
)).toEqual({
|
||||
domains: domains.map(replaceRedirectsOnDomain(editDomainRedirects)),
|
||||
@@ -85,7 +82,7 @@ describe('domainsListReducer', () => {
|
||||
['does_not_exist'],
|
||||
])('replaces status on proper domain on VALIDATE_DOMAIN', (domain) => {
|
||||
expect(reducer(
|
||||
Mock.of<DomainsList>({ domains, filteredDomains }),
|
||||
fromPartial({ domains, filteredDomains }),
|
||||
checkDomainHealth.fulfilled({ domain, status: 'valid' }, '', ''),
|
||||
)).toEqual({
|
||||
domains: domains.map(replaceStatusOnDomain(domain, 'valid')),
|
||||
@@ -122,8 +119,8 @@ describe('domainsListReducer', () => {
|
||||
const domain = 'example.com';
|
||||
|
||||
it('dispatches invalid status when selected server does not have all required data', async () => {
|
||||
getState.mockReturnValue(Mock.of<ShlinkState>({
|
||||
selectedServer: Mock.all<SelectedServer>(),
|
||||
getState.mockReturnValue(fromPartial<ShlinkState>({
|
||||
selectedServer: {},
|
||||
}));
|
||||
|
||||
await checkDomainHealth(domain)(dispatch, getState, {});
|
||||
@@ -136,11 +133,11 @@ describe('domainsListReducer', () => {
|
||||
});
|
||||
|
||||
it('dispatches invalid status when health endpoint returns an error', async () => {
|
||||
getState.mockReturnValue(Mock.of<ShlinkState>({
|
||||
selectedServer: Mock.of<ServerData>({
|
||||
getState.mockReturnValue(fromPartial<ShlinkState>({
|
||||
selectedServer: {
|
||||
url: 'https://myerver.com',
|
||||
apiKey: '123',
|
||||
}),
|
||||
},
|
||||
}));
|
||||
health.mockRejectedValue({});
|
||||
|
||||
@@ -160,11 +157,11 @@ describe('domainsListReducer', () => {
|
||||
healthStatus,
|
||||
expectedStatus,
|
||||
) => {
|
||||
getState.mockReturnValue(Mock.of<ShlinkState>({
|
||||
selectedServer: Mock.of<ServerData>({
|
||||
getState.mockReturnValue(fromPartial<ShlinkState>({
|
||||
selectedServer: {
|
||||
url: 'https://myerver.com',
|
||||
apiKey: '123',
|
||||
}),
|
||||
},
|
||||
}));
|
||||
health.mockResolvedValue({ status: healthStatus });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user