I'm encountering an issue with testing this JavaScript code:
$("#ShootBtn").on('click', () => foo.testFunc());
var foo = {
testFunc: function() {
hub.server.shoot(true, username, gameCode);
}
}
For my testing framework, I am using Jasmine along with Karma and Chutzpah as the test runners.
In my test project where I am trying to reference the above file, I have attempted various approaches but I can't seem to figure it out. The current state of the test is as follows:
/// <reference path="../../TankWebApplication/Scripts/jquery-3.3.1.js"/>
/// <reference path="../../TankWebApplication/Scripts/virtualjoystick.js"/>
/// <reference path="../../TankWebApplication/Scripts/gameController.js"/>
describe("DefaultTest",
function () {
beforeEach(function() {
})
it("Test",
function() {
spyOn(foo, 'testFunc');
document.getElementById('ShootBtn').click();
expect(foo.testFunc).toHaveBeenCalled();
});
});
The error message indicates that the testFunc has not been called:
TypeError: Cannot read property 'click' of null
This leads me to believe that the issue lies in clicking on the shootBtn element. Is it impossible to test this functionality, or am I missing something?