Не уверен, поможет ли это вам, но если вы хотите проверить генератор genOfGrid
, вы можете попробовать это:
// we need all those dependencies as injections so we can mock them
const genOfGridFactory = (drawGrid, NextGeneration, fieldSize, requestAnimationFrame) => (ctx, grid) => {
ctx.clearRect(0, 0, fieldSize, fieldSize);
drawGrid(ctx, grid);
const gridOfNextGeneration = NextGeneration(grid);
//These is the function i want to test
setTimeout(() => {
requestAnimationFrame(() => genOfGrid(ctx, gridOfNextGeneration));
}, 1000 / 10);
};
it('causes a timeout to be called synchronously', () => {
const requestAnimationFrameMock = jasmine.createSpy('requestAnimationFrame').and.callFake((cb) => cb()); // instant execution
const genOfGrid = genOfGridFactory(
drawGridStub,
NextGenerationMock,
fieldSizeStub,
requestAnimationFrameMock
)// setup them as you wish
const spy = spyOn(genOfGrid);
const ctxStub = ...;
genOfGrid(ctxStub, gridStub, NextGenerationMock);
jasmine.clock().tick(1000/10);// progress enough time to trigger setTimeout requestAnimationFrameMock will trigger its interior immediately
expect(spy).toHaveBeenCalledWith(ctxStub, NextGenerationMock(gridStub));
});