-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redux): add packaging options redux
- Loading branch information
1 parent
1e22c37
commit 1fa1bc5
Showing
18 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/redux/src/checkout/packaging/__tests__/reducer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as actionTypes from '../../actionTypes.js'; | ||
import reducer, * as fromReducer from '../reducer.js'; | ||
import type { PackagingOptionsState } from '../types/index.js'; | ||
|
||
let initialState: PackagingOptionsState; | ||
|
||
describe('Packaging Options reducer', () => { | ||
beforeEach(() => { | ||
initialState = fromReducer.INITIAL_STATE; | ||
}); | ||
|
||
it.each([actionTypes.FETCH_PACKAGING_OPTIONS_REQUEST])( | ||
'should handle %s action type', | ||
actionType => { | ||
expect(reducer(undefined, { type: actionType })).toEqual({ | ||
error: initialState.error, | ||
isLoading: true, | ||
result: null, | ||
}); | ||
}, | ||
); | ||
|
||
it.each([actionTypes.FETCH_PACKAGING_OPTIONS_FAILURE])( | ||
'should handle %s action type', | ||
actionType => { | ||
const error = 'foo'; | ||
const reducerResult = reducer(undefined, { | ||
payload: { error }, | ||
type: actionType, | ||
}); | ||
const expectedResult = { | ||
error, | ||
isLoading: false, | ||
result: null, | ||
}; | ||
|
||
expect(reducerResult).toEqual(expectedResult); | ||
}, | ||
); | ||
|
||
it.each([actionTypes.FETCH_PACKAGING_OPTIONS_SUCCESS])( | ||
'should handle %s action type', | ||
actionType => { | ||
const result = 'foo'; | ||
|
||
const reducerResult = reducer(undefined, { | ||
payload: { result }, | ||
type: actionType, | ||
}); | ||
const expectedResult = { | ||
error: initialState.error, | ||
isLoading: false, | ||
result, | ||
}; | ||
|
||
expect(reducerResult).toEqual(expectedResult); | ||
}, | ||
); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/redux/src/checkout/packaging/__tests__/selectors.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as packagingOptionsReducer from '../reducer.js'; | ||
import * as selectors from '../selectors.js'; | ||
import { mockPackagingOptionsState } from 'tests/__fixtures__/checkout/index.mjs'; | ||
import type { StoreState } from '../../../index.js'; | ||
|
||
describe('packaging options redux selectors', () => { | ||
beforeEach(jest.clearAllMocks); | ||
|
||
it('should get the packaging options result property from state', () => { | ||
const spy = jest.spyOn(packagingOptionsReducer, 'getPackagingOptions'); | ||
|
||
expect( | ||
selectors.getPackagingOptionsResult( | ||
mockPackagingOptionsState as StoreState, | ||
), | ||
).toEqual(mockPackagingOptionsState.packagingOptions.result); | ||
expect(spy).toHaveBeenCalledWith( | ||
mockPackagingOptionsState.packagingOptions, | ||
); | ||
}); | ||
|
||
it('should get the packaging options error property from state', () => { | ||
const spy = jest.spyOn(packagingOptionsReducer, 'getPackagingOptions'); | ||
|
||
expect( | ||
selectors.getPackagingOptionsError( | ||
mockPackagingOptionsState as StoreState, | ||
), | ||
).toEqual(mockPackagingOptionsState.packagingOptions.error); | ||
expect(spy).toHaveBeenCalledWith( | ||
mockPackagingOptionsState.packagingOptions, | ||
); | ||
}); | ||
|
||
it('should get the packaging options isLoading property from state', () => { | ||
const spy = jest.spyOn(packagingOptionsReducer, 'getPackagingOptions'); | ||
|
||
expect( | ||
selectors.isPackagingOptionsLoading( | ||
mockPackagingOptionsState as StoreState, | ||
), | ||
).toEqual(mockPackagingOptionsState.packagingOptions.isLoading); | ||
expect(spy).toHaveBeenCalledWith( | ||
mockPackagingOptionsState.packagingOptions, | ||
); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/redux/src/checkout/packaging/actions/factories/fetchPackagingOptionsFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as actionTypes from '../../../actionTypes.js'; | ||
import { | ||
type Config, | ||
type GetPackagingOptions, | ||
type GetPackagingOptionsQuery, | ||
type PackagingOption, | ||
toBlackoutError, | ||
} from '@farfetch/blackout-client'; | ||
import type { Dispatch } from 'redux'; | ||
|
||
/** | ||
* Method responsible for get all packaging options. | ||
* | ||
* @param getPackagingOptions - Get all packaging options. | ||
* | ||
* @returns Thunk factory. | ||
*/ | ||
const fetchPackagingOptionsFactory = | ||
(getPackagingOptions: GetPackagingOptions) => | ||
(query: GetPackagingOptionsQuery, config?: Config) => | ||
async (dispatch: Dispatch): Promise<PackagingOption[]> => { | ||
try { | ||
dispatch({ | ||
type: actionTypes.FETCH_PACKAGING_OPTIONS_REQUEST, | ||
}); | ||
|
||
const result = await getPackagingOptions(query, config); | ||
|
||
dispatch({ | ||
payload: result, | ||
type: actionTypes.FETCH_PACKAGING_OPTIONS_SUCCESS, | ||
}); | ||
|
||
return result; | ||
} catch (error) { | ||
const errorAsBlackoutError = toBlackoutError(error); | ||
|
||
dispatch({ | ||
payload: { error: errorAsBlackoutError }, | ||
type: actionTypes.FETCH_PACKAGING_OPTIONS_FAILURE, | ||
}); | ||
|
||
throw errorAsBlackoutError; | ||
} | ||
}; | ||
|
||
export default fetchPackagingOptionsFactory; |
5 changes: 5 additions & 0 deletions
5
packages/redux/src/checkout/packaging/actions/factories/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Packaging options actions factories. | ||
*/ | ||
|
||
export { default as fetchPackagingOptionsFactory } from './fetchPackagingOptionsFactory.js'; |
7 changes: 7 additions & 0 deletions
7
packages/redux/src/checkout/packaging/actions/fetchPackagingOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { fetchPackagingOptionsFactory } from './factories/index.js'; | ||
import { getPackagingOptions } from '@farfetch/blackout-client'; | ||
|
||
/** | ||
* Fetch all packaging options. | ||
*/ | ||
export default fetchPackagingOptionsFactory(getPackagingOptions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Packaging actions. | ||
*/ | ||
|
||
export { default as fetchPackagingOptions } from './fetchPackagingOptions.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './actions/index.js'; | ||
export * from './actions/factories/index.js'; | ||
export * from './types/index.js'; | ||
export { default as packagingOptionsReducer } from './reducer.js'; | ||
export * from './selectors.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as actionTypes from './../actionTypes.js'; | ||
import { type AnyAction, combineReducers } from 'redux'; | ||
import { createReducerWithResult } from '../../helpers/reducerFactory.js'; | ||
import type { PackagingOptionsState } from './index.js'; | ||
|
||
export const INITIAL_STATE: PackagingOptionsState = { | ||
error: null, | ||
result: null, | ||
isLoading: false, | ||
}; | ||
|
||
export const packagingOptions = createReducerWithResult( | ||
'FETCH_PACKAGING_OPTIONS', | ||
INITIAL_STATE, | ||
actionTypes, | ||
); | ||
|
||
const error = ( | ||
state = INITIAL_STATE.error, | ||
action: AnyAction, | ||
): PackagingOptionsState['error'] => { | ||
switch (action.type) { | ||
case actionTypes.FETCH_PACKAGING_OPTIONS_FAILURE: | ||
return action.payload.error; | ||
case actionTypes.FETCH_PACKAGING_OPTIONS_REQUEST: | ||
return INITIAL_STATE.error; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const isLoading = ( | ||
state = INITIAL_STATE.isLoading, | ||
action: AnyAction, | ||
): PackagingOptionsState['isLoading'] => { | ||
switch (action.type) { | ||
case actionTypes.FETCH_PACKAGING_OPTIONS_REQUEST: | ||
return true; | ||
case actionTypes.FETCH_PACKAGING_OPTIONS_SUCCESS: | ||
case actionTypes.FETCH_PACKAGING_OPTIONS_FAILURE: | ||
return INITIAL_STATE.isLoading; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const result = ( | ||
state = INITIAL_STATE.result, | ||
action: AnyAction, | ||
): PackagingOptionsState['result'] => { | ||
switch (action.type) { | ||
case actionTypes.FETCH_PACKAGING_OPTIONS_SUCCESS: | ||
return action.payload.result; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export const getPackagingOptions = ( | ||
state: PackagingOptionsState, | ||
): PackagingOptionsState => state; | ||
|
||
const packagingOptionsReducer = combineReducers({ | ||
result, | ||
error, | ||
isLoading, | ||
}); | ||
|
||
export default packagingOptionsReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { getPackagingOptions } from './reducer.js'; | ||
import type { PackagingOptionsState, StoreState } from '../../index.js'; | ||
|
||
/** | ||
* Returns the packaging options. | ||
* | ||
* @param state - Application state. | ||
* | ||
* @returns Packaging Options result. | ||
*/ | ||
export const getPackagingOptionsResult = (state: StoreState) => | ||
getPackagingOptions(state.packagingOptions as PackagingOptionsState).result; | ||
|
||
/** | ||
* Returns the loading status for the packaging options operation. | ||
* | ||
* @param state - Application state. | ||
* | ||
* @returns Packaging Options operation Loading status. | ||
*/ | ||
export const isPackagingOptionsLoading = (state: StoreState) => | ||
getPackagingOptions(state.packagingOptions as PackagingOptionsState) | ||
.isLoading; | ||
|
||
/** | ||
* Returns the error status for the packaging options operation. | ||
* | ||
* @param state - Application state. | ||
* | ||
* @returns Packaging Options operation error. | ||
*/ | ||
export const getPackagingOptionsError = (state: StoreState) => | ||
getPackagingOptions(state.packagingOptions as PackagingOptionsState).error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { BlackoutError, PackagingOption } from '@farfetch/blackout-client'; | ||
|
||
export type PackagingOptionsState = { | ||
result: PackagingOption[] | null; | ||
isLoading: boolean; | ||
error: BlackoutError | null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { schema } from 'normalizr'; | ||
|
||
export default new schema.Entity('packagingOptions'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { PackagingOption } from '@farfetch/blackout-client'; | ||
|
||
export type PackagingOptionsEntity = PackagingOption[]; |
Oops, something went wrong.