mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-15 11:06:18 +00:00
Fix shlink-web-component tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkDomainRedirects } from '../../src/api/types';
|
||||
import type { ShlinkDomainRedirects } from '../../src/api-contract';
|
||||
import type { Domain } from '../../src/domains/data';
|
||||
import { DomainRow } from '../../src/domains/DomainRow';
|
||||
|
||||
@@ -21,7 +21,6 @@ describe('<DomainRow />', () => {
|
||||
<DomainRow
|
||||
domain={domain}
|
||||
defaultRedirects={defaultRedirects}
|
||||
selectedServer={fromPartial({})}
|
||||
editDomainRedirects={vi.fn()}
|
||||
checkDomainHealth={vi.fn()}
|
||||
/>
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
import { screen, waitForElementToBeRemoved } from '@testing-library/react';
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import type { SelectedServer } from '../../../../src/servers/data';
|
||||
import type { SemVer } from '../../../../src/utils/helpers/version';
|
||||
import type { Domain } from '../../../src/domains/data';
|
||||
import { DomainDropdown } from '../../../src/domains/helpers/DomainDropdown';
|
||||
import { FeaturesProvider } from '../../../src/utils/features';
|
||||
import { RoutesPrefixProvider } from '../../../src/utils/routesPrefix';
|
||||
import { renderWithEvents } from '../../__helpers__/setUpTest';
|
||||
|
||||
describe('<DomainDropdown />', () => {
|
||||
const editDomainRedirects = vi.fn().mockResolvedValue(undefined);
|
||||
const setUp = (domain?: Domain, selectedServer?: SelectedServer) => renderWithEvents(
|
||||
const setUp = ({ domain, withVisits = true }: { domain?: Domain; withVisits?: boolean } = {}) => renderWithEvents(
|
||||
<MemoryRouter>
|
||||
<DomainDropdown
|
||||
domain={domain ?? fromPartial({})}
|
||||
selectedServer={selectedServer ?? fromPartial({})}
|
||||
editDomainRedirects={editDomainRedirects}
|
||||
/>
|
||||
<RoutesPrefixProvider value="/server/123">
|
||||
<FeaturesProvider value={fromPartial({ domainVisits: withVisits })}>
|
||||
<DomainDropdown
|
||||
domain={domain ?? fromPartial({})}
|
||||
editDomainRedirects={editDomainRedirects}
|
||||
/>
|
||||
</FeaturesProvider>
|
||||
</RoutesPrefixProvider>
|
||||
</MemoryRouter>,
|
||||
);
|
||||
|
||||
it('renders expected menu items', () => {
|
||||
setUp();
|
||||
setUp({ withVisits: false });
|
||||
|
||||
expect(screen.queryByText('Visit stats')).not.toBeInTheDocument();
|
||||
expect(screen.getByText('Edit redirects')).toBeInTheDocument();
|
||||
@@ -30,37 +33,17 @@ describe('<DomainDropdown />', () => {
|
||||
[true, '_DEFAULT'],
|
||||
[false, ''],
|
||||
])('points first link to the proper section', (isDefault, expectedLink) => {
|
||||
setUp(
|
||||
fromPartial({ domain: 'foo.com', isDefault }),
|
||||
fromPartial({ version: '3.1.0', id: '123' }),
|
||||
);
|
||||
setUp({ domain: fromPartial({ domain: 'foo.com', isDefault }) });
|
||||
|
||||
expect(screen.getByText('Visit stats')).toHaveAttribute('href', `/server/123/domain/foo.com${expectedLink}/visits`);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[true, '2.9.0' as SemVer, false],
|
||||
[true, '2.10.0' as SemVer, true],
|
||||
[false, '2.9.0' as SemVer, true],
|
||||
])('allows editing certain the domains', (isDefault, serverVersion, canBeEdited) => {
|
||||
setUp(
|
||||
fromPartial({ domain: 'foo.com', isDefault }),
|
||||
fromPartial({ version: serverVersion, id: '123' }),
|
||||
);
|
||||
|
||||
if (canBeEdited) {
|
||||
expect(screen.getByText('Edit redirects')).not.toHaveAttribute('disabled');
|
||||
} else {
|
||||
expect(screen.getByText('Edit redirects')).toHaveAttribute('disabled');
|
||||
}
|
||||
});
|
||||
|
||||
it.each([
|
||||
['foo.com'],
|
||||
['bar.org'],
|
||||
['baz.net'],
|
||||
])('displays modal when editing redirects', async (domain) => {
|
||||
const { user } = setUp(fromPartial({ domain, isDefault: false }));
|
||||
const { user } = setUp({ domain: fromPartial({ domain, isDefault: false }) });
|
||||
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole('form')).not.toBeInTheDocument();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
|
||||
import type { ShlinkDomainRedirects } from '../../../src/api/types';
|
||||
import type { ShlinkDomainRedirects } from '../../../src/api-contract';
|
||||
import { editDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
||||
|
||||
describe('domainRedirectsReducer', () => {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
|
||||
import type { ShlinkState } from '../../../../src/container/types';
|
||||
import type { ShlinkDomainRedirects } from '../../../src/api/types';
|
||||
import { parseApiError } from '../../../src/api/utils';
|
||||
import type { ShlinkApiClient, ShlinkDomainRedirects } from '../../../src/api-contract';
|
||||
import { parseApiError } from '../../../src/api-contract/utils';
|
||||
import type { Domain } from '../../../src/domains/data';
|
||||
import type { EditDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
||||
import { editDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
||||
@@ -17,35 +15,35 @@ describe('domainsListReducer', () => {
|
||||
const getState = vi.fn();
|
||||
const listDomains = vi.fn();
|
||||
const health = vi.fn();
|
||||
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ listDomains, health });
|
||||
const apiClientFactory = () => fromPartial<ShlinkApiClient>({ listDomains, health });
|
||||
const filteredDomains: Domain[] = [
|
||||
fromPartial({ domain: 'foo', status: 'validating' }),
|
||||
fromPartial({ domain: 'Boo', 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 editDomainRedirectsThunk = editDomainRedirects(apiClientFactory);
|
||||
const { reducer, listDomains: listDomainsAction, checkDomainHealth, filterDomains } = domainsListReducerCreator(
|
||||
buildShlinkApiClient,
|
||||
apiClientFactory,
|
||||
editDomainRedirectsThunk,
|
||||
);
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns loading on LIST_DOMAINS_START', () => {
|
||||
expect(reducer(undefined, listDomainsAction.pending(''))).toEqual(
|
||||
expect(reducer(undefined, listDomainsAction.pending('', {}))).toEqual(
|
||||
{ domains: [], filteredDomains: [], loading: true, error: false },
|
||||
);
|
||||
});
|
||||
|
||||
it('returns error on LIST_DOMAINS_ERROR', () => {
|
||||
expect(reducer(undefined, listDomainsAction.rejected(error, ''))).toEqual(
|
||||
expect(reducer(undefined, listDomainsAction.rejected(error, '', {}))).toEqual(
|
||||
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error) },
|
||||
);
|
||||
});
|
||||
|
||||
it('returns domains on LIST_DOMAINS', () => {
|
||||
expect(
|
||||
reducer(undefined, listDomainsAction.fulfilled({ domains }, '')),
|
||||
reducer(undefined, listDomainsAction.fulfilled({ domains }, '', {})),
|
||||
).toEqual({ domains, filteredDomains: domains, loading: false, error: false });
|
||||
});
|
||||
|
||||
@@ -93,7 +91,7 @@ describe('domainsListReducer', () => {
|
||||
it('dispatches domains once loaded', async () => {
|
||||
listDomains.mockResolvedValue({ data: domains });
|
||||
|
||||
await listDomainsAction()(dispatch, getState, {});
|
||||
await listDomainsAction({})(dispatch, getState, {});
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
||||
@@ -116,33 +114,13 @@ describe('domainsListReducer', () => {
|
||||
describe('checkDomainHealth', () => {
|
||||
const domain = 'example.com';
|
||||
|
||||
it('dispatches invalid status when selected server does not have all required data', async () => {
|
||||
getState.mockReturnValue(fromPartial<ShlinkState>({
|
||||
selectedServer: {},
|
||||
}));
|
||||
|
||||
await checkDomainHealth(domain)(dispatch, getState, {});
|
||||
|
||||
expect(getState).toHaveBeenCalledTimes(1);
|
||||
expect(health).not.toHaveBeenCalled();
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
||||
payload: { domain, status: 'invalid' },
|
||||
}));
|
||||
});
|
||||
|
||||
it('dispatches invalid status when health endpoint returns an error', async () => {
|
||||
getState.mockReturnValue(fromPartial<ShlinkState>({
|
||||
selectedServer: {
|
||||
url: 'https://myerver.com',
|
||||
apiKey: '123',
|
||||
},
|
||||
}));
|
||||
health.mockRejectedValue({});
|
||||
|
||||
await checkDomainHealth(domain)(dispatch, getState, {});
|
||||
|
||||
expect(getState).toHaveBeenCalledTimes(1);
|
||||
expect(health).toHaveBeenCalledTimes(1);
|
||||
expect(health).toHaveBeenCalledWith(domain);
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
||||
payload: { domain, status: 'invalid' },
|
||||
}));
|
||||
@@ -155,18 +133,12 @@ describe('domainsListReducer', () => {
|
||||
healthStatus,
|
||||
expectedStatus,
|
||||
) => {
|
||||
getState.mockReturnValue(fromPartial<ShlinkState>({
|
||||
selectedServer: {
|
||||
url: 'https://myerver.com',
|
||||
apiKey: '123',
|
||||
},
|
||||
}));
|
||||
health.mockResolvedValue({ status: healthStatus });
|
||||
|
||||
await checkDomainHealth(domain)(dispatch, getState, {});
|
||||
|
||||
expect(getState).toHaveBeenCalledTimes(1);
|
||||
expect(health).toHaveBeenCalledTimes(1);
|
||||
expect(health).toHaveBeenCalledWith(domain);
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
||||
payload: { domain, status: expectedStatus },
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user