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

216 lines
7.5 KiB
TypeScript
Raw Normal View History

2020-11-28 14:22:52 +03:00
import { Mock } from 'ts-mockery';
import { AxiosError } from 'axios';
import {
2020-11-28 14:22:52 +03:00
LIST_DOMAINS,
LIST_DOMAINS_ERROR,
LIST_DOMAINS_START,
2021-08-22 10:00:58 +03:00
FILTER_DOMAINS,
VALIDATE_DOMAIN,
2021-08-22 10:00:58 +03:00
DomainsCombinedAction,
DomainsList,
2020-11-28 14:22:52 +03:00
listDomains as listDomainsAction,
2021-08-22 10:00:58 +03:00
filterDomains as filterDomainsAction,
replaceRedirectsOnDomain,
checkDomainHealth as validateDomain,
replaceStatusOnDomain,
domainsListReducerCreator,
2020-11-28 14:22:52 +03:00
} from '../../../src/domains/reducers/domainsList';
2021-08-22 10:00:58 +03:00
import { EDIT_DOMAIN_REDIRECTS } from '../../../src/domains/reducers/domainRedirects';
import { ShlinkDomainRedirects } from '../../../src/api/types';
2022-05-28 11:47:39 +03:00
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import { Domain } from '../../../src/domains/data';
import { ShlinkState } from '../../../src/container/types';
import { SelectedServer, ServerData } from '../../../src/servers/data';
import { parseApiError } from '../../../src/api/utils';
2020-11-28 14:22:52 +03:00
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' }),
];
2022-03-26 14:17:42 +03:00
const domains = [...filteredDomains, Mock.of<Domain>({ domain: 'bar', status: 'validating' })];
const error = Mock.of<AxiosError>({
response: {
data: { type: 'NOT_FOUND', status: 404 },
},
});
// @ts-expect-error gfreg
const { reducer, listDomains: listDomainsThunk, filterDomains, checkDomainHealth } = domainsListReducerCreator(
buildShlinkApiClient,
);
beforeEach(jest.clearAllMocks);
2020-11-28 14:22:52 +03:00
describe('reducer', () => {
2021-08-22 10:00:58 +03:00
const action = (type: string, args: Partial<DomainsCombinedAction> = {}) => Mock.of<DomainsCombinedAction>(
2020-11-28 14:22:52 +03:00
{ type, ...args },
);
it('returns loading on LIST_DOMAINS_START', () => {
expect(reducer(undefined, action(listDomainsThunk.pending.toString()))).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, action(listDomainsThunk.rejected.toString(), { error } as any))).toEqual(
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error as any) },
);
2020-11-28 14:22:52 +03:00
});
it('returns domains on LIST_DOMAINS', () => {
expect(
reducer(undefined, action(listDomainsThunk.fulfilled.toString(), { payload: { domains } } as any)),
).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(Mock.of<DomainsList>({ domains }), action(filterDomains.toString(), { payload: 'oO' as any })),
).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,
};
expect(reducer(
Mock.of<DomainsList>({ domains, filteredDomains }),
action(EDIT_DOMAIN_REDIRECTS, { domain, redirects }),
)).toEqual({
domains: domains.map(replaceRedirectsOnDomain(domain, redirects)),
filteredDomains: filteredDomains.map(replaceRedirectsOnDomain(domain, redirects)),
});
});
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(
Mock.of<DomainsList>({ domains, filteredDomains }),
action(checkDomainHealth.fulfilled.toString(), { payload: { domain, status: 'valid' } } as any),
)).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 error when loading domains fails', async () => {
listDomains.mockRejectedValue(new Error('error'));
await listDomainsAction(buildShlinkApiClient)()(dispatch, getState);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_DOMAINS_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_DOMAINS_ERROR });
expect(listDomains).toHaveBeenCalledTimes(1);
});
it('dispatches domains once loaded', async () => {
listDomains.mockResolvedValue({ data: domains });
2020-11-28 14:22:52 +03:00
await listDomainsAction(buildShlinkApiClient)()(dispatch, getState);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_DOMAINS_START });
expect(dispatch).toHaveBeenNthCalledWith(2, {
type: LIST_DOMAINS,
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(filterDomainsAction(searchTerm)).toEqual({ type: FILTER_DOMAINS, payload: 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(Mock.of<ShlinkState>({
selectedServer: Mock.all<SelectedServer>(),
}));
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState);
expect(getState).toHaveBeenCalledTimes(1);
expect(health).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' },
});
});
it('dispatches invalid status when health endpoint returns an error', async () => {
getState.mockReturnValue(Mock.of<ShlinkState>({
selectedServer: Mock.of<ServerData>({
url: 'https://myerver.com',
apiKey: '123',
}),
}));
health.mockRejectedValue({});
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState);
expect(getState).toHaveBeenCalledTimes(1);
expect(health).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({
type: VALIDATE_DOMAIN,
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(Mock.of<ShlinkState>({
selectedServer: Mock.of<ServerData>({
url: 'https://myerver.com',
apiKey: '123',
}),
}));
health.mockResolvedValue({ status: healthStatus });
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState);
expect(getState).toHaveBeenCalledTimes(1);
expect(health).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({
type: VALIDATE_DOMAIN,
payload: { domain, status: expectedStatus },
});
});
});
2020-11-28 14:22:52 +03:00
});