Skip to content

Commit

Permalink
Merge pull request #578 from seznam/itl-hooks
Browse files Browse the repository at this point in the history
feat: add more hooks into config
  • Loading branch information
Filipoliko authored Sep 23, 2024
2 parents af20eaf + 371ca1f commit 67e8e06
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-ligers-call.md
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 22 additions & 3 deletions packages/testing-library/src/client/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ContextValue, ImaApp } from '../types';
import type { ContextValue, ImaApp, ImaRenderResult } from '../types';

export interface ClientConfiguration {
/**
Expand All @@ -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<void>;
/**
* The function that will be called after the IMA application is initialized.
*/
afterInitImaApp: (app: ImaApp) => void;
afterInitImaApp: (app: ImaApp) => void | Promise<void>;
/**
* The function that will be called after the context value is created.
*/
getContextValue: (app: ImaApp) => ContextValue;
getContextValue: (app: ImaApp) => ContextValue | Promise<ContextValue>;
beforeRenderWithContext: ({
app,
contextValue,
}: {
app: ImaApp | null;
contextValue: ContextValue;
}) => void | Promise<void>;
afterRenderWithContext: ({
app,
contextValue,
...result
}: ImaRenderResult) => void | Promise<void>;
}

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
};

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/testing-library/src/rtl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export async function initImaApp(): Promise<ImaApp> {

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();
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 67e8e06

Please sign in to comment.