Skip to content

Commit

Permalink
fix: reset error on request (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
Melisa Anabella Rossi authored Apr 28, 2023
1 parent 0fa1c37 commit c919fb1
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
83 changes: 83 additions & 0 deletions src/modules/authorization/reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -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: []
})
)
})
}
})
3 changes: 2 additions & 1 deletion src/modules/authorization/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type AuthorizationState = {
error: string | null
}

const INITIAL_STATE = {
export const INITIAL_STATE = {
data: [],
loading: [],
error: null
Expand Down Expand Up @@ -62,6 +62,7 @@ export function authorizationReducer(
case FETCH_AUTHORIZATIONS_REQUEST: {
return {
...state,
error: null,
loading: loadingReducer(state.loading, action)
}
}
Expand Down

0 comments on commit c919fb1

Please sign in to comment.