2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-04 12:16:01 +03:00
|
|
|
import type { ShlinkApiClient, ShlinkDomainRedirects } from '../../../src/api-contract';
|
|
|
|
import { parseApiError } from '../../../src/api-contract/utils';
|
2023-08-02 10:01:44 +03:00
|
|
|
import type { Domain } from '../../../src/domains/data';
|
|
|
|
import type { EditDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
|
|
|
import { editDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
|
2022-11-03 22:15:28 +03:00
|
|
|
import {
|
2023-02-18 13:11:01 +03:00
|
|
|
domainsListReducerCreator,
|
2021-08-22 10:00:58 +03:00
|
|
|
replaceRedirectsOnDomain,
|
2021-12-26 15:38:17 +03:00
|
|
|
replaceStatusOnDomain,
|
2023-08-02 10:01:44 +03:00
|
|
|
} from '../../../src/domains/reducers/domainsList';
|
2020-11-28 14:22:52 +03:00
|
|
|
|
2021-12-09 15:44:29 +03:00
|
|
|
describe('domainsListReducer', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const dispatch = vi.fn();
|
|
|
|
const getState = vi.fn();
|
|
|
|
const listDomains = vi.fn();
|
|
|
|
const health = vi.fn();
|
2023-08-04 12:16:01 +03:00
|
|
|
const apiClientFactory = () => fromPartial<ShlinkApiClient>({ listDomains, health });
|
2023-04-13 22:48:29 +03:00
|
|
|
const filteredDomains: Domain[] = [
|
|
|
|
fromPartial({ domain: 'foo', status: 'validating' }),
|
|
|
|
fromPartial({ domain: 'Boo', status: 'validating' }),
|
2021-12-26 15:38:17 +03:00
|
|
|
];
|
2023-04-13 22:48:29 +03:00
|
|
|
const domains: Domain[] = [...filteredDomains, fromPartial({ domain: 'bar', status: 'validating' })];
|
2023-03-18 14:09:38 +03:00
|
|
|
const error = { type: 'NOT_FOUND', status: 404 } as unknown as Error;
|
2023-08-04 12:16:01 +03:00
|
|
|
const editDomainRedirectsThunk = editDomainRedirects(apiClientFactory);
|
2022-11-03 22:29:10 +03:00
|
|
|
const { reducer, listDomains: listDomainsAction, checkDomainHealth, filterDomains } = domainsListReducerCreator(
|
2023-08-04 12:16:01 +03:00
|
|
|
apiClientFactory,
|
2022-11-04 20:56:34 +03:00
|
|
|
editDomainRedirectsThunk,
|
2022-11-03 22:15:28 +03:00
|
|
|
);
|
2021-12-26 15:38:17 +03:00
|
|
|
|
2020-11-28 14:22:52 +03:00
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on LIST_DOMAINS_START', () => {
|
2023-08-04 12:16:01 +03:00
|
|
|
expect(reducer(undefined, listDomainsAction.pending('', {}))).toEqual(
|
2021-08-21 18:53:06 +03:00
|
|
|
{ domains: [], filteredDomains: [], loading: true, error: false },
|
|
|
|
);
|
2020-11-28 14:22:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error on LIST_DOMAINS_ERROR', () => {
|
2023-08-04 12:16:01 +03:00
|
|
|
expect(reducer(undefined, listDomainsAction.rejected(error, '', {}))).toEqual(
|
2022-11-04 18:50:03 +03:00
|
|
|
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error) },
|
2021-08-21 18:53:06 +03:00
|
|
|
);
|
2020-11-28 14:22:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns domains on LIST_DOMAINS', () => {
|
2022-11-03 22:15:28 +03:00
|
|
|
expect(
|
2023-08-04 12:16:01 +03:00
|
|
|
reducer(undefined, listDomainsAction.fulfilled({ domains }, '', {})),
|
2022-11-03 22:15:28 +03:00
|
|
|
).toEqual({ domains, filteredDomains: domains, loading: false, error: false });
|
2020-11-28 14:22:52 +03:00
|
|
|
});
|
2021-08-22 10:00:58 +03:00
|
|
|
|
|
|
|
it('filters domains on FILTER_DOMAINS', () => {
|
2023-04-13 22:48:29 +03:00
|
|
|
expect(reducer(fromPartial({ domains }), filterDomains('oO'))).toEqual({ domains, filteredDomains });
|
2021-08-22 10:00:58 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['foo'],
|
|
|
|
['bar'],
|
|
|
|
['does_not_exist'],
|
2021-08-22 10:00:58 +03:00
|
|
|
])('replaces redirects on proper domain on EDIT_DOMAIN_REDIRECTS', (domain) => {
|
|
|
|
const redirects: ShlinkDomainRedirects = {
|
|
|
|
baseUrlRedirect: 'bar',
|
|
|
|
regular404Redirect: 'foo',
|
|
|
|
invalidShortUrlRedirect: null,
|
|
|
|
};
|
2023-03-18 14:09:38 +03:00
|
|
|
const editDomainRedirects: EditDomainRedirects = { domain, redirects };
|
2021-08-22 10:00:58 +03:00
|
|
|
|
2023-03-18 14:09:38 +03:00
|
|
|
expect(reducer(
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial({ domains, filteredDomains }),
|
2023-03-18 14:09:38 +03:00
|
|
|
editDomainRedirectsThunk.fulfilled(editDomainRedirects, '', editDomainRedirects),
|
|
|
|
)).toEqual({
|
|
|
|
domains: domains.map(replaceRedirectsOnDomain(editDomainRedirects)),
|
|
|
|
filteredDomains: filteredDomains.map(replaceRedirectsOnDomain(editDomainRedirects)),
|
2021-08-22 10:00:58 +03:00
|
|
|
});
|
|
|
|
});
|
2021-12-26 15:38:17 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['foo'],
|
|
|
|
['bar'],
|
|
|
|
['does_not_exist'],
|
2021-12-26 15:38:17 +03:00
|
|
|
])('replaces status on proper domain on VALIDATE_DOMAIN', (domain) => {
|
|
|
|
expect(reducer(
|
2023-04-13 22:48:29 +03:00
|
|
|
fromPartial({ domains, filteredDomains }),
|
2023-03-18 14:09:38 +03:00
|
|
|
checkDomainHealth.fulfilled({ domain, status: 'valid' }, '', ''),
|
2021-12-26 15:38:17 +03:00
|
|
|
)).toEqual({
|
|
|
|
domains: domains.map(replaceStatusOnDomain(domain, 'valid')),
|
|
|
|
filteredDomains: filteredDomains.map(replaceStatusOnDomain(domain, 'valid')),
|
|
|
|
});
|
|
|
|
});
|
2020-11-28 14:22:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('listDomains', () => {
|
|
|
|
it('dispatches domains once loaded', async () => {
|
2021-12-09 15:16:28 +03:00
|
|
|
listDomains.mockResolvedValue({ data: domains });
|
2020-11-28 14:22:52 +03:00
|
|
|
|
2023-08-04 12:16:01 +03:00
|
|
|
await listDomainsAction({})(dispatch, getState, {});
|
2020-11-28 14:22:52 +03:00
|
|
|
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
2023-03-18 14:09:38 +03:00
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
2022-11-03 21:52:57 +03:00
|
|
|
payload: { domains },
|
2022-11-03 22:29:10 +03:00
|
|
|
}));
|
2020-11-28 14:22:52 +03:00
|
|
|
expect(listDomains).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
2021-08-22 10:00:58 +03:00
|
|
|
|
|
|
|
describe('filterDomains', () => {
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['foo'],
|
|
|
|
['bar'],
|
|
|
|
['something'],
|
2021-08-22 10:00:58 +03:00
|
|
|
])('creates action as expected', (searchTerm) => {
|
2023-03-18 14:09:38 +03:00
|
|
|
expect(filterDomains(searchTerm).payload).toEqual(searchTerm);
|
2021-08-22 10:00:58 +03:00
|
|
|
});
|
|
|
|
});
|
2021-12-26 15:38:17 +03:00
|
|
|
|
|
|
|
describe('checkDomainHealth', () => {
|
|
|
|
const domain = 'example.com';
|
|
|
|
|
|
|
|
it('dispatches invalid status when health endpoint returns an error', async () => {
|
|
|
|
health.mockRejectedValue({});
|
|
|
|
|
2022-11-03 22:29:10 +03:00
|
|
|
await checkDomainHealth(domain)(dispatch, getState, {});
|
2021-12-26 15:38:17 +03:00
|
|
|
|
|
|
|
expect(health).toHaveBeenCalledTimes(1);
|
2023-08-04 12:16:01 +03:00
|
|
|
expect(health).toHaveBeenCalledWith(domain);
|
2022-11-03 22:29:10 +03:00
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
2022-11-03 21:52:57 +03:00
|
|
|
payload: { domain, status: 'invalid' },
|
2022-11-03 22:29:10 +03:00
|
|
|
}));
|
2021-12-26 15:38:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
['pass', 'valid'],
|
|
|
|
['fail', 'invalid'],
|
2021-12-26 15:38:17 +03:00
|
|
|
])('dispatches proper status based on status returned from health endpoint', async (
|
|
|
|
healthStatus,
|
|
|
|
expectedStatus,
|
|
|
|
) => {
|
|
|
|
health.mockResolvedValue({ status: healthStatus });
|
|
|
|
|
2022-11-03 22:29:10 +03:00
|
|
|
await checkDomainHealth(domain)(dispatch, getState, {});
|
2021-12-26 15:38:17 +03:00
|
|
|
|
|
|
|
expect(health).toHaveBeenCalledTimes(1);
|
2023-08-04 12:16:01 +03:00
|
|
|
expect(health).toHaveBeenCalledWith(domain);
|
2022-11-03 22:29:10 +03:00
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
2022-11-03 21:52:57 +03:00
|
|
|
payload: { domain, status: expectedStatus },
|
2022-11-03 22:29:10 +03:00
|
|
|
}));
|
2021-12-26 15:38:17 +03:00
|
|
|
});
|
|
|
|
});
|
2020-11-28 14:22:52 +03:00
|
|
|
});
|