From da97b7656346c9cf7776329cefac5098f6b79dbc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 3 Nov 2022 20:29:10 +0100 Subject: [PATCH] Migrated rest of domainslistreducer-related elements on test to the new ones --- src/domains/reducers/domainsList.ts | 67 +-------------------- test/domains/reducers/domainsList.test.ts | 71 +++++++++++------------ 2 files changed, 35 insertions(+), 103 deletions(-) diff --git a/src/domains/reducers/domainsList.ts b/src/domains/reducers/domainsList.ts index 4e0a88c5..b6da573c 100644 --- a/src/domains/reducers/domainsList.ts +++ b/src/domains/reducers/domainsList.ts @@ -1,10 +1,8 @@ import { createSlice, PayloadAction, createAsyncThunk, SliceCaseReducers } from '@reduxjs/toolkit'; -import { Dispatch } from 'redux'; import { AxiosError } from 'axios'; import { ShlinkDomainRedirects } from '../../api/types'; import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder'; -import { GetState, ShlinkState } from '../../container/types'; -import { ApiErrorAction } from '../../api/types/actions'; +import { ShlinkState } from '../../container/types'; import { Domain, DomainStatus } from '../data'; import { hasServerData } from '../../servers/data'; import { replaceAuthorityFromUri } from '../../utils/helpers/uri'; @@ -12,10 +10,7 @@ import { EDIT_DOMAIN_REDIRECTS, EditDomainRedirectsAction } from './domainRedire import { ProblemDetailsError } from '../../api/types/errors'; 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 FILTER_DOMAINS = 'shlink/domainsList/FILTER_DOMAINS'; export const VALIDATE_DOMAIN = 'shlink/domainsList/VALIDATE_DOMAIN'; export interface DomainsList { @@ -61,66 +56,6 @@ export const replaceRedirectsOnDomain = (domain: string, redirects: ShlinkDomain export const replaceStatusOnDomain = (domain: string, status: DomainStatus) => (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({ type: LIST_DOMAINS, payload }); - } catch (e: any) { - dispatch({ 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({ - 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({ - type: VALIDATE_DOMAIN, - payload: { domain, status: status === 'pass' ? 'valid' : 'invalid' }, - }); - } catch (e) { - dispatch({ - type: VALIDATE_DOMAIN, - payload: { domain, status: 'invalid' }, - }); - } -}; - export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => { // eslint-disable-next-line @typescript-eslint/no-shadow const listDomains = createAsyncThunk( diff --git a/test/domains/reducers/domainsList.test.ts b/test/domains/reducers/domainsList.test.ts index 7bdf2034..f6640dd8 100644 --- a/test/domains/reducers/domainsList.test.ts +++ b/test/domains/reducers/domainsList.test.ts @@ -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(), })); - 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 }, - }); + })); }); }); });