From 2dc5b8e89cb82ecd500958249c0679f37aa0b8a9 Mon Sep 17 00:00:00 2001 From: Christine Lytwynec Date: Thu, 24 Sep 2015 16:25:49 -0400 Subject: [PATCH] make verifyElementInFocus a ViewHelpers method --- .../js/spec/components/feedback_spec.js | 28 ++++--------------- .../common/js/spec_helpers/view_helpers.js | 23 +++++++++++++-- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/common/static/common/js/spec/components/feedback_spec.js b/common/static/common/js/spec/components/feedback_spec.js index a5e406fe3c..9ed52894ce 100644 --- a/common/static/common/js/spec/components/feedback_spec.js +++ b/common/static/common/js/spec/components/feedback_spec.js @@ -1,8 +1,8 @@ // Generated by CoffeeScript 1.6.1 (function() { - define(["jquery", "common/js/components/views/feedback", "common/js/components/views/feedback_notification", "common/js/components/views/feedback_alert", "common/js/components/views/feedback_prompt", "sinon", "jquery.simulate"], - function($, SystemFeedback, NotificationView, AlertView, PromptView, sinon) { + define(["jquery", "common/js/components/views/feedback", "common/js/components/views/feedback_notification", "common/js/components/views/feedback_alert", "common/js/components/views/feedback_prompt", 'common/js/spec_helpers/view_helpers', "sinon", "jquery.simulate"], + function($, SystemFeedback, NotificationView, AlertView, PromptView, ViewHelpers, sinon) { var tpl; tpl = readFixtures('system-feedback.underscore'); beforeEach(function() { @@ -137,21 +137,13 @@ var view; view = new PromptView.Confirmation(this.options).show(); expect(this.inFocusSpy).toHaveBeenCalled(); - return waitsFor( - function() { return view.$('.wrapper-prompt:focus').length === 1; }, - "The modal should have focus", - 500 - ); + return ViewHelpers.verifyElementInFocus(view, ".wrapper-prompt") }); it("is not focused on hide", function() { var view; view = new PromptView.Confirmation(this.options).hide(); expect(this.outFocusSpy).toHaveBeenCalled(); - return waitsFor( - function() { return view.$('.wrapper-prompt:focus').length === 0; }, - "The modal should not have focus", - 500 - ); + return ViewHelpers.verifyElementNotInFocus(view, ".wrapper-prompt") }); it("traps keyboard focus when moving forward", function() { var view; @@ -161,11 +153,7 @@ "keydown", { keyCode: $.simulate.keyCode.TAB } ); - return waitsFor( - function() { return view.$('.action-primary:focus').length === 1; }, - "The first action button should have focus", - 500 - ); + return ViewHelpers.verifyElementInFocus(view, ".action-primary") }); it("traps keyboard focus when moving backward", function() { var view; @@ -175,11 +163,7 @@ "keydown", { keyCode: $.simulate.keyCode.TAB, shiftKey: true } ); - return waitsFor( - function() { return view.$('.action-secondary:focus').length === 1; }, - "The last action button should have focus", - 500 - ); + return ViewHelpers.verifyElementInFocus(view, ".action-secondary") }); return it("changes class on body", function() { var view; diff --git a/common/static/common/js/spec_helpers/view_helpers.js b/common/static/common/js/spec_helpers/view_helpers.js index 32885ebc39..93b5827b89 100644 --- a/common/static/common/js/spec_helpers/view_helpers.js +++ b/common/static/common/js/spec_helpers/view_helpers.js @@ -10,7 +10,8 @@ define(["jquery", "common/js/components/views/feedback_notification", "common/js verifyFeedbackHidden, createNotificationSpy, verifyNotificationShowing, verifyNotificationHidden, createPromptSpy, confirmPrompt, inlineEdit, verifyInlineEditChange, installMockAnalytics, removeMockAnalytics, verifyPromptShowing, verifyPromptHidden, - clickDeleteItem, patchAndVerifyRequest, submitAndVerifyFormSuccess, submitAndVerifyFormError; + clickDeleteItem, patchAndVerifyRequest, submitAndVerifyFormSuccess, submitAndVerifyFormError, + verifyElementInFocus, verifyElementNotInFocus; installViewTemplates = function() { appendSetFixtures('
'); @@ -127,6 +128,22 @@ define(["jquery", "common/js/components/views/feedback_notification", "common/js verifyNotificationShowing(notificationSpy, /Saving/); }; + verifyElementInFocus = function(view, selector) { + waitsFor( + function() { return view.$(selector + ':focus').length === 1; }, + "element to have focus: " + selector, + 500 + ); + }; + + verifyElementNotInFocus = function(view, selector) { + waitsFor( + function() { return view.$(selector + ':focus').length === 0; }, + "element to not have focus: " + selector, + 500 + ); + }; + return { 'installViewTemplates': installViewTemplates, 'createNotificationSpy': createNotificationSpy, @@ -143,7 +160,9 @@ define(["jquery", "common/js/components/views/feedback_notification", "common/js 'clickDeleteItem': clickDeleteItem, 'patchAndVerifyRequest': patchAndVerifyRequest, 'submitAndVerifyFormSuccess': submitAndVerifyFormSuccess, - 'submitAndVerifyFormError': submitAndVerifyFormError + 'submitAndVerifyFormError': submitAndVerifyFormError, + 'verifyElementInFocus': verifyElementInFocus, + 'verifyElementNotInFocus': verifyElementNotInFocus }; }); }).call(this, define || RequireJS.define);