From 759a3c4f1f9115c97bdeb444bebde8aac0394ebc Mon Sep 17 00:00:00 2001 From: Peter Fogg Date: Mon, 2 Nov 2015 15:42:06 -0500 Subject: [PATCH] Fix `withData` and `withConfiguration` helpers. Both of them dynamically generate specs which close over the iteration variable of a for loop. Closures capture *references*, not values, and so when the variable is mutated on loop iteration its new value is used when the spec is called. This means that instead of running a spec with n different values, we run the spec n times with the same value. This is bad. --- .../common/js/spec_helpers/spec_helpers.js | 20 +++++++++++-------- .../teams/js/spec/views/teams_tab_spec.js | 7 ++++--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/common/static/common/js/spec_helpers/spec_helpers.js b/common/static/common/js/spec_helpers/spec_helpers.js index 158be185f5..b6cd315046 100644 --- a/common/static/common/js/spec_helpers/spec_helpers.js +++ b/common/static/common/js/spec_helpers/spec_helpers.js @@ -13,9 +13,11 @@ define([], function () { var withData = function (data, func) { for (var name in data) { if (data.hasOwnProperty(name)) { - it(name, function () { - func.apply(this, data[name]); - }); + (function (name) { + it(name, function () { + func.apply(this, data[name]); + }); + })(name); } } }; @@ -31,12 +33,14 @@ define([], function () { var withConfiguration = function (config, setup, test) { for (var name in config) { if (config.hasOwnProperty(name)) { - describe(name, function () { - beforeEach(function () { - setup.apply(this, config[name]); + (function (name) { + describe(name, function () { + beforeEach(function () { + setup.apply(this, config[name]); + }); + test(); }); - test(); - }); + })(name); } } }; diff --git a/lms/djangoapps/teams/static/teams/js/spec/views/teams_tab_spec.js b/lms/djangoapps/teams/static/teams/js/spec/views/teams_tab_spec.js index e366d004f7..e9f58659dd 100644 --- a/lms/djangoapps/teams/static/teams/js/spec/views/teams_tab_spec.js +++ b/lms/djangoapps/teams/static/teams/js/spec/views/teams_tab_spec.js @@ -171,10 +171,11 @@ define([ ] }, function (url, expectedEvent) { var teamsTabView = createTeamsTabView(this, { - userInfo: TeamSpecHelpers.createMockUserInfo({staff: true}) - }); + userInfo: TeamSpecHelpers.createMockUserInfo({staff: true}) + }); + teamsTabView.teamsCollection = TeamSpecHelpers.createMockTeams(); teamsTabView.router.navigate(url, {trigger: true}); - if (AjaxHelpers.currentRequest(requests)) { + if (requests.length > requests.currentIndex) { AjaxHelpers.respondWithJson(requests, {}); } expect(Logger.log).toHaveBeenCalledWith('edx.team.page_viewed', expectedEvent);