Skip to content

Instantly share code, notes, and snippets.

@valterbarros
Created January 27, 2023 16:34
Show Gist options
  • Save valterbarros/d708d2710d605e6c0d3b0bf5b9002a16 to your computer and use it in GitHub Desktop.
Save valterbarros/d708d2710d605e6c0d3b0bf5b9002a16 to your computer and use it in GitHub Desktop.
mock mutation observer and use on jest test
const mockMutationObserver = function() {
observerDomChangesCaf.mockImplementation((_, errorCallback) => {
console.log('observerDomChangesCaf');
errorCallback();
});
const el = document.createElement('div');
useMutationObserver.mockImplementation((_, callback) => {
const mut = new MutationObserver(callback);
mut.observe(el, {attributes: true});
});
const triggerMutation = () => el.style.display = 'flex';
return triggerMutation;
};
it.only('should emit capture-error event on error', async () => {
const triggerMutation = mockMutationObserver();
// gen.then((fn1) => {
// console.log('inside then');
// triggerMutation = fn1
// });
const imageUrl = 'https://url-image.com';
wrapperCaptureFrameSpy.mockImplementation(() => imageUrl);
// mockMutationObserver();
const { emitted } = renderWidgetCapture({
step: 0,
documentType: 'CNH',
showCapture: true,
});
await flushPromises();
const captureButton = screen.queryByTestId('captureButton');
await fireEvent.click(captureButton);
triggerMutation();
await flushPromises();
await waitFor(() => expect(emitted()['capture-error'][0])
.toEqual([imageUrl]));
// expect(wrapperCaptureFrameSpy).
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment