diff --git a/src/modules/authorization/reducer.spec.ts b/src/modules/authorization/reducer.spec.ts new file mode 100644 index 00000000..e35a66fe --- /dev/null +++ b/src/modules/authorization/reducer.spec.ts @@ -0,0 +1,83 @@ +import { ChainId } from '@dcl/schemas' +import { + FETCH_AUTHORIZATIONS_REQUEST, + GRANT_TOKEN_REQUEST, + GRANT_TOKEN_SUCCESS, + REVOKE_TOKEN_REQUEST, + REVOKE_TOKEN_SUCCESS, + fetchAuthorizationsRequest, + revokeTokenSuccess, + revokeTokenRequest, + grantTokenRequest, + grantTokenSuccess +} from './actions' +import { authorizationReducer, INITIAL_STATE } from './reducer' +import { Authorization } from './types' + +describe.each([ + { + type: GRANT_TOKEN_REQUEST, + action: grantTokenRequest({} as Authorization), + addLoading: true + }, + { + type: REVOKE_TOKEN_REQUEST, + action: revokeTokenRequest({} as Authorization), + addLoading: true + }, + { + type: GRANT_TOKEN_SUCCESS, + action: grantTokenSuccess( + {} as Authorization, + ChainId.ETHEREUM_GOERLI, + 'tsx' + ) + }, + { + type: REVOKE_TOKEN_SUCCESS, + action: revokeTokenSuccess( + {} as Authorization, + ChainId.ETHEREUM_GOERLI, + 'tsx' + ) + }, + { + type: FETCH_AUTHORIZATIONS_REQUEST, + action: fetchAuthorizationsRequest([]), + addLoading: true + } +])('when handling $type action', ({ action, addLoading }) => { + it('should set error as null', () => { + const initialStateWithError = { + ...INITIAL_STATE, + error: 'Something went wrong' + } + expect(authorizationReducer(initialStateWithError, action)).toEqual( + expect.objectContaining({ + error: null + }) + ) + }) + + if (addLoading) { + it('should add action to loading array', () => { + expect(authorizationReducer(INITIAL_STATE, action)).toEqual( + expect.objectContaining({ + loading: [action] + }) + ) + }) + } else { + it('should remove action from loading array', () => { + const initialStateWithLoading = { + ...INITIAL_STATE, + loading: [action] + } + expect(authorizationReducer(initialStateWithLoading, action)).toEqual( + expect.objectContaining({ + loading: [] + }) + ) + }) + } +}) diff --git a/src/modules/authorization/reducer.ts b/src/modules/authorization/reducer.ts index fad17bc2..4cfa5b80 100644 --- a/src/modules/authorization/reducer.ts +++ b/src/modules/authorization/reducer.ts @@ -32,7 +32,7 @@ export type AuthorizationState = { error: string | null } -const INITIAL_STATE = { +export const INITIAL_STATE = { data: [], loading: [], error: null @@ -62,6 +62,7 @@ export function authorizationReducer( case FETCH_AUTHORIZATIONS_REQUEST: { return { ...state, + error: null, loading: loadingReducer(state.loading, action) } }