Skip to content

Commit

Permalink
fix(ember): keep route hook context when performance-wrapping (#3274)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Mar 9, 2021
1 parent 58b2ba1 commit a40a0da
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/ember/addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ export const instrumentRoutePerformance = (BaseRoute: any) => {
return {
[BaseRoute.name]: class extends BaseRoute {
beforeModel(...args: any[]) {
return instrumentFunction('ember.route.beforeModel', (<any>this).fullRouteName, super.beforeModel, args);
return instrumentFunction('ember.route.beforeModel', (<any>this).fullRouteName, super.beforeModel.bind(this), args);
}

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

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

async setupController(...args: any[]) {
return instrumentFunction(
'ember.route.setupController',
(<any>this).fullRouteName,
super.setupController,
super.setupController.bind(this),
args,
);
}
Expand Down
62 changes: 62 additions & 0 deletions packages/ember/tests/unit/instrument-route-performance-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import Route from '@ember/routing/route';
import { instrumentRoutePerformance } from '@sentry/ember';
import sinon from 'sinon'
import { setupSentryTest } from '../helpers/setup-sentry';

module('Unit | Utility | instrument-route-performance', function(hooks) {
setupTest(hooks);
setupSentryTest(hooks);

test('wrapped Route hooks maintain the current context', function(assert) {
const beforeModel = sinon.spy();
const model = sinon.spy();
const afterModel = sinon.spy();
const setupController = sinon.spy();

class DummyRoute extends Route {
beforeModel(...args: any[]) {
return beforeModel.call(this, ...args);
}

model(...args: any[]) {
return model.call(this, ...args);
}

afterModel(...args: any[]) {
return afterModel.call(this, ...args);
}

setupController(...args: any[]) {
return setupController.call(this, ...args);
}
}

const InstrumentedDummyRoute = instrumentRoutePerformance(DummyRoute);

this.owner.register('route:dummy', InstrumentedDummyRoute);

const route = this.owner.lookup('route:dummy');

route.beforeModel('foo');

assert.ok(beforeModel.calledOn(route), "The context for `beforeModel` is the route");
assert.ok(beforeModel.calledWith('foo'), 'The arguments for `beforeModel` are passed through');

route.model('bar');

assert.ok(model.calledOn(route), "The context for `model` is the route");
assert.ok(model.calledWith('bar'), 'The arguments for `model` are passed through');

route.afterModel('bax');

assert.ok(afterModel.calledOn(route), "The context for `afterModel` is the route");
assert.ok(afterModel.calledWith('bax'), 'The arguments for `afterModel` are passed through');

route.setupController('baz');

assert.ok(setupController.calledOn(route), "The context for `setupController` is the route");
assert.ok(setupController.calledWith('baz'), 'The arguments for `setupController` are passed through');
});
});

0 comments on commit a40a0da

Please sign in to comment.