From af84323bb6cf93d31adc56d1203f9c8a0fb4c710 Mon Sep 17 00:00:00 2001 From: Matthijs Groen Date: Fri, 21 Jun 2024 16:56:26 +0200 Subject: [PATCH] allow mutation for updateProps as well --- src/support/testing/testSubject.tsx | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/support/testing/testSubject.tsx b/src/support/testing/testSubject.tsx index c9e0a38f..c7c247ff 100644 --- a/src/support/testing/testSubject.tsx +++ b/src/support/testing/testSubject.tsx @@ -6,6 +6,8 @@ type DecoratorSettings< TKey extends string, > = TDecorators extends Decorator ? TSettings : never; +type UpdatedSettings = Partial | ((initialSettings: T) => Partial); + type TestHelpers< TComponent extends React.FC, TDecorators extends Decorator[], @@ -20,24 +22,22 @@ type TestHelpers< * Update the properties of the component. * After a `renderComponent` call these settings get reset. */ - updateProps(newProps: Partial>): void; + updateProps( + newProps: UpdatedSettings>, + ): void; /** * Update the settings of a decorator. * After a `renderComponent` call these settings get reset. */ updateDecorator( key: TKey, - settings: - | Partial> - | (( - initialSettings: DecoratorSettings, - ) => Partial>), + settings: UpdatedSettings>, ): void; /** * Render the component wrapped with all decorators, applying * all property and decorator setting updates. */ - renderComponent(): Promise; + renderComponent(): RenderResult; }; export type Decorator< @@ -125,8 +125,7 @@ export const setTestSubject = < let lastRender: RenderResult | null = null; return { - // eslint-disable-next-line require-await - renderComponent: async () => { + renderComponent: () => { if (initialProps === null) { throw new Error("No props specified with setProps"); } @@ -160,7 +159,12 @@ export const setTestSubject = < if (initialProps === null) { throw new Error("No props specified with setProps"); } - props = { ...initialProps, ...props, ...updatedProps }; + const update: Partial> = + typeof updatedProps === "function" + ? updatedProps(props ? props : initialProps) + : updatedProps; + + props = { ...initialProps, ...props, ...update }; }, updateDecorator: (name, updatedSettings) => { const update: Record =