UNPKG

@blac/react

Version:

React bindings for BlaC — useBloc hook with automatic re-render optimization

58 lines (57 loc) 1.41 kB
import { StateContainerRegistry, getRegistry, setRegistry } from "@blac/core"; import { createCubitStub, registerOverride } from "@blac/core/testing"; import { render } from "@testing-library/react"; //#region src/testing.ts function renderWithBloc(ui, options) { const { bloc: BlocClass, args, ...stubOptions } = options; const previous = getRegistry(); setRegistry(new StateContainerRegistry()); const instance = createCubitStub(BlocClass, { ...stubOptions, args }); registerOverride(BlocClass, instance, args); let renderResult; try { renderResult = render(ui); } catch (e) { setRegistry(previous); throw e; } const originalUnmount = renderResult.unmount; renderResult.unmount = () => { originalUnmount(); setRegistry(previous); }; return { ...renderResult, bloc: instance }; } function renderWithRegistry(ui, setup) { const previous = getRegistry(); const testRegistry = new StateContainerRegistry(); setRegistry(testRegistry); try { setup(testRegistry); } catch (e) { setRegistry(previous); throw e; } let renderResult; try { renderResult = render(ui); } catch (e) { setRegistry(previous); throw e; } const originalUnmount = renderResult.unmount; renderResult.unmount = () => { originalUnmount(); setRegistry(previous); }; return renderResult; } //#endregion export { renderWithBloc, renderWithRegistry }; //# sourceMappingURL=testing.js.map