shlink-web-client/test/domains/reducers/domainsList.test.ts

176 lines
6.2 KiB
TypeScript
Raw Normal View History

import { fromPartial } from '@total-typescript/shoehorn';
2023-02-18 13:11:01 +03:00
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkDomainRedirects } from '../../../src/api/types';
import { parseApiError } from '../../../src/api/utils';
import type { ShlinkState } from '../../../src/container/types';
import type { Domain } from '../../../src/domains/data';
import type { EditDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
2023-02-18 13:11:01 +03:00
import { editDomainRedirects } from '../../../src/domains/reducers/domainRedirects';
import {
2023-02-18 13:11:01 +03:00
domainsListReducerCreator,
2021-08-22 10:00:58 +03:00
replaceRedirectsOnDomain,
replaceStatusOnDomain,
2020-11-28 14:22:52 +03:00
} from '../../../src/domains/reducers/domainsList';
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();
const buildShlinkApiClient = () => 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 { reducer, listDomains: listDomainsAction, checkDomainHealth, filterDomains } = domainsListReducerCreator(
buildShlinkApiClient,
editDomainRedirectsThunk,
);
2020-11-28 14:22:52 +03:00
describe('reducer', () => {
it('returns loading on LIST_DOMAINS_START', () => {
expect(reducer(undefined, listDomainsAction.pending(''))).toEqual(
{ domains: [], filteredDomains: [], loading: true, error: false },
);
2020-11-28 14:22:52 +03:00
});
it('returns error on LIST_DOMAINS_ERROR', () => {
expect(reducer(undefined, listDomainsAction.rejected(error, ''))).toEqual(
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error) },
);
2020-11-28 14:22:52 +03:00
});
it('returns domains on LIST_DOMAINS', () => {
expect(
reducer(undefined, listDomainsAction.fulfilled({ domains }, '')),
).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', () => {
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,
};
const editDomainRedirects: EditDomainRedirects = { domain, redirects };
2021-08-22 10:00:58 +03:00
expect(reducer(
fromPartial({ domains, filteredDomains }),
editDomainRedirectsThunk.fulfilled(editDomainRedirects, '', editDomainRedirects),
)).toEqual({
domains: domains.map(replaceRedirectsOnDomain(editDomainRedirects)),
filteredDomains: filteredDomains.map(replaceRedirectsOnDomain(editDomainRedirects)),
2021-08-22 10:00:58 +03:00
});
});
it.each([
2022-03-26 14:17:42 +03:00
['foo'],
['bar'],
['does_not_exist'],
])('replaces status on proper domain on VALIDATE_DOMAIN', (domain) => {
expect(reducer(
fromPartial({ domains, filteredDomains }),
checkDomainHealth.fulfilled({ domain, status: 'valid' }, '', ''),
)).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 () => {
listDomains.mockResolvedValue({ data: domains });
2020-11-28 14:22:52 +03:00
await listDomainsAction()(dispatch, getState, {});
2020-11-28 14:22:52 +03:00
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
payload: { domains },
}));
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) => {
expect(filterDomains(searchTerm).payload).toEqual(searchTerm);
2021-08-22 10:00:58 +03:00
});
});
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(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
payload: { domain, status: 'invalid' },
}));
});
it.each([
2022-03-26 14:17:42 +03:00
['pass', 'valid'],
['fail', 'invalid'],
])('dispatches proper status based on status returned from health endpoint', async (
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(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
payload: { domain, status: expectedStatus },
}));
});
});
2020-11-28 14:22:52 +03:00
});