mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Replaced domainsList old reducer with new reducer in test
This commit is contained in:
parent
88e8f3363b
commit
d25dbd5ae6
3 changed files with 34 additions and 46 deletions
|
@ -11,7 +11,6 @@ import { replaceAuthorityFromUri } from '../../utils/helpers/uri';
|
|||
import { EDIT_DOMAIN_REDIRECTS, EditDomainRedirectsAction } from './domainRedirects';
|
||||
import { ProblemDetailsError } from '../../api/types/errors';
|
||||
import { parseApiError } from '../../api/utils';
|
||||
import { buildReducer } from '../../utils/helpers/redux';
|
||||
|
||||
export const LIST_DOMAINS_START = 'shlink/domainsList/LIST_DOMAINS_START';
|
||||
export const LIST_DOMAINS_ERROR = 'shlink/domainsList/LIST_DOMAINS_ERROR';
|
||||
|
@ -52,7 +51,6 @@ const initialState: DomainsList = {
|
|||
};
|
||||
|
||||
export type DomainsCombinedAction = ListDomainsAction
|
||||
& ApiErrorAction
|
||||
& FilterDomainsAction
|
||||
& EditDomainRedirectsAction
|
||||
& ValidateDomainAction;
|
||||
|
@ -63,28 +61,6 @@ export const replaceRedirectsOnDomain = (domain: string, redirects: ShlinkDomain
|
|||
export const replaceStatusOnDomain = (domain: string, status: DomainStatus) =>
|
||||
(d: Domain): Domain => (d.domain !== domain ? d : { ...d, status });
|
||||
|
||||
const oldReducer = buildReducer<DomainsList, DomainsCombinedAction>({
|
||||
[LIST_DOMAINS_START]: () => ({ ...initialState, loading: true }),
|
||||
[LIST_DOMAINS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
||||
[LIST_DOMAINS]: (_, { payload }) => ({ ...initialState, domains: payload.domains, filteredDomains: payload.domains }),
|
||||
[FILTER_DOMAINS]: (state, { payload }) => ({
|
||||
...state,
|
||||
filteredDomains: state.domains.filter(({ domain }) => domain.toLowerCase().match(payload.toLowerCase())),
|
||||
}),
|
||||
[EDIT_DOMAIN_REDIRECTS]: (state, { domain, redirects }) => ({
|
||||
...state,
|
||||
domains: state.domains.map(replaceRedirectsOnDomain(domain, redirects)),
|
||||
filteredDomains: state.filteredDomains.map(replaceRedirectsOnDomain(domain, redirects)),
|
||||
}),
|
||||
[VALIDATE_DOMAIN]: (state, { payload }) => ({
|
||||
...state,
|
||||
domains: state.domains.map(replaceStatusOnDomain(payload.domain, payload.status)),
|
||||
filteredDomains: state.filteredDomains.map(replaceStatusOnDomain(payload.domain, payload.status)),
|
||||
}),
|
||||
}, initialState);
|
||||
|
||||
export default oldReducer;
|
||||
|
||||
export const listDomains = (buildShlinkApiClient: ShlinkApiClientBuilder) => () => async (
|
||||
dispatch: Dispatch,
|
||||
getState: GetState,
|
||||
|
@ -145,7 +121,7 @@ export const checkDomainHealth = (buildShlinkApiClient: ShlinkApiClientBuilder)
|
|||
}
|
||||
};
|
||||
|
||||
export const domainsReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => {
|
||||
export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||
const listDomains = createAsyncThunk<ListDomains, void, { state: ShlinkState }>(
|
||||
LIST_DOMAINS,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { prop } from 'ramda';
|
||||
import Bottle from 'bottlejs';
|
||||
import { ConnectDecorator } from '../../container/types';
|
||||
import { domainsReducerCreator } from '../reducers/domainsList';
|
||||
import { domainsListReducerCreator } from '../reducers/domainsList';
|
||||
import { DomainSelector } from '../DomainSelector';
|
||||
import { ManageDomains } from '../ManageDomains';
|
||||
import { editDomainRedirects } from '../reducers/domainRedirects';
|
||||
|
@ -18,14 +18,14 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
|
|||
));
|
||||
|
||||
// Reducer
|
||||
bottle.serviceFactory('domainsReducerCreator', domainsReducerCreator, 'buildShlinkApiClient');
|
||||
bottle.serviceFactory('domainsListReducer', prop('reducer'), 'domainsReducerCreator'); // TODO Improve type checks on the prop that gets picked here
|
||||
bottle.serviceFactory('domainsListReducerCreator', domainsListReducerCreator, 'buildShlinkApiClient');
|
||||
bottle.serviceFactory('domainsListReducer', prop('reducer'), 'domainsListReducerCreator'); // TODO Improve type checks on the prop that gets picked here
|
||||
|
||||
// Actions
|
||||
bottle.serviceFactory('listDomains', prop('listDomains'), 'domainsReducerCreator'); // TODO Improve type checks on the prop that gets picked here
|
||||
bottle.serviceFactory('filterDomains', prop('filterDomains'), 'domainsReducerCreator'); // TODO Improve type checks on the prop that gets picked here
|
||||
bottle.serviceFactory('listDomains', prop('listDomains'), 'domainsListReducerCreator'); // TODO Improve type checks on the prop that gets picked here
|
||||
bottle.serviceFactory('filterDomains', prop('filterDomains'), 'domainsListReducerCreator'); // TODO Improve type checks on the prop that gets picked here
|
||||
bottle.serviceFactory('editDomainRedirects', editDomainRedirects, 'buildShlinkApiClient');
|
||||
bottle.serviceFactory('checkDomainHealth', prop('checkDomainHealth'), 'domainsReducerCreator'); // TODO Improve type checks on the prop that gets picked here
|
||||
bottle.serviceFactory('checkDomainHealth', prop('checkDomainHealth'), 'domainsListReducerCreator'); // TODO Improve type checks on the prop that gets picked here
|
||||
};
|
||||
|
||||
export default provideServices;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { Mock } from 'ts-mockery';
|
||||
import reducer, {
|
||||
import { AxiosError } from 'axios';
|
||||
import {
|
||||
LIST_DOMAINS,
|
||||
LIST_DOMAINS_ERROR,
|
||||
LIST_DOMAINS_START,
|
||||
|
@ -10,8 +11,9 @@ import reducer, {
|
|||
listDomains as listDomainsAction,
|
||||
filterDomains as filterDomainsAction,
|
||||
replaceRedirectsOnDomain,
|
||||
checkDomainHealth,
|
||||
checkDomainHealth as validateDomain,
|
||||
replaceStatusOnDomain,
|
||||
domainsListReducerCreator,
|
||||
} from '../../../src/domains/reducers/domainsList';
|
||||
import { EDIT_DOMAIN_REDIRECTS } from '../../../src/domains/reducers/domainRedirects';
|
||||
import { ShlinkDomainRedirects } from '../../../src/api/types';
|
||||
|
@ -19,6 +21,7 @@ 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';
|
||||
|
||||
describe('domainsListReducer', () => {
|
||||
const dispatch = jest.fn();
|
||||
|
@ -31,6 +34,15 @@ describe('domainsListReducer', () => {
|
|||
Mock.of<Domain>({ domain: 'Boo', status: 'validating' }),
|
||||
];
|
||||
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);
|
||||
|
||||
|
@ -40,27 +52,27 @@ describe('domainsListReducer', () => {
|
|||
);
|
||||
|
||||
it('returns loading on LIST_DOMAINS_START', () => {
|
||||
expect(reducer(undefined, action(LIST_DOMAINS_START))).toEqual(
|
||||
expect(reducer(undefined, action(listDomainsThunk.pending.toString()))).toEqual(
|
||||
{ domains: [], filteredDomains: [], loading: true, error: false },
|
||||
);
|
||||
});
|
||||
|
||||
it('returns error on LIST_DOMAINS_ERROR', () => {
|
||||
expect(reducer(undefined, action(LIST_DOMAINS_ERROR))).toEqual(
|
||||
{ domains: [], filteredDomains: [], loading: false, error: true },
|
||||
expect(reducer(undefined, action(listDomainsThunk.rejected.toString(), { error } as any))).toEqual(
|
||||
{ domains: [], filteredDomains: [], loading: false, error: true, errorData: parseApiError(error as any) },
|
||||
);
|
||||
});
|
||||
|
||||
it('returns domains on LIST_DOMAINS', () => {
|
||||
expect(reducer(undefined, action(LIST_DOMAINS, { payload: { domains } } as any))).toEqual(
|
||||
{ domains, filteredDomains: domains, loading: false, error: false },
|
||||
);
|
||||
expect(
|
||||
reducer(undefined, action(listDomainsThunk.fulfilled.toString(), { payload: { domains } } as any)),
|
||||
).toEqual({ domains, filteredDomains: domains, loading: false, error: false });
|
||||
});
|
||||
|
||||
it('filters domains on FILTER_DOMAINS', () => {
|
||||
expect(reducer(Mock.of<DomainsList>({ domains }), action(FILTER_DOMAINS, { payload: 'oO' as any }))).toEqual(
|
||||
{ domains, filteredDomains },
|
||||
);
|
||||
expect(
|
||||
reducer(Mock.of<DomainsList>({ domains }), action(filterDomains.toString(), { payload: 'oO' as any })),
|
||||
).toEqual({ domains, filteredDomains });
|
||||
});
|
||||
|
||||
it.each([
|
||||
|
@ -90,7 +102,7 @@ describe('domainsListReducer', () => {
|
|||
])('replaces status on proper domain on VALIDATE_DOMAIN', (domain) => {
|
||||
expect(reducer(
|
||||
Mock.of<DomainsList>({ domains, filteredDomains }),
|
||||
action(VALIDATE_DOMAIN, { payload: { domain, status: 'valid' } } as any),
|
||||
action(checkDomainHealth.fulfilled.toString(), { payload: { domain, status: 'valid' } } as any),
|
||||
)).toEqual({
|
||||
domains: domains.map(replaceStatusOnDomain(domain, 'valid')),
|
||||
filteredDomains: filteredDomains.map(replaceStatusOnDomain(domain, 'valid')),
|
||||
|
@ -143,7 +155,7 @@ describe('domainsListReducer', () => {
|
|||
selectedServer: Mock.all<SelectedServer>(),
|
||||
}));
|
||||
|
||||
await checkDomainHealth(buildShlinkApiClient)(domain)(dispatch, getState);
|
||||
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState);
|
||||
|
||||
expect(getState).toHaveBeenCalledTimes(1);
|
||||
expect(health).not.toHaveBeenCalled();
|
||||
|
@ -163,7 +175,7 @@ describe('domainsListReducer', () => {
|
|||
}));
|
||||
health.mockRejectedValue({});
|
||||
|
||||
await checkDomainHealth(buildShlinkApiClient)(domain)(dispatch, getState);
|
||||
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState);
|
||||
|
||||
expect(getState).toHaveBeenCalledTimes(1);
|
||||
expect(health).toHaveBeenCalledTimes(1);
|
||||
|
@ -189,7 +201,7 @@ describe('domainsListReducer', () => {
|
|||
}));
|
||||
health.mockResolvedValue({ status: healthStatus });
|
||||
|
||||
await checkDomainHealth(buildShlinkApiClient)(domain)(dispatch, getState);
|
||||
await validateDomain(buildShlinkApiClient)(domain)(dispatch, getState);
|
||||
|
||||
expect(getState).toHaveBeenCalledTimes(1);
|
||||
expect(health).toHaveBeenCalledTimes(1);
|
||||
|
|
Loading…
Reference in a new issue