lundi 24 février 2020

Jest: How to I test a function that calls another function in the body

I am trying to test the makeRandomComputerMove function, however, I am unable to properly mock the getRandomNumber function exported from the same module using Jest.

If I call the getRandomNumber function directly in the test file it works as expected, but I was under the impression that mocking the function in the test file should force the internal makeRandomComputerMove function to use the mocked value.

Any help would be appreciated.

test.ticTacToe.js

describe('TicTacToe Utils', () => {
    const randomMock = jest.spyOn(Utils, 'getRandomNumber')
        .mockReturnValueOnce(0)
        .mockReturnValueOnce(1)

    const board = [
        [['X'], [], []],
        [[], [], []],
        [[], [], []]
        ]

    it('makeRandomComputerMove', () => {
        const location = Utils.makeRandomComputerMove(board)
        // expect(location.x).toBe(0)
        // expect(location.y).toBe(1)
    })
})

ticTacToe.js

export const getRandomNumber = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

export const makeRandomComputerMove = (board) => {
    const location = {};
    location.x = getRandomNumber(0, board.length - 1);
    location.y = getRandomNumber(0, board.length - 1);
    if (location.x < 3 && location.y < 3) {
        return location

    }
    return makeRandomComputerMove(board);   
};

Aucun commentaire:

Enregistrer un commentaire