From 315a7c9aa3d6cc6cde51a67b6dcc91aea085f518 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 28 Oct 2013 10:48:53 +0100 Subject: reset button loading... state on error (#4231) including test refactored error display some --- users/app/assets/javascripts/users.js | 51 ++++++++++++++++++-------- users/test/integration/browser/account_test.rb | 14 +++++++ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/users/app/assets/javascripts/users.js b/users/app/assets/javascripts/users.js index aaeba6e..8486756 100644 --- a/users/app/assets/javascripts/users.js +++ b/users/app/assets/javascripts/users.js @@ -46,6 +46,13 @@ $(form).find('input[type="submit"]').button('loading'); }; + resetButtons = function(submitEvent) { + var form = $('form.submitted') + // bootstrap loading state: + $(form).find('input[type="submit"]').button('reset'); + $(form).removeClass('submitted') + }; + // // PUBLIC FUNCTIONS // @@ -70,24 +77,36 @@ // srp.error = function(message) { clear_errors(); - var element, error, field; + var errors = extractErrors(message); + displayErrors(errors); + resetButtons(); + } + + function extractErrors(message) { if ($.isPlainObject(message) && message.errors) { - for (field in message.errors) { - if (field == 'base') { - alert_message(message.errors[field]); - continue; - } - error = message.errors[field]; - element = $('form input[name$="[' + field + ']"]'); - if (!element) { - continue; - } - element.trigger('element:validate:fail.ClientSideValidations', error).data('valid', false); - } - } else if (message.error) { - alert_message(message.error); + return message.errors; } else { - alert_message(JSON.stringify(message)); + return { + base: (message.error || JSON.stringify(message)) + }; + } + } + + function displayErrors(errors) { + for (var field in errors) { + var error = errors[field]; + if (field === 'base') { + alert_message(error); + } else { + displayFieldError(field, error); + } + } + } + + function displayFieldError(field, error) { + var element = $('form input[name$="[' + field + ']"]'); + if (element) { + element.trigger('element:validate:fail.ClientSideValidations', error).data('valid', false); } }; diff --git a/users/test/integration/browser/account_test.rb b/users/test/integration/browser/account_test.rb index 1deda45..243ccfb 100644 --- a/users/test/integration/browser/account_test.rb +++ b/users/test/integration/browser/account_test.rb @@ -27,6 +27,20 @@ class AccountTest < BrowserIntegrationTest User.find_by_login(username).account.destroy end + test "failed login" do + username, password = submit_signup + click_on 'Logout' + click_on 'Log In' + fill_in 'Username', with: username + fill_in 'Password', with: "wrong password" + click_on 'Log In' + assert page.has_selector? 'input.btn-primary.disabled' + assert page.has_content? I18n.t(:invalid_user_pass) + assert page.has_no_content?("Welcome #{username}") + assert page.has_no_selector? 'input.btn-primary.disabled' + User.find_by_login(username).account.destroy + end + test "change password" do username, password = submit_signup click_on "Account Settings" -- cgit v1.2.3 From 1ef3e9df271934b983ff5afe60c2dcf34c090a98 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 28 Oct 2013 11:08:31 +0100 Subject: no need to create a user for testing failed login attempt --- users/test/integration/browser/account_test.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/users/test/integration/browser/account_test.rb b/users/test/integration/browser/account_test.rb index 243ccfb..8e03856 100644 --- a/users/test/integration/browser/account_test.rb +++ b/users/test/integration/browser/account_test.rb @@ -28,17 +28,14 @@ class AccountTest < BrowserIntegrationTest end test "failed login" do - username, password = submit_signup - click_on 'Logout' + visit '/' click_on 'Log In' - fill_in 'Username', with: username + fill_in 'Username', with: "username" fill_in 'Password', with: "wrong password" click_on 'Log In' assert page.has_selector? 'input.btn-primary.disabled' assert page.has_content? I18n.t(:invalid_user_pass) - assert page.has_no_content?("Welcome #{username}") assert page.has_no_selector? 'input.btn-primary.disabled' - User.find_by_login(username).account.destroy end test "change password" do -- cgit v1.2.3