Jessi
Jessi

Reputation: 821

how do I write jasmine test for innerHTML element

so my code does this...

diff = timevalue - (((Date.now() - startDate)/1000)|0);

document.getElementById("timer").innerHTML = diff.toLocaleString();

part of my current test looks like this...

  it("count should define tictoc", function(){
    jasmine.clock().install();
    aTimer.count(25);
    jasmine.clock().tick(1002);
    expect(aTimer.tictoc).toEqual(1);
    expect(document.getElementById("timer").innerHtml).toBe("success");
    jasmine.clock().uninstall();
  });

but I am getting this back from jasmine...

"TypeError: Cannot set property 'innerHTML' of null"

now I think this is because the html element is not present....which makes sense since I'm testing....but how do I mock that element so my test works?

Upvotes: 2

Views: 5091

Answers (1)

Jessi
Jessi

Reputation: 821

I just got it:

  it("count should define tictoc", function(){
    jasmine.clock().install();
    var dummyElement = document.createElement('span');
    document.getElementById = jasmine.createSpy('HTML Element').and.returnValue(dummyElement);

    aTimer.count(25);
    jasmine.clock().tick(1002);
    expect(aTimer.tictoc).toEqual(1);
    expect(document.getElementById("timer").innerHTML).toEqual('25');
    jasmine.clock().uninstall();
  });

Upvotes: 2

Related Questions