How to write test for confirm dialog

3,315 views
Skip to first unread message

Quazi Marufur Rahman

unread,
Jan 30, 2014, 2:42:10 PM1/30/14
to jasmi...@googlegroups.com
Hi,

I have written a code snippet which will show a alert box if there is any text in an input field 
and a user tries to reload the page or redirect to another page.

I want to write a test for this scenario using Jasmine. I have written a test, but it always fails. 

Please help me to find the problem. No confirm box is creating during simulation. Why this fails and how to solve this issue?
Thanks in advance.

Michael Bielski

unread,
Jan 30, 2014, 3:47:31 PM1/30/14
to jasmi...@googlegroups.com
Mocking window.confirm:
window.confirm = function(msg){
     //return false to simulate the Cancel button having been clicked, or true to simulate the OK button having been clicked
     return false;
}; 

Christian G. Warden

unread,
Jan 30, 2014, 4:03:44 PM1/30/14
to jasmi...@googlegroups.com
I would split it up into two suites. Test that your event handler is
bound to the window events by using a spy, and test that your event
handler does what it's supposed to by calling it directly.

describe('beforeunload', function() {
it('should trigger warning', function() {
spyOn(someObject, '_unsavedPublisherWarning');
$(window).trigger('beforeunload');
expect(someObject._unsavedPublisherWarning).toHaveBeenCalled();
});
});

describe('someObject._unsavedPublisherWarning', function() {
describe('when input exists', function() {
beforeEach(function() {
someObject.el_input.val('whatever');
});
it('should return a warning', function() {
expect(someObject._unsavedPublisherWarning()).toBe('navigation_warning');
});
});
describe('when no input exists', function() {
beforeEach(function() {
someObject.el_input.val('');
});
it('should not return a warning', function() {
expect(someObject._unsavedPublisherWarning()).not.toBeDefined();
});
});
});

On Thu, Jan 30, 2014 at 11:42:10AM -0800, Quazi Marufur Rahman wrote:
> Hi,
>
> I have written a code snippet which will show a alert box if there is any
> text in an input field
> and a user tries to reload the page or redirect to another page.
> Here is the code. <https://gist.github.com/qmaruf/8717053>
>
> I want to write a test for this scenario using Jasmine. I have written a
> test, but it always fails.
> Here is the test. <https://gist.github.com/qmaruf/8717089>
>
> Please help me to find the problem. No confirm box is creating during
> simulation. Why this fails and how to solve this issue?
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups "Jasmine" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
> To post to this group, send email to jasmi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/jasmine-js.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages