Skip to content

Commit e135681

Browse files
committed
feat: add test helper function to create update args
1 parent faa1303 commit e135681

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -850,19 +850,20 @@ To test your **update** function you can use some helper functions in `react-elm
850850
| --- | --- |
851851
| `getOfMsgParams` | Extracts the messages out of a command |
852852
| `execCmd` | Executes the provided command and returns an array of all messages. |
853-
| `getUpdateFn` | returns an `update` function for your update map object. |
853+
| `getUpdateFn` | Returns an `update` function for your update map object. |
854+
| `createUpdateArgsFactory` | Creates a factory function to create a message, a model, and props in a test. |
854855

855856
### Testing the model and simple message commands
856857

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

861+
const createUpdateArgs = Testing.createUpdateArgsFactory(() => ({ /* initial model */ }), () => ({ /* initial props */ }));
862+
860863
...
861864
it("returns the correct model and cmd", () => {
862865
// arrange
863-
const model = // create model for test
864-
const props = // create props for test
865-
const msg = Shared.Msg.test();
866+
const [msg, model, props] = createUpdateArgs(Shared.Msg.test(), { /* optionally override model here */ }, { /* optionally override props here */ });
866867

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

899898
// mock function which is called when the "AsyncTest" message is handled
900899
const functionMock = jest.fn();

src/Testing/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,53 @@ async function execCmd<TMsg> (cmd?: Cmd<TMsg>): Promise<Nullable<TMsg> []> {
4646
return results;
4747
}
4848

49+
/**
50+
* Creates an update function out of an UpdateMap.
51+
* @param {UpdateMap<TProps, TModel, TMessage>} updateMap The UpdateMap.
52+
* @returns {(msg: TMessage, model: TModel, props: TProps) => UpdateReturnType<TModel, TMessage>} The created update function which can be used in tests.
53+
*/
4954
function getUpdateFn<TProps, TModel, TMessage extends MessageBase> (updateMap: UpdateMap<TProps, TModel, TMessage>): (msg: TMessage, model: TModel, props: TProps) => UpdateReturnType<TModel, TMessage> {
5055
return function (msg: TMessage, model: TModel, props: TProps): UpdateReturnType<TModel, TMessage> {
5156
return callUpdateMap(updateMap, msg, model, props);
5257
};
5358
}
5459

60+
type UpdateArgsFactory<TProps, TModel, TMessage extends MessageBase> = (msg: TMessage, modelTemplate?: Partial<TModel>, propsTemplate?: Partial<TProps>) => [TMessage, TModel, TProps];
61+
62+
/**
63+
* Creates a factory function to create a message, a model, and props which can be passed to an update function in tests.
64+
* @param {() => TModel} initModel A function to create an initial model.
65+
* @param {() => TProps} initProps A function to create initial props.
66+
* @returns {UpdateArgsFactory<TProps, TModel, TMessage>} A function to create a message, a model, and props.
67+
* @example
68+
* // one time
69+
* const createUpdateArgs = createUpdateArgsFactory(() => ({ ... }), () => ({ ... }));
70+
* // in tests
71+
* const [msg, model, props] = createUpdateArgs(Msg.myMessage(), { ... }, , { ... });
72+
*/
73+
function createUpdateArgsFactory <TProps, TModel, TMessage extends MessageBase> (initModel: () => TModel, initProps: () => TProps): UpdateArgsFactory<TProps, TModel, TMessage> {
74+
return function (msg: TMessage, modelTemplate?: Partial<TModel>, propsTemplate?: Partial<TProps>): [TMessage, TModel, TProps] {
75+
return [
76+
msg,
77+
{
78+
...initModel(),
79+
...modelTemplate,
80+
},
81+
{
82+
...initProps(),
83+
...propsTemplate,
84+
},
85+
];
86+
};
87+
}
88+
89+
export type {
90+
UpdateArgsFactory,
91+
};
92+
5593
export {
5694
getOfMsgParams,
5795
execCmd,
5896
getUpdateFn,
97+
createUpdateArgsFactory,
5998
};

0 commit comments

Comments
 (0)