1

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:

  1. In the negative scenario, use a specifier that is not found. The first expectation will then not fail.
  2. In between on the positive and negative test case, I used jQuerys .append() method to add a div with id "found" to the body

That's it!

4
  • the selector itself is a string. What are you using to select DOM element? jQuery,pure js,... Commented Jan 10, 2013 at 11:43
  • the module itself is dependent on jQuery, so I'll just select the element like '$(cssSelector)'. Have not heard about jQuery.pure.js -- I'll check if out to see if solves my problem - thanks! Commented Jan 10, 2013 at 12:11
  • no I've meant just js no framework Commented Jan 10, 2013 at 12:13
  • right, then the answer is: jQuery Commented Jan 10, 2013 at 13:48

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

jasmine-dom is what I was looking for. I still don't find it elegant, but it will definityly do the trick. Thanks!
0

Maybe because you are missing a ); after the "#not-found"}?

1 Comment

thanks for the post, sorry that's not the problem though -- I removed some code to get it more clear what I'm trying to do and the ); disappeared in that process.
0

In jQuery this will work. If there's such DOM node it will have length greater than 0.

var exists = $('cssSelector').length > 0;

2 Comments

thanks, that's what I ended up using. however, I needed one more thing to get everything right -- I've updated my post with that.
I really don't get it, maybe you could be more specific

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.