|
| 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