Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const counter = new CounterContainer();
const tree = renderer.create(
);
renderer.act(() => {
const el: any = tree
.toJSON()
.children.find(({ props = {} }) => props.id === 'offset-text');
expect(el.children).toEqual(['0']);
});
// This update should not trigger a re-render
renderer.act(() => {
counter.increment();
});
renderer.act(() => {
const el: any = tree
.toJSON()
.children.find(({ props = {} }) => props.id === 'offset-text');
expect(el.children).toEqual(['0']);
});
// This update should trigger a re-render
renderer.act(() => {
counter.setOffset(10);
});
renderer.act(() => {
export default function act(callback: () => T): T {
let callbackResult: T;
const actResult = TestRenderer.act(() => {
callbackResult = callback();
return callbackResult as any;
});
if (isPromiseLike(callbackResult!)) {
// Return it this way, to preserve warnings that React emits.
return {
then(resolve: (value: any) => void, reject: (err?: Error) => void) {
(actResult as PromiseLike).then(() => {
resolve(callbackResult);
}, reject);
},
} as any;
}
return callbackResult!;
}
it('should render without errors in ReactTestRenderer', () => {
ReactTestRenderer.act(() => {
ReactTestRenderer.create(element, {
createNodeMock: node => {
return document.createElement(node.type);
},
});
});
});
}
export async function asyncAct(callback: () => any) {
return await act(async () => {
await callback()
})
}
async function click({ children = [] }, id) {
renderer.act(() => {
const el: any = children.find(({ props = {} }) => props.id === id);
el.props.onClick();
});
}
const hookProps = { current: initialProps }
const wrapUiIfNeeded = (innerElement) =>
wrapper ? React.createElement(wrapper, null, innerElement) : innerElement
const toRender = () =>
wrapUiIfNeeded(
}>
{setValue}
)
let testRenderer
act(() => {
testRenderer = create(toRender())
})
const { unmount, update } = testRenderer
return {
result,
rerender: (newProps = hookProps.current) => {
hookProps.current = newProps
act(() => {
update(toRender())
})
},
unmount: () => {
act(() => {
unmount()
})
localStorage: createPromisifiedStorage(),
sessionStorage: createPromisifiedStorage(),
rest: new RestClient("http://localhost/api"),
postMessage: new PostMessageService(),
browserInfo: params.browserInfo || { ios: false },
uuidGenerator: createUUIDGenerator(),
eventEmitter: new EventEmitter2({ wildcard: true, maxListeners: 20 }),
clearSession: sinon.stub(),
changeLocale: sinon.stub(),
transitionControl: {
allowTransition: true,
history: [],
},
};
let testRenderer: ReactTestRenderer;
TestRenderer.act(() => {
testRenderer = TestRenderer.create(
{element},
{ createNodeMock }
);
});
return {
context,
testRenderer: testRenderer!,
subscriptionHandler: subscriptionHandler as SubscriptionHandlerReadOnly,
};
}
function unmountHook() {
act(() => {
removeCleanup(unmountHook)
unmount()
})
}
const onBackdropClick = sinon.stub();
const onClose = sinon.stub();
expect(document.body.lastChild).toBeNull();
let testRenderer: ReactTestRenderer;
TestRenderer.act(() => {
testRenderer = TestRenderer.create(
<div>Test</div>
,
{ createNodeMock }
);
});
testRenderer!.root
.findByProps({ "data-testid": "backdrop" })
.props.onClick(event);
TestRenderer.act(() => {
testRenderer.unmount();
});
expect(onBackdropClick.called).toBe(true);
expect(onClose.calledWith(event, "backdropClick")).toBe(true);
});
export async function doAsyncAct(cb: Actable, patchAdapter: Patcher): Promise {
const restoreConsole = patchConsoleErrors();
const restoreAdapter = patchAdapter();
let value: T;
try {
await act(async () => {
value = await integrationOptions.runWithTimers(cb);
});
} finally {
restoreConsole();
restoreAdapter();
}
return value!;
}