Skip to content

Commit a40a0da

Browse files
fix(ember): keep route hook context when performance-wrapping (#3274)
1 parent 58b2ba1 commit a40a0da

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

packages/ember/addon/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,22 @@ export const instrumentRoutePerformance = (BaseRoute: any) => {
7171
return {
7272
[BaseRoute.name]: class extends BaseRoute {
7373
beforeModel(...args: any[]) {
74-
return instrumentFunction('ember.route.beforeModel', (<any>this).fullRouteName, super.beforeModel, args);
74+
return instrumentFunction('ember.route.beforeModel', (<any>this).fullRouteName, super.beforeModel.bind(this), args);
7575
}
7676

7777
async model(...args: any[]) {
78-
return instrumentFunction('ember.route.model', (<any>this).fullRouteName, super.model, args);
78+
return instrumentFunction('ember.route.model', (<any>this).fullRouteName, super.model.bind(this), args);
7979
}
8080

8181
async afterModel(...args: any[]) {
82-
return instrumentFunction('ember.route.afterModel', (<any>this).fullRouteName, super.afterModel, args);
82+
return instrumentFunction('ember.route.afterModel', (<any>this).fullRouteName, super.afterModel.bind(this), args);
8383
}
8484

8585
async setupController(...args: any[]) {
8686
return instrumentFunction(
8787
'ember.route.setupController',
8888
(<any>this).fullRouteName,
89-
super.setupController,
89+
super.setupController.bind(this),
9090
args,
9191
);
9292
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'ember-qunit';
3+
import Route from '@ember/routing/route';
4+
import { instrumentRoutePerformance } from '@sentry/ember';
5+
import sinon from 'sinon'
6+
import { setupSentryTest } from '../helpers/setup-sentry';
7+
8+
module('Unit | Utility | instrument-route-performance', function(hooks) {
9+
setupTest(hooks);
10+
setupSentryTest(hooks);
11+
12+
test('wrapped Route hooks maintain the current context', function(assert) {
13+
const beforeModel = sinon.spy();
14+
const model = sinon.spy();
15+
const afterModel = sinon.spy();
16+
const setupController = sinon.spy();
17+
18+
class DummyRoute extends Route {
19+
beforeModel(...args: any[]) {
20+
return beforeModel.call(this, ...args);
21+
}
22+
23+
model(...args: any[]) {
24+
return model.call(this, ...args);
25+
}
26+
27+
afterModel(...args: any[]) {
28+
return afterModel.call(this, ...args);
29+
}
30+
31+
setupController(...args: any[]) {
32+
return setupController.call(this, ...args);
33+
}
34+
}
35+
36+
const InstrumentedDummyRoute = instrumentRoutePerformance(DummyRoute);
37+
38+
this.owner.register('route:dummy', InstrumentedDummyRoute);
39+
40+
const route = this.owner.lookup('route:dummy');
41+
42+
route.beforeModel('foo');
43+
44+
assert.ok(beforeModel.calledOn(route), "The context for `beforeModel` is the route");
45+
assert.ok(beforeModel.calledWith('foo'), 'The arguments for `beforeModel` are passed through');
46+
47+
route.model('bar');
48+
49+
assert.ok(model.calledOn(route), "The context for `model` is the route");
50+
assert.ok(model.calledWith('bar'), 'The arguments for `model` are passed through');
51+
52+
route.afterModel('bax');
53+
54+
assert.ok(afterModel.calledOn(route), "The context for `afterModel` is the route");
55+
assert.ok(afterModel.calledWith('bax'), 'The arguments for `afterModel` are passed through');
56+
57+
route.setupController('baz');
58+
59+
assert.ok(setupController.calledOn(route), "The context for `setupController` is the route");
60+
assert.ok(setupController.calledWith('baz'), 'The arguments for `setupController` are passed through');
61+
});
62+
});

0 commit comments

Comments
 (0)