Having a problem asserting that an element does not exist.

19 views
Skip to first unread message

Bobby Johnson (NotMyself)

unread,
May 12, 2010, 7:04:42 PM5/12/10
to jspec
I don't seem to be able to figure out how to tell if an element does
not exist in a way that makes me confident that the test is actually
valid. Here is my scenario. I am writing a bit a functionality that
will prompt the user using impromptu if a cookie does not exist. If
the cookie does exist, it should not prompt the user. Here is my spec:

describe 'and the user has already been notified of an error for the
specific date'
mock_request().and_return('{ "tradeDateIsValid" : false }',
'application/json')

$.cookie($('#ExportDate').val(), "true");

$(document).fileLoadHealthCheck({
datePicker: '#ExportDate',
notificationArea: '#HealthCheckArea'
});

it 'should display the red light'
$
('#HealthCheckArea').children('img').attr('src').should.include
'red_light.png'
end

it 'should make the health check area visible'
$('#HealthCheckArea').should.be_visible
end

it 'should not warn the user'
$('#jqibox').should.be_null
end
end

How should I be validating that #jqibox does not exist in the dom?

--
JSpec thanks you for your interest and support! To post simply reply
to this email.

documentation: http://jspec.info
unsubscribe: jspec+un...@googlegroups.com
group: http://groups.google.com/group/jspec

Kevin Gisi

unread,
May 12, 2010, 7:07:54 PM5/12/10
to js...@googlegroups.com
$('#jqibox') will return a jQuery selector, even if it doesn't find anything, so it won't evaluate to null. However, the length of the object should be zero (meaning that jQuery did not match any elements).

So substitute $('#jqibox').should.be_empty in, and you should be all set! :-)

-Kevin W. Gisi
ke...@kevingisi.com | (916) 538-4474
http://www.kevingisi.com

Bobby Johnson (NotMyself)

unread,
May 12, 2010, 7:12:39 PM5/12/10
to jspec
Hrm... this works as long as I have no other specs. Here is the full
spec file am I missing something?

describe 'when a page with the file load health check initially
loads'

it 'should have the health check area not visible'
$('#HealthCheckArea').should.not.be_visible
end

it 'should have the green light already loaded'
$
('#HealthCheckArea').children('img').attr('src').should.include
'green_light.png'
end
end


describe 'when checking the health of the currently selected trade
date'

before_each
end
after_each
$.cookie($('#ExportDate').val(), null);
$.prompt.close();
end

describe 'and all files have loaded for the given date'
mock_request().and_return('{ "tradeDateIsValid" : true }',
'application/json')

$(document).fileLoadHealthCheck({
datePicker: '#ExportDate',
notificationArea: '#HealthCheckArea'
});

it 'should display the green light'
$
('#HealthCheckArea').children('img').attr('src').should.include
'green_light.png'
end

it 'should make the health check area visible'
$('#HealthCheckArea').should.be_visible
end

end

describe 'and all files have not loaded for the given date'
mock_request().and_return('{ "tradeDateIsValid" : false }',
'application/json')

$(document).fileLoadHealthCheck({
datePicker: '#ExportDate',
notificationArea: '#HealthCheckArea'
});

it 'should display the red light'
$
('#HealthCheckArea').children('img').attr('src').should.include
'red_light.png'
end

it 'should make the health check area visible'
$('#HealthCheckArea').should.be_visible
end

it 'should warn the user that the given date is incomplete'
$('#jqibox').should.be_visible
end
end

describe 'and the user has already been notified of an error for
the specific date'
mock_request().and_return('{ "tradeDateIsValid" : false }',
'application/json')

$.cookie($('#ExportDate').val(), "true");

$(document).fileLoadHealthCheck({
datePicker: '#ExportDate',
notificationArea: '#HealthCheckArea'
});

it 'should display the red light'
$
('#HealthCheckArea').children('img').attr('src').should.include
'red_light.png'
end

it 'should make the health check area visible'
$('#HealthCheckArea').should.be_visible
end

it 'should not warn the user'
$('#jqibox').should.be_empty
end
end
end


On May 12, 4:07 pm, Kevin Gisi <ke...@kevingisi.com> wrote:
> $('#jqibox') will return a jQuery selector, even if it doesn't find
> anything, so it won't evaluate to null. However, the length of the object
> should be zero (meaning that jQuery did not match any elements).
>
> So substitute $('#jqibox').should.be_empty in, and you should be all set!
> :-)
>
> -Kevin W. Gisi
> ke...@kevingisi.com | (916) 538-4474http://www.kevingisi.com
> > unsubscribe: jspec+un...@googlegroups.com<jspec%2Bunsu...@googlegroups.com>

Bobby Johnson (NotMyself)

unread,
May 12, 2010, 7:23:26 PM5/12/10
to jspec
Oh god, I am friggin dense. The prompt fades out... 8(

adding a $('#jqibox').remove(); in my after_each did the trick.

Sorry for the newb flailing...

On May 12, 4:12 pm, "Bobby Johnson (NotMyself)"
Reply all
Reply to author
Forward
0 new messages