mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Introduce test utils for rendering and running with thrown mobx reactions
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
8123472919
commit
e99828b8e4
@ -2,3 +2,5 @@ export * from "./src/flush-promises";
|
|||||||
export * from "./src/get-global-override-for-function";
|
export * from "./src/get-global-override-for-function";
|
||||||
export * from "./src/get-global-override";
|
export * from "./src/get-global-override";
|
||||||
export * from "./src/get-promise-status";
|
export * from "./src/get-promise-status";
|
||||||
|
export * from "./src/render-for";
|
||||||
|
export * from "./src/run-with-thrown-mobx-reactions";
|
||||||
|
|||||||
@ -23,5 +23,13 @@
|
|||||||
"build": "webpack",
|
"build": "webpack",
|
||||||
"dev": "webpack --mode=development --watch",
|
"dev": "webpack --mode=development --watch",
|
||||||
"test": "jest --coverage --runInBand"
|
"test": "jest --coverage --runInBand"
|
||||||
|
},
|
||||||
|
|
||||||
|
"peerDependencies": {
|
||||||
|
"@ogre-tools/injectable": "^15.1.2",
|
||||||
|
"@ogre-tools/injectable-react": "^15.1.2",
|
||||||
|
"@testing-library/react": "^12.1.5",
|
||||||
|
"lodash": "^4.17.21",
|
||||||
|
"react": "^17.0.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
24
packages/utility-features/test-utils/src/render-for.tsx
Normal file
24
packages/utility-features/test-utils/src/render-for.tsx
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import React from "react";
|
||||||
|
import type { RenderResult } from "@testing-library/react";
|
||||||
|
import { render as testingLibraryRender } from "@testing-library/react";
|
||||||
|
import type { DiContainer } from "@ogre-tools/injectable";
|
||||||
|
import { DiContextProvider } from "@ogre-tools/injectable-react";
|
||||||
|
|
||||||
|
export type DiRender = (ui: React.ReactElement) => RenderResult;
|
||||||
|
|
||||||
|
type DiRenderFor = (di: DiContainer) => DiRender;
|
||||||
|
|
||||||
|
export const renderFor: DiRenderFor = (di) => (ui) => {
|
||||||
|
const result = testingLibraryRender(
|
||||||
|
<DiContextProvider value={{ di }}>{ui}</DiContextProvider>
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...result,
|
||||||
|
|
||||||
|
rerender: (ui: React.ReactElement) =>
|
||||||
|
result.rerender(
|
||||||
|
<DiContextProvider value={{ di }}>{ui}</DiContextProvider>
|
||||||
|
),
|
||||||
|
};
|
||||||
|
};
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
import { noop } from "lodash/fp";
|
||||||
|
import { _resetGlobalState, configure } from "mobx";
|
||||||
|
|
||||||
|
export const runWithThrownMobxReactions = (callback: () => void) => {
|
||||||
|
const originalConsoleWarn = console.warn;
|
||||||
|
|
||||||
|
console.warn = noop;
|
||||||
|
|
||||||
|
configure({
|
||||||
|
disableErrorBoundaries: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.warn = originalConsoleWarn;
|
||||||
|
|
||||||
|
let error: any;
|
||||||
|
|
||||||
|
try {
|
||||||
|
callback();
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
} finally {
|
||||||
|
configure({
|
||||||
|
disableErrorBoundaries: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
// This is because when disableErrorBoundaries is true, MobX doesn't recover from the thrown
|
||||||
|
// errors, and its global state starts bleeding between tests making.
|
||||||
|
_resetGlobalState();
|
||||||
|
|
||||||
|
if (!error) {
|
||||||
|
throw new Error(
|
||||||
|
"Tried to run with thrown MobX reactions but nothing was thrown"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user