refactor(client): document and test create types utilities (#42960)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Victor Duarte
2021-07-24 11:24:08 -05:00
committed by GitHub
parent 99c16ae551
commit 10511a7402
5 changed files with 49 additions and 17 deletions

View File

@ -1,7 +1,7 @@
import { createAction, handleActions } from 'redux-actions'; import { createAction, handleActions } from 'redux-actions';
import { isEmpty } from 'lodash-es';
import { createTypes } from '../../../../utils/stateManagement'; import { createTypes } from '../../../utils/create-types';
import { createPoly } from '../../../../../utils/polyvinyl'; import { createPoly } from '../../../../../utils/polyvinyl';
import { getLines } from '../../../../../utils/get-lines'; import { getLines } from '../../../../../utils/get-lines';
import completionEpic from './completion-epic'; import completionEpic from './completion-epic';
@ -14,7 +14,6 @@ import { createCurrentChallengeSaga } from './current-challenge-saga';
import { challengeTypes } from '../../../../utils/challengeTypes'; import { challengeTypes } from '../../../../utils/challengeTypes';
import { getTargetEditor } from '../utils/getTargetEditor'; import { getTargetEditor } from '../utils/getTargetEditor';
import { completedChallengesSelector } from '../../../redux'; import { completedChallengesSelector } from '../../../redux';
import { isEmpty } from 'lodash-es';
export const ns = 'challenge'; export const ns = 'challenge';
export const backendNS = 'backendChallenge'; export const backendNS = 'backendChallenge';

View File

@ -1,5 +1,6 @@
import { createAction, handleActions } from 'redux-actions'; import { createAction, handleActions } from 'redux-actions';
import { createTypes } from '../../../../utils/stateManagement';
import { createTypes } from '../../../utils/create-types';
export const ns = 'curriculumMap'; export const ns = 'curriculumMap';

View File

@ -0,0 +1,25 @@
import { createTypes, createAsyncTypes } from './create-types';
describe('Create types utility (createTypes)', () => {
it('should generate an object with action types', () => {
const types = ['lorem', 'ipsum'];
const ns = 'namespace';
expect(createTypes(types, ns)).toEqual({
lorem: 'namespace.lorem',
ipsum: 'namespace.ipsum'
});
});
});
describe('Create async types utility (createAsyncTypes)', () => {
it('should generate a list of actions names', () => {
const action = 'action';
expect(createAsyncTypes(action)).toEqual([
'action',
'actionComplete',
'actionError'
]);
});
});

View File

@ -2,10 +2,18 @@ type CreateTypesType = {
[action: string]: string; [action: string]: string;
}; };
export function createTypes( /**
types: string[] = [], * Creates an object in which the `keys` represent the action names and the
ns = 'annon' * `values` the action type.
): CreateTypesType { * {
* action: actionType,
* ...
* }
* @param {array} types Names of the actions.
* @param {string} ns Name of the namespace.
* @returns {object} Object with action types.
*/
export function createTypes(types: string[], ns: string): CreateTypesType {
return types.reduce( return types.reduce(
(types, action: string) => ({ (types, action: string) => ({
...types, ...types,
@ -15,6 +23,14 @@ export function createTypes(
); );
} }
/**
* Creates an array with action names.
* - original
* - complete
* - error
* @param {string} action The name of the action.
* @returns {array} Names of action names.
*/
export const createAsyncTypes = (action: string): string[] => [ export const createAsyncTypes = (action: string): string[] => [
`${action}`, `${action}`,
`${action}Complete`, `${action}Complete`,

View File

@ -1,9 +0,0 @@
export function createTypes(types = [], ns = 'annon') {
return types.reduce(
(types, action) => ({
...types,
[action]: `${ns}.${action}`
}),
{}
);
}