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