Created
October 22, 2011 09:48
-
-
Save dnagir/1305824 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Users | |
should save record when losing focus from input | |
Expected spy updateFromForm to have been called. | |
Error: Expected spy updateFromForm to have been called. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App.Users extends Spine.Controller | |
elements: | |
'form': 'form' | |
# Events are bound via Proxy, so you can't spy on them. We have to wrap actual handler in a dummy method to spy on those. | |
events: | |
'blur input': 'onBlur' | |
constructor: -> | |
super | |
@render() | |
render: -> | |
@html @generate @template, @model | |
onBlur: -> @updateFromForm() | |
updateFromForm: -> | |
@model.updateAttributes @form.serializeForm() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "Users", -> | |
users = null | |
user = null | |
input = -> $(".users input[name='name']") | |
beforeEach -> | |
setFixtures "<div class='users' />" | |
user = new App.User(name: 'Dima', email: 'me@example.com', summary: 'Summary') | |
users = new App.Users(el: '.users', template: 'edit', model: user) | |
spyOn user, 'save' | |
# This passes | |
it "should udpate record with form data", -> | |
input().val('Jimmy') | |
users.updateFromForm() | |
expect(user.name).toBe 'Jimmy' | |
expect(user.save).toHaveBeenCalled() | |
# This fails, I can't see why the 'blur' is not triggered | |
# The blur is, however, triggered when I manually bind it: | |
# users.el.delegate 'input', 'blur', -> console.warn('Bluurrrred!') | |
it "should save record when losing focus from input", -> | |
spyOn users, 'updateFromForm' | |
input().trigger('blur') | |
expect(users.updateFromForm).toHaveBeenCalled() |
@maccman, you're right. When updateFromForm
is called as event handler - it is proxied, so that the actual function is hidden and can't be replaces (spied on).
The only way to avoid that - is to make a dummy function onBlur
for event handlers and call the actual function from there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@maccman, the weird thing is that when I manually
users.el.delegate
, it does work. So the event is triggers successfully and handled.I must be missing something stupid when defining it with Spine.