Fixed domainsListReducer test so that it works with new payload prop in actions

This commit is contained in:
Alejandro Celaya 2022-11-03 19:52:57 +01:00
parent 24483ec330
commit 88e8f3363b
2 changed files with 35 additions and 22 deletions

View file

@ -28,17 +28,21 @@ export interface DomainsList {
errorData?: ProblemDetailsError;
}
type ListDomainsAction = PayloadAction<{
interface ListDomains {
domains: Domain[];
defaultRedirects?: ShlinkDomainRedirects;
}>;
}
type ListDomainsAction = PayloadAction<ListDomains>;
type FilterDomainsAction = PayloadAction<string>;
type ValidateDomain = PayloadAction<{
interface ValidateDomain {
domain: string;
status: DomainStatus;
}>;
}
type ValidateDomainAction = PayloadAction<ValidateDomain>;
const initialState: DomainsList = {
domains: [],
@ -51,7 +55,7 @@ export type DomainsCombinedAction = ListDomainsAction
& ApiErrorAction
& FilterDomainsAction
& EditDomainRedirectsAction
& ValidateDomain;
& ValidateDomainAction;
export const replaceRedirectsOnDomain = (domain: string, redirects: ShlinkDomainRedirects) =>
(d: Domain): Domain => (d.domain !== domain ? d : { ...d, redirects });
@ -62,7 +66,7 @@ export const replaceStatusOnDomain = (domain: string, status: DomainStatus) =>
const oldReducer = buildReducer<DomainsList, DomainsCombinedAction>({
[LIST_DOMAINS_START]: () => ({ ...initialState, loading: true }),
[LIST_DOMAINS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
[LIST_DOMAINS]: (_, { payload }) => ({ ...initialState, searchTerm: payload, filteredDomains: payload.domains }),
[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())),
@ -112,7 +116,7 @@ export const checkDomainHealth = (buildShlinkApiClient: ShlinkApiClientBuilder)
const { selectedServer } = getState();
if (!hasServerData(selectedServer)) {
dispatch<ValidateDomain>({
dispatch<ValidateDomainAction>({
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' },
});
@ -129,12 +133,12 @@ export const checkDomainHealth = (buildShlinkApiClient: ShlinkApiClientBuilder)
const { status } = await health();
dispatch<ValidateDomain>({
dispatch<ValidateDomainAction>({
type: VALIDATE_DOMAIN,
payload: { domain, status: status === 'pass' ? 'valid' : 'invalid' },
});
} catch (e) {
dispatch<ValidateDomain>({
dispatch<ValidateDomainAction>({
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' },
});
@ -143,10 +147,7 @@ export const checkDomainHealth = (buildShlinkApiClient: ShlinkApiClientBuilder)
export const domainsReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const listDomains = createAsyncThunk<{
domains: Domain[];
defaultRedirects?: ShlinkDomainRedirects;
}, void, { state: ShlinkState }>(
const listDomains = createAsyncThunk<ListDomains, void, { state: ShlinkState }>(
LIST_DOMAINS,
async (_, { getState }) => {
const { listDomains: shlinkListDomains } = buildShlinkApiClient(getState);
@ -160,7 +161,7 @@ export const domainsReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuild
);
// eslint-disable-next-line @typescript-eslint/no-shadow
const checkDomainHealth = createAsyncThunk<{ domain: string; status: DomainStatus }, string, { state: ShlinkState }>(
const checkDomainHealth = createAsyncThunk<ValidateDomain, string, { state: ShlinkState }>(
VALIDATE_DOMAIN,
async (domain: string, { getState }) => {
const { selectedServer } = getState();

View file

@ -52,13 +52,13 @@ describe('domainsListReducer', () => {
});
it('returns domains on LIST_DOMAINS', () => {
expect(reducer(undefined, action(LIST_DOMAINS, { domains }))).toEqual(
expect(reducer(undefined, action(LIST_DOMAINS, { 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, { searchTerm: 'oO' }))).toEqual(
expect(reducer(Mock.of<DomainsList>({ domains }), action(FILTER_DOMAINS, { payload: 'oO' as any }))).toEqual(
{ domains, filteredDomains },
);
});
@ -90,7 +90,7 @@ describe('domainsListReducer', () => {
])('replaces status on proper domain on VALIDATE_DOMAIN', (domain) => {
expect(reducer(
Mock.of<DomainsList>({ domains, filteredDomains }),
action(VALIDATE_DOMAIN, { domain, status: 'valid' }),
action(VALIDATE_DOMAIN, { payload: { domain, status: 'valid' } } as any),
)).toEqual({
domains: domains.map(replaceStatusOnDomain(domain, 'valid')),
filteredDomains: filteredDomains.map(replaceStatusOnDomain(domain, 'valid')),
@ -117,7 +117,10 @@ describe('domainsListReducer', () => {
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_DOMAINS_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_DOMAINS, domains, defaultRedirects: undefined });
expect(dispatch).toHaveBeenNthCalledWith(2, {
type: LIST_DOMAINS,
payload: { domains },
});
expect(listDomains).toHaveBeenCalledTimes(1);
});
});
@ -128,7 +131,7 @@ describe('domainsListReducer', () => {
['bar'],
['something'],
])('creates action as expected', (searchTerm) => {
expect(filterDomainsAction(searchTerm)).toEqual({ type: FILTER_DOMAINS, searchTerm });
expect(filterDomainsAction(searchTerm)).toEqual({ type: FILTER_DOMAINS, payload: searchTerm });
});
});
@ -145,7 +148,10 @@ describe('domainsListReducer', () => {
expect(getState).toHaveBeenCalledTimes(1);
expect(health).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: VALIDATE_DOMAIN, domain, status: 'invalid' });
expect(dispatch).toHaveBeenCalledWith({
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' },
});
});
it('dispatches invalid status when health endpoint returns an error', async () => {
@ -162,7 +168,10 @@ describe('domainsListReducer', () => {
expect(getState).toHaveBeenCalledTimes(1);
expect(health).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: VALIDATE_DOMAIN, domain, status: 'invalid' });
expect(dispatch).toHaveBeenCalledWith({
type: VALIDATE_DOMAIN,
payload: { domain, status: 'invalid' },
});
});
it.each([
@ -185,7 +194,10 @@ describe('domainsListReducer', () => {
expect(getState).toHaveBeenCalledTimes(1);
expect(health).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: VALIDATE_DOMAIN, domain, status: expectedStatus });
expect(dispatch).toHaveBeenCalledWith({
type: VALIDATE_DOMAIN,
payload: { domain, status: expectedStatus },
});
});
});
});