Skip to content

Commit

Permalink
fix: pass original options to update functions in tests if not overwr…
Browse files Browse the repository at this point in the history
…itten
  • Loading branch information
atheck committed Jul 1, 2024
1 parent 7303725 commit 1f33123
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 32 deletions.
21 changes: 2 additions & 19 deletions src/Testing/createModelAndProps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { createCallBase } from "../createCallBase";
import { createDefer } from "../createDefer";
import type { InitFunction, Message, UpdateFunctionOptions } from "../Types";
import type { InitFunction, Message } from "../Types";

function createModelAndProps<TProps, TModel, TMessage extends Message>(
init: InitFunction<TProps, TModel, TMessage>,
Expand All @@ -17,19 +15,4 @@ function createModelAndProps<TProps, TModel, TMessage extends Message>(
return [{ ...model, ...modelTemplate }, props];
}

function createOptions<TProps, TModel, TMessage extends Message>(
msg: TMessage,
model: TModel,
props: TProps,
optionsTemplate?: Partial<UpdateFunctionOptions<TProps, TModel, TMessage>>,
): UpdateFunctionOptions<TProps, TModel, TMessage> {
const defer = optionsTemplate?.defer ?? createDefer<TModel, TMessage>()[0];

return {
callBase: createCallBase(msg, model, props, { defer }),
defer,
...optionsTemplate,
};
}

export { createModelAndProps, createOptions };
export { createModelAndProps };
11 changes: 4 additions & 7 deletions src/Testing/createUpdateArgsFactory.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import type { Message, UpdateFunctionOptions } from "../Types";
import { createOptions } from "./createModelAndProps";

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

type ModelAndPropsFactory<TProps, TModel> = (
modelTemplate?: Partial<TModel>,
propsTemplate?: Partial<TProps>,
) => [TModel, TProps];

/**
* Creates a factory function to create a message, a model, and props which can be passed to an update function in tests.
* Creates a factory function to create a message, a model, props, and options 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.
Expand All @@ -33,7 +32,7 @@ function createUpdateArgsFactory<TProps, TModel, TMessage extends Message>(
modelTemplate?: Partial<TModel>,
propsTemplate?: Partial<TProps>,
optionsTemplate?: Partial<UpdateFunctionOptions<TProps, TModel, TMessage>>,
): [TMessage, TModel, TProps, UpdateFunctionOptions<TProps, TModel, TMessage>] {
): [TMessage, TModel, TProps, Partial<UpdateFunctionOptions<TProps, TModel, TMessage>>?] {
const model = {
...initModel(),
...modelTemplate,
Expand All @@ -43,9 +42,7 @@ function createUpdateArgsFactory<TProps, TModel, TMessage extends Message>(
...propsTemplate,
};

const options = createOptions(msg, model, props, optionsTemplate);

return [msg, model, props, options];
return [msg, model, props, optionsTemplate];
};
}

Expand Down
11 changes: 5 additions & 6 deletions src/Testing/getCreateUpdateArgs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { InitFunction, Message, UpdateFunctionOptions } from "../Types";
import { createModelAndProps, createOptions } from "./createModelAndProps";
import { createModelAndProps } from "./createModelAndProps";
import type { ModelAndPropsFactory, UpdateArgsFactory } from "./createUpdateArgsFactory";

/**
* Creates a factory function to create a message, a model, and props which can be passed to an update function in tests.
* Creates a factory function to create a message, a model, props, and options which can be passed to an update function in tests.
* @param {InitFunction<TProps, TModel, TMessage>} init The init function which creates the 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.
Expand All @@ -22,16 +22,15 @@ function getCreateUpdateArgs<TProps, TModel, TMessage extends Message>(
modelTemplate?: Partial<TModel>,
propsTemplate?: Partial<TProps>,
optionsTemplate?: Partial<UpdateFunctionOptions<TProps, TModel, TMessage>>,
): [TMessage, TModel, TProps, UpdateFunctionOptions<TProps, TModel, TMessage>] {
): [TMessage, TModel, TProps, Partial<UpdateFunctionOptions<TProps, TModel, TMessage>>?] {
const args = createModelAndProps(init, initProps, modelTemplate, propsTemplate);
const options = createOptions(msg, ...args, optionsTemplate);

return [msg, ...args, options];
return [msg, ...args, optionsTemplate];
};
}

/**
* Creates a factory function to create a model, and props which can be passed to an update or subscription function in tests.
* Creates a factory function to create a model, props, and options which can be passed to an update or subscription function in tests.
* @param {InitFunction<TProps, TModel, TMessage>} init The init function which creates the model.
* @param {() => TProps} initProps A function to create initial props.
* @returns {ModelAndPropsFactory<TProps, TModel>} A function to create a a model and props.
Expand Down

0 comments on commit 1f33123

Please sign in to comment.