From feb3824c1a342260545b50f0bda264a74e7322f3 Mon Sep 17 00:00:00 2001 From: Dan Gebhardt Date: Tue, 10 Feb 2015 14:00:50 -0500 Subject: [PATCH] Ensure callbacks are called in the test module's context. Provides access to any of a module's properties (e.g. `container`, `callbacks`, etc.) from within a callback. --- .../test-module-for-component.js | 2 +- lib/ember-test-helpers/test-module.js | 3 ++- tests/test-module-test.js | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/ember-test-helpers/test-module-for-component.js b/lib/ember-test-helpers/test-module-for-component.js index b4421f251..b48384a94 100644 --- a/lib/ember-test-helpers/test-module-for-component.js +++ b/lib/ember-test-helpers/test-module-for-component.js @@ -49,7 +49,7 @@ export default TestModule.extend({ this.callbacks.append = function() { Ember.deprecate('this.append() is deprecated. Please use this.render() instead.'); - return this.render(); + return this.callbacks.render(); }; context.$ = function() { diff --git a/lib/ember-test-helpers/test-module.js b/lib/ember-test-helpers/test-module.js index c4f5fa144..58b00698d 100644 --- a/lib/ember-test-helpers/test-module.js +++ b/lib/ember-test-helpers/test-module.js @@ -164,7 +164,8 @@ export default Klass.extend({ context[key] = function(options) { if (_this.cache[key]) { return _this.cache[key]; } - var result = callbacks[key](options, factory()); + var result = callbacks[key].call(_this, options, factory()); + _this.cache[key] = result; return result; diff --git a/tests/test-module-test.js b/tests/test-module-test.js index 3f9bf6f23..7e748056c 100644 --- a/tests/test-module-test.js +++ b/tests/test-module-test.js @@ -83,3 +83,17 @@ test("can lookup factory registered in setup", function() { equal(service.get('purpose'), 'blabering'); }); + +moduleFor('component:x-foo', 'component:x-foo -- callback context', { + beforeSetup: function() { + setupRegistry(); + }, + + getSubjectName: function() { + return this.subjectName; + } +}); + +test("callbacks are called in the context of the module", function() { + equal(this.getSubjectName(), 'component:x-foo'); +});