From 9d763614f99e14b12a4eeb502525b4bcde830789 Mon Sep 17 00:00:00 2001 From: Dan Freeman Date: Tue, 2 Oct 2018 12:35:18 -0400 Subject: [PATCH] Remove automatic injection behavior --- README.md | 25 ++--------- addon/instance-initializers/debug-logger.ts | 18 -------- app/instance-initializers/debug-logger.js | 1 - package.json | 4 +- tests/acceptance/container-keys-test.ts | 38 ++++++++--------- tests/unit/utils/debug-logger-test.ts | 46 ++++++++++----------- yarn.lock | 30 ++++++++------ 7 files changed, 66 insertions(+), 96 deletions(-) delete mode 100644 addon/instance-initializers/debug-logger.ts delete mode 100644 app/instance-initializers/debug-logger.js diff --git a/README.md b/README.md index f3db2a5..1b9b649 100644 --- a/README.md +++ b/README.md @@ -56,13 +56,16 @@ Namespaces will automatically be differentiated by color, and the time between m ### Automatic Namespacing -By default, this addon will inject a `debug` method on to all routes, components, controllers, and services that are instantiated by your application's container. This method will automatically use its instance's container key as the namespace. +This addon exports a `debugLogger` function you can attach to a class definition. The resulting method will automatically use its instance's container key as the namespace. ```js // app/routes/index.js import Route from '@ember/route'; +import debugLogger from 'ember-debug-logger'; export default Route.extend({ + debug: debugLogger(); + activate() { this.debug('Hello from the application index.'); } @@ -70,23 +73,3 @@ export default Route.extend({ ``` ![image](https://cloud.githubusercontent.com/assets/108688/8262107/e0e71bb8-169d-11e5-9b74-9a895ed7e418.png) - - -You can also manually add the method when defining any other class that will be instantiated by the container. - -```js -// app/adapters/application.js -import DS from 'ember-data'; -import debugLogger from 'ember-debug-logger'; - -export default DS.JSONAPIAdapter.extend({ - debug: debugLogger(), - - init() { - this._super(...arguments); - this.debug('Hello from the application adapter!'); - } -}); -``` - -![image](https://cloud.githubusercontent.com/assets/108688/8262918/52e85f82-16a4-11e5-9b00-22e95e3848ae.png) diff --git a/addon/instance-initializers/debug-logger.ts b/addon/instance-initializers/debug-logger.ts deleted file mode 100644 index f04013a..0000000 --- a/addon/instance-initializers/debug-logger.ts +++ /dev/null @@ -1,18 +0,0 @@ -import debugLogger from 'ember-debug-logger/utils/debug-logger'; - -export function initialize(instance: any) { // The actual type of instance varies across Ember versions - // In 1.13, the app instance exposes the registry; in 2.x, it proxies it instead - let registry = instance.register ? instance : instance.registry; - let inject = registry.inject || registry.injection; - - registry.register('debug-logger:main', debugLogger(), { instantiate: false }); - - ['route', 'component', 'controller', 'service'].forEach((type) => { - inject.call(registry, type, 'debug', 'debug-logger:main'); - }); -} - -export default { - name: 'debug-logger', - initialize, -}; diff --git a/app/instance-initializers/debug-logger.js b/app/instance-initializers/debug-logger.js deleted file mode 100644 index 355e4d7..0000000 --- a/app/instance-initializers/debug-logger.js +++ /dev/null @@ -1 +0,0 @@ -export { default, initialize } from 'ember-debug-logger/instance-initializers/debug-logger'; diff --git a/package.json b/package.json index 8aad3a4..40015e2 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,8 @@ "devDependencies": { "@ember/optional-features": "^0.6.3", "@types/ember": "*", - "@types/ember-qunit": "^3.0.1", - "@types/ember-testing-helpers": "^0.0.3", + "@types/ember-qunit": "^3.4.2", + "@types/ember__test-helpers": "^0.7.5", "@types/qunit": "^2.0.31", "@types/rsvp": "^4.0.1", "@types/sinon": "^4.1.3", diff --git a/tests/acceptance/container-keys-test.ts b/tests/acceptance/container-keys-test.ts index 81feac4..195c292 100644 --- a/tests/acceptance/container-keys-test.ts +++ b/tests/acceptance/container-keys-test.ts @@ -1,41 +1,41 @@ import Route from '@ember/routing/route'; import Service from '@ember/service'; import debug from 'debug'; -import moduleForAcceptance from 'dummy/tests/helpers/module-for-acceptance'; -import { TestContext } from 'ember-test-helpers'; -import { test } from 'qunit'; +import debugLogger from 'ember-debug-logger'; +import { module, test } from 'qunit'; +import { setupApplicationTest } from 'ember-qunit'; import sinon, { SinonStub } from 'sinon'; +import { visit } from '@ember/test-helpers'; +import { TestContext } from 'ember-test-helpers'; -let log: SinonStub; +module('Acceptance | logging from container-managed objects', function(hooks) { + let log: SinonStub; -moduleForAcceptance('Acceptance | logging from container-managed objects', { - beforeEach(this: TestContext) { - this.application.register('route:application', Route); - this.application.register('service:my/test/module', Service); + setupApplicationTest(hooks); + + hooks.beforeEach(function(this: TestContext) { + this.owner.register('route:application', Route.extend({ debug: debugLogger() }), {}); + this.owner.register('service:my/test/module', Service.extend({ debug: debugLogger() }), {}); debug.enable('route:*, service:*'); log = sinon.stub(console, 'log'); - }, + }); - afterEach() { + hooks.afterEach(function() { log.restore(); debug.disable(); - }, -}); - -test('it automatically finds keys when attached to container-managed objects', function(assert) { - let lookup = (key: string) => (this.application as any).__container__.lookup(key); + }); - visit('/'); + test('it automatically finds keys when attached to container-managed objects', async function(assert) { + await visit('/'); - andThen(() => { - const appRoute = lookup('route:application'); + const appRoute = this.owner.lookup('route:application'); appRoute.debug('test message from the application route'); let [routeMessage] = log.lastCall.args; assert.ok(/route:application/.test(routeMessage), 'Route message should include its container key'); - const testService = lookup('service:my/test/module'); + const testService = this.owner.lookup('service:my/test/module'); testService.debug('test message from the mysterious service'); let [serviceMessage] = log.lastCall.args; diff --git a/tests/unit/utils/debug-logger-test.ts b/tests/unit/utils/debug-logger-test.ts index 7b02c39..c374e33 100644 --- a/tests/unit/utils/debug-logger-test.ts +++ b/tests/unit/utils/debug-logger-test.ts @@ -4,38 +4,38 @@ import debugLogger from 'ember-debug-logger'; import { module, test } from 'qunit'; import sinon, { SinonStub } from 'sinon'; -let log: SinonStub; +module('Unit | Utility | debug logger', function(hooks) { + let log: SinonStub; -module('Unit | Utility | debug logger', { - beforeEach() { + hooks.beforeEach(function() { debug.enable('test:sample-key'); log = sinon.stub(console, 'log'); - }, + }); - afterEach() { + hooks.afterEach(function() { debug.disable(); log.restore(); - }, -}); + }); -test('it honors an explicit key', function(assert) { - const logger = debugLogger('test:sample-key'); - logger('placeholder message'); + test('it honors an explicit key', function(assert) { + const logger = debugLogger('test:sample-key'); + logger('placeholder message'); - let [message] = A(log.args).get('lastObject') || ['']; - assert.ok(/test:sample-key/.test(message), 'Log entry should contain the namespace'); - assert.ok(/placeholder message/.test(message), 'Log entry should contain the message'); -}); + let [message] = A(log.args).get('lastObject') || ['']; + assert.ok(/test:sample-key/.test(message), 'Log entry should contain the namespace'); + assert.ok(/placeholder message/.test(message), 'Log entry should contain the message'); + }); -test('it throws with a useful message when it can\'t determine a key', function(assert) { - const logger = debugLogger(); + test('it throws with a useful message when it can\'t determine a key', function(assert) { + const logger = debugLogger(); - assert.throws(function() { - logger('message'); - }, /explicit key/); + assert.throws(function() { + logger('message'); + }, /explicit key/); - assert.throws(function() { - let object = { debug: logger }; - object.debug('message'); - }, /explicit key/); + assert.throws(function() { + let object = { debug: logger }; + object.debug('message'); + }, /explicit key/); + }); }); diff --git a/yarn.lock b/yarn.lock index 3faf6d2..09a347e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -106,10 +106,10 @@ dependencies: "@types/rsvp" "*" -"@types/ember-qunit@^3.0.1": - version "3.0.1" - resolved "https://registry.yarnpkg.com/@types/ember-qunit/-/ember-qunit-3.0.1.tgz#ddf7f25e75eb0a91f18e159369876bf02496dd38" - integrity sha512-qXstV0xyCL8aNbmjHbt2UbmD995cRLeqa4woYLLarTNp62AKJWA1Qy+90BZSyLuV0CxZsNAPo7+TpAdyVDUsaw== +"@types/ember-qunit@^3.4.2": + version "3.4.2" + resolved "https://registry.yarnpkg.com/@types/ember-qunit/-/ember-qunit-3.4.2.tgz#90a81afca9c5baada588f44a71919ee6cad5901f" + integrity sha512-1o99nikJmXgXhyL670P4zCEqBm9ebqmKDPgyeaL6p9xu1HFQEXw4T/LvwQb1NDXBm7eMia7SceUG5qoy8ADKcQ== dependencies: "@types/ember" "*" "@types/ember-test-helpers" "*" @@ -125,14 +125,6 @@ "@types/jquery" "*" "@types/rsvp" "*" -"@types/ember-testing-helpers@^0.0.3": - version "0.0.3" - resolved "https://registry.yarnpkg.com/@types/ember-testing-helpers/-/ember-testing-helpers-0.0.3.tgz#1a6cfc484b63d19ddd822c87e4dd710597db17d9" - integrity sha512-QG3QBBR7PFzz3zhLTbsZWBgk3cNQIZYVG6rbXKMM36+YP3dcSkkWQ6CRTyQImUIfgAkYPMaWqGlGEtkuanq3Bg== - dependencies: - "@types/jquery" "*" - "@types/rsvp" "*" - "@types/ember@*": version "2.8.8" resolved "https://registry.yarnpkg.com/@types/ember/-/ember-2.8.8.tgz#a3621385cb91a2ff4a9e2cd2a5f0f7c33b9f5d5d" @@ -142,6 +134,20 @@ "@types/jquery" "*" "@types/rsvp" "*" +"@types/ember__error@*": + version "3.0.1" + resolved "https://registry.yarnpkg.com/@types/ember__error/-/ember__error-3.0.1.tgz#1e915599d8d72cc4cf35473ee8a22d9cab036824" + integrity sha512-JON2moBi7Y6uf0bw1qrBalm7jxr4zQe4T2Q5flZLmPaRIrLTO5csQgffJGhwjFX0sAQgW1W2m9X8sPzgi8or1w== + +"@types/ember__test-helpers@^0.7.5": + version "0.7.5" + resolved "https://registry.yarnpkg.com/@types/ember__test-helpers/-/ember__test-helpers-0.7.5.tgz#219305edc96875995114952a3b7902a8fbe754a1" + integrity sha512-7E1XVzzGDXERJFxKSPAoiCs550DGEHiRtFBTPjiQ5m3gAV5Aeet0HP0/ODAccXE7PuI8wJXzrXkRgceviVfJxQ== + dependencies: + "@types/ember" "*" + "@types/ember__error" "*" + "@types/htmlbars-inline-precompile" "*" + "@types/handlebars@*": version "4.0.36" resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.0.36.tgz#ff57c77fa1ab6713bb446534ddc4d979707a3a79"