I have a function that does some operations with the MutationRecords collected in a MutationObserver. I would like to test the operations done to these mutations in an unit test. I am using JEST.
I found a way to mock the MutationObserver, but would like to mock the MutationRecords too.
This is my .spec.ts
import { functionWithMutationObserverInside } from "./testingQuestion"
const mutationObserverMock = jest
.fn<MutationObserver, [MutationCallback]>()
.mockImplementation(() => {
return {
observe: jest.fn(),
disconnect: jest.fn(),
takeRecords: jest.fn(),
}
})
global.MutationObserver = mutationObserverMock
const mutations: MutationRecord[] = []
describe("MutationObserver", () => {
it("checks if MutiationObserver is called", () => {
functionWithMutationObserverInside()
const observerCb = mutationObserverMock.mock.calls[0][0]
observerCb(mutations, mutationObserverMock.mock.instances[0])
})
})
Thanks!