Migrated rest of domainslistreducer-related elements on test to the new ones

This commit is contained in:
Alejandro Celaya 2022-11-03 20:29:10 +01:00
parent d25dbd5ae6
commit da97b76563
2 changed files with 35 additions and 103 deletions

View file

@ -1,10 +1,8 @@
import { createSlice, PayloadAction, createAsyncThunk, SliceCaseReducers } from '@reduxjs/toolkit'; import { createSlice, PayloadAction, createAsyncThunk, SliceCaseReducers } from '@reduxjs/toolkit';
import { Dispatch } from 'redux';
import { AxiosError } from 'axios'; import { AxiosError } from 'axios';
import { ShlinkDomainRedirects } from '../../api/types'; import { ShlinkDomainRedirects } from '../../api/types';
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
import { GetState, ShlinkState } from '../../container/types'; import { ShlinkState } from '../../container/types';
import { ApiErrorAction } from '../../api/types/actions';
import { Domain, DomainStatus } from '../data'; import { Domain, DomainStatus } from '../data';
import { hasServerData } from '../../servers/data'; import { hasServerData } from '../../servers/data';
import { replaceAuthorityFromUri } from '../../utils/helpers/uri'; import { replaceAuthorityFromUri } from '../../utils/helpers/uri';
@ -12,10 +10,7 @@ import { EDIT_DOMAIN_REDIRECTS, EditDomainRedirectsAction } from './domainRedire
import { ProblemDetailsError } from '../../api/types/errors'; import { ProblemDetailsError } from '../../api/types/errors';
import { parseApiError } from '../../api/utils'; import { parseApiError } from '../../api/utils';
export const LIST_DOMAINS_START = 'shlink/domainsList/LIST_DOMAINS_START';
export const LIST_DOMAINS_ERROR = 'shlink/domainsList/LIST_DOMAINS_ERROR';
export const LIST_DOMAINS = 'shlink/domainsList/LIST_DOMAINS'; export const LIST_DOMAINS = 'shlink/domainsList/LIST_DOMAINS';
export const FILTER_DOMAINS = 'shlink/domainsList/FILTER_DOMAINS';
export const VALIDATE_DOMAIN = 'shlink/domainsList/VALIDATE_DOMAIN'; export const VALIDATE_DOMAIN = 'shlink/domainsList/VALIDATE_DOMAIN';
export interface DomainsList { export interface DomainsList {
@ -61,66 +56,6 @@ export const replaceRedirectsOnDomain = (domain: string, redirects: ShlinkDomain
export const replaceStatusOnDomain = (domain: string, status: DomainStatus) => export const replaceStatusOnDomain = (domain: string, status: DomainStatus) =>
(d: Domain): Domain => (d.domain !== domain ? d : { ...d, status }); (d: Domain): Domain => (d.domain !== domain ? d : { ...d, status });
export const listDomains = (buildShlinkApiClient: ShlinkApiClientBuilder) => () => async (
dispatch: Dispatch,
getState: GetState,
) => {
dispatch({ type: LIST_DOMAINS_START });
const { listDomains: shlinkListDomains } = buildShlinkApiClient(getState);
try {
const payload = await shlinkListDomains().then(({ data, defaultRedirects }) => ({
domains: data.map((domain): Domain => ({ ...domain, status: 'validating' })),
defaultRedirects,
}));
dispatch<ListDomainsAction>({ type: LIST_DOMAINS, payload });
} catch (e: any) {
dispatch<ApiErrorAction>({ type: LIST_DOMAINS_ERROR, errorData: parseApiError(e) });
}
};
export const filterDomains = (searchTerm: string): FilterDomainsAction => ({
type: FILTER_DOMAINS,
payload: searchTerm,
});
export const checkDomainHealth = (buildShlinkApiClient: ShlinkApiClientBuilder) => (domain: string) => async (
dispatch: Dispatch,
getState: GetState,
) => {
const { selectedServer } = getState();
if (!hasServerData(selectedServer)) {
dispatch<ValidateDomainAction>({
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' },
});
return;
}
try {
const { url, ...rest } = selectedServer;
const { health } = buildShlinkApiClient({
...rest,
url: replaceAuthorityFromUri(url, domain),
});
const { status } = await health();
dispatch<ValidateDomainAction>({
type: VALIDATE_DOMAIN,
payload: { domain, status: status === 'pass' ? 'valid' : 'invalid' },
});
} catch (e) {
dispatch<ValidateDomainAction>({
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' },
});
}
};
export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => { export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => {
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
const listDomains = createAsyncThunk<ListDomains, void, { state: ShlinkState }>( const listDomains = createAsyncThunk<ListDomains, void, { state: ShlinkState }>(

View file

@ -1,17 +1,9 @@
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import { AxiosError } from 'axios'; import { AxiosError } from 'axios';
import { import {
LIST_DOMAINS,
LIST_DOMAINS_ERROR,
LIST_DOMAINS_START,
FILTER_DOMAINS,
VALIDATE_DOMAIN,
DomainsCombinedAction, DomainsCombinedAction,
DomainsList, DomainsList,
listDomains as listDomainsAction,
filterDomains as filterDomainsAction,
replaceRedirectsOnDomain, replaceRedirectsOnDomain,
checkDomainHealth as validateDomain,
replaceStatusOnDomain, replaceStatusOnDomain,
domainsListReducerCreator, domainsListReducerCreator,
} from '../../../src/domains/reducers/domainsList'; } from '../../../src/domains/reducers/domainsList';
@ -39,8 +31,8 @@ describe('domainsListReducer', () => {
data: { type: 'NOT_FOUND', status: 404 }, data: { type: 'NOT_FOUND', status: 404 },
}, },
}); });
// @ts-expect-error gfreg // @ts-expect-error filterDomains is actually part of the result
const { reducer, listDomains: listDomainsThunk, filterDomains, checkDomainHealth } = domainsListReducerCreator( const { reducer, listDomains: listDomainsAction, checkDomainHealth, filterDomains } = domainsListReducerCreator(
buildShlinkApiClient, buildShlinkApiClient,
); );
@ -52,20 +44,20 @@ describe('domainsListReducer', () => {
); );
it('returns loading on LIST_DOMAINS_START', () => { it('returns loading on LIST_DOMAINS_START', () => {
expect(reducer(undefined, action(listDomainsThunk.pending.toString()))).toEqual( expect(reducer(undefined, action(listDomainsAction.pending.toString()))).toEqual(
{ domains: [], filteredDomains: [], loading: true, error: false }, { domains: [], filteredDomains: [], loading: true, error: false },
); );
}); });
it('returns error on LIST_DOMAINS_ERROR', () => { it('returns error on LIST_DOMAINS_ERROR', () => {
expect(reducer(undefined, action(listDomainsThunk.rejected.toString(), { error } as any))).toEqual( expect(reducer(undefined, action(listDomainsAction.rejected.toString(), { error } as any))).toEqual(
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error as any) }, { domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error as any) },
); );
}); });
it('returns domains on LIST_DOMAINS', () => { it('returns domains on LIST_DOMAINS', () => {
expect( expect(
reducer(undefined, action(listDomainsThunk.fulfilled.toString(), { payload: { domains } } as any)), reducer(undefined, action(listDomainsAction.fulfilled.toString(), { payload: { domains } } as any)),
).toEqual({ domains, filteredDomains: domains, loading: false, error: false }); ).toEqual({ domains, filteredDomains: domains, loading: false, error: false });
}); });
@ -114,25 +106,31 @@ describe('domainsListReducer', () => {
it('dispatches error when loading domains fails', async () => { it('dispatches error when loading domains fails', async () => {
listDomains.mockRejectedValue(new Error('error')); listDomains.mockRejectedValue(new Error('error'));
await listDomainsAction(buildShlinkApiClient)()(dispatch, getState); await listDomainsAction()(dispatch, getState, {});
expect(dispatch).toHaveBeenCalledTimes(2); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_DOMAINS_START }); expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_DOMAINS_ERROR }); type: listDomainsAction.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: listDomainsAction.rejected.toString(),
}));
expect(listDomains).toHaveBeenCalledTimes(1); expect(listDomains).toHaveBeenCalledTimes(1);
}); });
it('dispatches domains once loaded', async () => { it('dispatches domains once loaded', async () => {
listDomains.mockResolvedValue({ data: domains }); listDomains.mockResolvedValue({ data: domains });
await listDomainsAction(buildShlinkApiClient)()(dispatch, getState); await listDomainsAction()(dispatch, getState, {});
expect(dispatch).toHaveBeenCalledTimes(2); expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_DOMAINS_START }); expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
expect(dispatch).toHaveBeenNthCalledWith(2, { type: listDomainsAction.pending.toString(),
type: LIST_DOMAINS, }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: listDomainsAction.fulfilled.toString(),
payload: { domains }, payload: { domains },
}); }));
expect(listDomains).toHaveBeenCalledTimes(1); expect(listDomains).toHaveBeenCalledTimes(1);
}); });
}); });
@ -143,7 +141,9 @@ describe('domainsListReducer', () => {
['bar'], ['bar'],
['something'], ['something'],
])('creates action as expected', (searchTerm) => { ])('creates action as expected', (searchTerm) => {
expect(filterDomainsAction(searchTerm)).toEqual({ type: FILTER_DOMAINS, payload: searchTerm }); expect(filterDomains(searchTerm)).toEqual(
expect.objectContaining({ type: filterDomains.toString(), payload: searchTerm }),
);
}); });
}); });
@ -155,15 +155,14 @@ describe('domainsListReducer', () => {
selectedServer: Mock.all<SelectedServer>(), selectedServer: Mock.all<SelectedServer>(),
})); }));
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState); await checkDomainHealth(domain)(dispatch, getState, {});
expect(getState).toHaveBeenCalledTimes(1); expect(getState).toHaveBeenCalledTimes(1);
expect(health).not.toHaveBeenCalled(); expect(health).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledTimes(1); expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
expect(dispatch).toHaveBeenCalledWith({ type: checkDomainHealth.fulfilled.toString(),
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' }, payload: { domain, status: 'invalid' },
}); }));
}); });
it('dispatches invalid status when health endpoint returns an error', async () => { it('dispatches invalid status when health endpoint returns an error', async () => {
@ -175,15 +174,14 @@ describe('domainsListReducer', () => {
})); }));
health.mockRejectedValue({}); health.mockRejectedValue({});
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState); await checkDomainHealth(domain)(dispatch, getState, {});
expect(getState).toHaveBeenCalledTimes(1); expect(getState).toHaveBeenCalledTimes(1);
expect(health).toHaveBeenCalledTimes(1); expect(health).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(1); expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
expect(dispatch).toHaveBeenCalledWith({ type: checkDomainHealth.fulfilled.toString(),
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' }, payload: { domain, status: 'invalid' },
}); }));
}); });
it.each([ it.each([
@ -201,15 +199,14 @@ describe('domainsListReducer', () => {
})); }));
health.mockResolvedValue({ status: healthStatus }); health.mockResolvedValue({ status: healthStatus });
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState); await checkDomainHealth(domain)(dispatch, getState, {});
expect(getState).toHaveBeenCalledTimes(1); expect(getState).toHaveBeenCalledTimes(1);
expect(health).toHaveBeenCalledTimes(1); expect(health).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(1); expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
expect(dispatch).toHaveBeenCalledWith({ type: checkDomainHealth.fulfilled.toString(),
type: VALIDATE_DOMAIN,
payload: { domain, status: expectedStatus }, payload: { domain, status: expectedStatus },
}); }));
}); });
}); });
}); });