Reputation: 1454
I'm currently writing a javascript game module that takes the following parameter as constructor argument:
{
gameId : "string for unique game id",
cssSelector: "selector for target (used to initialze game)"
}
I have found a way to get around this, see new comment below
I have a pretty good test coverage on everything else, but I can't figure out how to write the following Jasmine test:
describe("Initialization of game", function() {
it("Should throw expection if css selector is not found", function() {
// what goes here?
//negative scenario
expect(function(){
var game = new Game({gameId : '1', cssSelector : "#not-found"});
}).toThrow("Cannot find the element corresponding to cssSelector");
//positive senario
expect(function(){
var game = new Game({gameId : '1', cssSelector : "#found"});
}).not.toThrow("Cannot find the element corresponding to cssSelector");
});
"Solution" I say "solution", because it feels a bit like a hack to get around this. I use the fact that the test is run in HTML and that I can manipulate the environment. So what I did was:
That's it!
Upvotes: 1
Views: 388
Reputation: 2275
If you need more in depth DOM testing, Jasmine won't do it alone.
For a simple DOM requirements and single test, you can continue doing what you're doing.
For simple, but repeated tests, use beforeEach
and afterEach
to set up and destroy the DOM elements you need during testing.
For anything but the most simple DOM tests, you could use something like: https://github.com/jeffwatkins/jasmine-dom to extend Jasmine in to the DOM.
Upvotes: 1
Reputation: 4504
In jQuery this will work. If there's such DOM node it will have length greater than 0.
var exists = $('cssSelector').length > 0;
Upvotes: 0