views:

138

answers:

3

Hi all:

Currently I'm unit testing the following code:

if ($(selectedElement).innerText == 'blah')
{
    // do something
}

with selectedElement being an anchor object selected from the UI.

In my test code, I have created a DOM structure which has that anchor in the proper position ready to be selected. The problem here is that since selectedElement is originally a javascript object, I need to convert the anchor I got from the DOM structure (which is a JQuery object) in order to get into the above condition.

I have tried the following, with no success:

// DOM structure using HtmlDoc
/*:DOC += <span id='testSpan' class='testSpanClass'><a href='#' id='selectedElem'>blah</a></span> */
selectedElement = $('#selectedElem')[0];

My goal is to be able to use a normal Javascript object to satisfy the condition, and also be able to switch it back to a jQuery object to satisfy conditions further down the function. But if there is a better approach I'll give it a go.

Does anyone have any ideas on how to go about this problem?

EDIT: Is there a solution that does not require changing of the code? selectedElement is actually a global variable.

Thanks.

+2  A: 

can you use jquery's .html() ?

if ($(selectedElement).html() == 'blah')
{
    // do something
}

otherwise, without changing code:

var selectedElement = $('#selectedElem')[0];

if (selectedElement.innerHTML == 'blah')
{
    // do something
}
Nick Spiers
`.html()` would not be the equivalent of `.innerText`. `.text()` would be what they would want.
Doug Neiner
After a quick test to make sure, it seems to work either way. But I was of the understanding that .html() was for anything between html tags.
Nick Spiers
If I can change the code, yes this works perfect. What if I cannot change the code? Are there anyway to write tests to actually get into the condition?
BeraCim
Hey Nick, you are right, in this case they would be the same. But if there were any child elements it would be a different result.
Doug Neiner
@BeraCim try my edit. @dcneiner Yeah, it would return everything between the anchor tags. I'm just used to innerHTML (like in my edit) working on this and not innerText which would be undefined, so I think of .html()
Nick Spiers
Code cannot be changed... so $(selectedElement).innerText must stay. This is why I found the problem very tricky as I can only write test to try to pass the condition. selectedElement is a html anchor object, not a jQuery object. And to make matters worse... this is the line of code after the condition: var sth = $(selectedElement).parents('span.className')[0];
BeraCim
In that case, I don't think it's possible. $(selectedElement).innerText just isn't going to work. innerText is not a property for a jQuery object.
Nick Spiers
@Nick right on. The point of testing is writing _failing_ tests, then _fixing_ the code to make the test past. Looks like we have the first part working... the _failing_ part. :)
Doug Neiner
+1  A: 

jQuery's get method returns the original DOM elements for that jQuery object. I think perhaps you need to use $('#selectedElem').get(0)

Tom Martin
+3  A: 

I am not sure what browser you are testing in, but innerText is an IE only property. Since you are already using jQuery, I would suggest you just call the .text() method on the selected element like this:

selectedElement = $('#selectedElem')[0]; // Get DOM element

if ($(selectedElement).text() == 'blah')
{
    // do something
}

You're method of getting the DOM object is fine: $('#selectedElem')[0] or $('#selectedElem').get(0) are equivalent, but the first one is faster in large loops.

Doug Neiner
Unfortunately it didn't work. Also, I would avoid changing the code as selectedElement is a global variable also used somewhere else (as updated in the question).
BeraCim
What didn't work? (I updated my code to reflect not changing the global variable). If you have a valid DOM element, $(el).text() will return the `innerText` so to speak.
Doug Neiner
What if I'm not allowed to change 'innerText'?
BeraCim
`innerText` is an Internet Explorer only property. It will only work in IE, other browsers have different properties. I think Firefox has `textContent`. Do you mean you can't change the value or you can't change the use of the property?
Doug Neiner
I have to use the expression $(selectedElement).innerText. This is in the source code which I'm not allowed to modify.
BeraCim
which explains why FF keeps returning undefined for innerText for me. After discovering jQuery, I really lost touch with javascript!
Nick Spiers
@BeraCim then your expression will always fail, which is the point of unit tests. The source code is incorrect if that is the format they are using. `$(selectedElement)` returns a jQuery wrapped DOM element. The jQuery object does not have an `.innerText` property and will thus, always fail.
Doug Neiner