Skip to content

fix(ember): keep route hook context when performance-wrapping #3274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});