Skip to content

Commit

Permalink
feat: add test helper function to create update args
Browse files Browse the repository at this point in the history
  • Loading branch information
atheck committed Jul 29, 2022
1 parent faa1303 commit e135681
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -850,19 +850,20 @@ To test your **update** function you can use some helper functions in `react-elm
| --- | --- |
| `getOfMsgParams` | Extracts the messages out of a command |
| `execCmd` | Executes the provided command and returns an array of all messages. |
| `getUpdateFn` | returns an `update` function for your update map object. |
| `getUpdateFn` | Returns an `update` function for your update map object. |
| `createUpdateArgsFactory` | Creates a factory function to create a message, a model, and props in a test. |

### Testing the model and simple message commands

```ts
import * as Testing from "react-elmish/dist/Testing";

const createUpdateArgs = Testing.createUpdateArgsFactory(() => ({ /* initial model */ }), () => ({ /* initial props */ }));

...
it("returns the correct model and cmd", () => {
// arrange
const model = // create model for test
const props = // create props for test
const msg = Shared.Msg.test();
const [msg, model, props] = createUpdateArgs(Shared.Msg.test(), { /* optionally override model here */ }, { /* optionally override props here */ });

const expectedValue = // what you expect in the model
const expectedCmds = [
Expand Down Expand Up @@ -892,9 +893,7 @@ import * as Testing from "react-elmish/dist/Testing";
...
it("returns the correct cmd", () => {
// arrange
const model = { /* create model */ };
const props = { /* create props */ };
const msg = Shared.Msg.asyncTest();
const [msg, model, props] = createUpdateArgs(Shared.Msg.asyncTest());

// mock function which is called when the "AsyncTest" message is handled
const functionMock = jest.fn();
Expand Down
39 changes: 39 additions & 0 deletions src/Testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,53 @@ async function execCmd<TMsg> (cmd?: Cmd<TMsg>): Promise<Nullable<TMsg> []> {
return results;
}

/**
* Creates an update function out of an UpdateMap.
* @param {UpdateMap<TProps, TModel, TMessage>} updateMap The UpdateMap.
* @returns {(msg: TMessage, model: TModel, props: TProps) => UpdateReturnType<TModel, TMessage>} The created update function which can be used in tests.
*/
function getUpdateFn<TProps, TModel, TMessage extends MessageBase> (updateMap: UpdateMap<TProps, TModel, TMessage>): (msg: TMessage, model: TModel, props: TProps) => UpdateReturnType<TModel, TMessage> {
return function (msg: TMessage, model: TModel, props: TProps): UpdateReturnType<TModel, TMessage> {
return callUpdateMap(updateMap, msg, model, props);
};
}

type UpdateArgsFactory<TProps, TModel, TMessage extends MessageBase> = (msg: TMessage, modelTemplate?: Partial<TModel>, propsTemplate?: Partial<TProps>) => [TMessage, TModel, TProps];

/**
* Creates a factory function to create a message, a model, and props which can be passed to an update function in tests.
* @param {() => TModel} initModel A function to create an initial model.
* @param {() => TProps} initProps A function to create initial props.
* @returns {UpdateArgsFactory<TProps, TModel, TMessage>} A function to create a message, a model, and props.
* @example
* // one time
* const createUpdateArgs = createUpdateArgsFactory(() => ({ ... }), () => ({ ... }));
* // in tests
* const [msg, model, props] = createUpdateArgs(Msg.myMessage(), { ... }, , { ... });
*/
function createUpdateArgsFactory <TProps, TModel, TMessage extends MessageBase> (initModel: () => TModel, initProps: () => TProps): UpdateArgsFactory<TProps, TModel, TMessage> {
return function (msg: TMessage, modelTemplate?: Partial<TModel>, propsTemplate?: Partial<TProps>): [TMessage, TModel, TProps] {
return [
msg,
{
...initModel(),
...modelTemplate,
},
{
...initProps(),
...propsTemplate,
},
];
};
}

export type {
UpdateArgsFactory,
};

export {
getOfMsgParams,
execCmd,
getUpdateFn,
createUpdateArgsFactory,
};

0 comments on commit e135681

Please sign in to comment.