From 371ca1f2040f1d5dde1c8a6450e45bc0c5e8f238 Mon Sep 17 00:00:00 2001 From: Filip Satek Date: Mon, 23 Sep 2024 11:07:36 +0200 Subject: [PATCH] feat: add more hooks into config --- .changeset/slow-ligers-call.md | 5 ++++ .../src/client/configuration.ts | 25 ++++++++++++++++--- packages/testing-library/src/rtl.tsx | 8 ++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .changeset/slow-ligers-call.md diff --git a/.changeset/slow-ligers-call.md b/.changeset/slow-ligers-call.md new file mode 100644 index 0000000000..985d6ff75f --- /dev/null +++ b/.changeset/slow-ligers-call.md @@ -0,0 +1,5 @@ +--- +"@ima/testing-library": minor +--- + +Add client configuration options `beforeInitImaApp`, `beforeRenderWithContext` and `afterRenderWithContext` to be able to specify additional logic, which will be called in the specific times. diff --git a/packages/testing-library/src/client/configuration.ts b/packages/testing-library/src/client/configuration.ts index 0e4e7904b3..7257aff6e9 100644 --- a/packages/testing-library/src/client/configuration.ts +++ b/packages/testing-library/src/client/configuration.ts @@ -1,4 +1,4 @@ -import type { ContextValue, ImaApp } from '../types'; +import type { ContextValue, ImaApp, ImaRenderResult } from '../types'; export interface ClientConfiguration { /** @@ -9,23 +9,42 @@ export interface ClientConfiguration { * The path to the IMA configuration file. This can be only configured once before first `initImaApp` call and cannot be reconfigured later. */ rootDir: string; + /** + * The function that will be called before the IMA application is initialized. + */ + beforeInitImaApp: () => void | Promise; /** * The function that will be called after the IMA application is initialized. */ - afterInitImaApp: (app: ImaApp) => void; + afterInitImaApp: (app: ImaApp) => void | Promise; /** * The function that will be called after the context value is created. */ - getContextValue: (app: ImaApp) => ContextValue; + getContextValue: (app: ImaApp) => ContextValue | Promise; + beforeRenderWithContext: ({ + app, + contextValue, + }: { + app: ImaApp | null; + contextValue: ContextValue; + }) => void | Promise; + afterRenderWithContext: ({ + app, + contextValue, + ...result + }: ImaRenderResult) => void | Promise; } const clientConfiguration: ClientConfiguration = { useFakeDictionary: true, rootDir: process.cwd(), + beforeInitImaApp: () => {}, // eslint-disable-line @typescript-eslint/no-empty-function afterInitImaApp: () => {}, // eslint-disable-line @typescript-eslint/no-empty-function getContextValue: app => ({ $Utils: app.oc.get('$ComponentUtils').getUtils(), }), + beforeRenderWithContext: () => {}, // eslint-disable-line @typescript-eslint/no-empty-function + afterRenderWithContext: () => {}, // eslint-disable-line @typescript-eslint/no-empty-function }; /** diff --git a/packages/testing-library/src/rtl.tsx b/packages/testing-library/src/rtl.tsx index 77ebb153db..1109e18f8f 100644 --- a/packages/testing-library/src/rtl.tsx +++ b/packages/testing-library/src/rtl.tsx @@ -27,6 +27,8 @@ export async function initImaApp(): Promise { const config = getImaTestingLibraryClientConfig(); + await config.beforeInitImaApp(); + // Init language files // This must be initialized before oc.get('$Dictionary').init() is called (usualy part of initServices) await generateDictionary(); @@ -91,8 +93,14 @@ async function renderWithContext( const wrapper = await getContextWrapper(contextValue); + const config = getImaTestingLibraryClientConfig(); + + await config.beforeRenderWithContext({ app, contextValue }); + const result = render(ui, { ...rest, wrapper }); + await config.afterRenderWithContext({ app, contextValue, ...result }); + return { ...result, app,