diff --git a/addon/app/container-debug-adapter.js b/addon/app/container-debug-adapter.js new file mode 100644 index 00000000..9efe8291 --- /dev/null +++ b/addon/app/container-debug-adapter.js @@ -0,0 +1 @@ +export { default } from "ember-resolver/resolvers/classic/container-debug-adapter"; diff --git a/addon/app/initializers/container-debug-adapter.js b/addon/app/initializers/container-debug-adapter.js deleted file mode 100644 index 4f6794d6..00000000 --- a/addon/app/initializers/container-debug-adapter.js +++ /dev/null @@ -1,11 +0,0 @@ -import ContainerDebugAdapter from 'ember-resolver/resolvers/classic/container-debug-adapter'; - -export default { - name: 'container-debug-adapter', - - initialize() { - let app = arguments[1] || arguments[0]; - - app.register('container-debug-adapter:main', ContainerDebugAdapter); - } -}; diff --git a/test-app/tests/unit/container-debug-adapter-test.js b/test-app/tests/unit/container-debug-adapter-test.js index a83a7d77..c23e5ec0 100644 --- a/test-app/tests/unit/container-debug-adapter-test.js +++ b/test-app/tests/unit/container-debug-adapter-test.js @@ -1,115 +1,105 @@ -/* eslint-disable ember/no-classic-classes */ - -import { run } from '@ember/runloop'; -import Application from '@ember/application'; -import { module, test } from 'qunit'; -import Resolver from 'ember-resolver/resolver'; -import ContainerDebugAdapter from 'ember-resolver/resolvers/classic/container-debug-adapter'; -import ContainerDebugAdapterInitializer from 'test-app/initializers/container-debug-adapter'; - -let containerDebugAdapter, App; - -let modules = {}; -function def(_module) { - modules[_module] = {}; -} - -function undef(_module) { - if (_module) { - delete modules[_module]; - } else { - modules = {}; - } -} - +/* globals define, requirejs */ +import { setupTest } from "ember-qunit"; +import { module, test } from "qunit"; -module("Container Debug Adapter Tests", { - beforeEach:function() { - let BaseApplication = Application.extend({ - Resolver, - ContainerDebugAdapter, - modulePrefix: 'appkit', +module("Container Debug Adapter Tests", function (hooks) { + setupTest(hooks); - init() { - this._super(...arguments); - this.deferReadiness(); - }, - - toString() { - return 'App'; - } - }); + let containerDebugAdapter; - BaseApplication.initializer(ContainerDebugAdapterInitializer); + let modulesToReset; - run(function() { - App = BaseApplication.create(); - }); - - run(function() { - containerDebugAdapter = App.__container__.lookup('container-debug-adapter:main'); - containerDebugAdapter._moduleRegistry._entries = modules; - }); - }, - afterEach: function() { - run(function() { - containerDebugAdapter.destroy(); - App.destroy(); - App = null; - }); - undef(); + function def(moduleName) { + define(moduleName, [], function () {}); + modulesToReset.push(moduleName); } -}); - -test("can access Container Debug Adapter which can catalog typical entries by type", function(assert) { - assert.equal(containerDebugAdapter.canCatalogEntriesByType('model'), true, "canCatalogEntriesByType should return false for model"); - assert.equal(containerDebugAdapter.canCatalogEntriesByType('template'), false, "canCatalogEntriesByType should return false for template"); - assert.equal(containerDebugAdapter.canCatalogEntriesByType('controller'), true, "canCatalogEntriesByType should return true for controller"); - assert.equal(containerDebugAdapter.canCatalogEntriesByType('route'), true, "canCatalogEntriesByType should return true for route"); - assert.equal(containerDebugAdapter.canCatalogEntriesByType('view'), true, "canCatalogEntriesByType should return true for view"); -}); - -test("the default ContainerDebugAdapter catalogs controller entries", function(assert) { - def('appkit/controllers/foo'); - def('appkit/controllers/users/foo'); - - let controllers = containerDebugAdapter.catalogEntriesByType('controller'); - assert.equal(controllers.length, 2, "controllers discovered"); - assert.equal(controllers[0], 'foo', "found the right class"); - assert.equal(controllers[1], 'users/foo', "the name is correct"); -}); - -test("Does not duplicate entries", function(assert) { - def('appkit/models/foo'); - def('appkit/more/models/foo'); - - let models = containerDebugAdapter.catalogEntriesByType('model'); - - assert.equal(models.length, 1, "Only one is returned"); - assert.equal(models[0], 'foo', "the name is correct"); -}); - -test("Pods support", function(assert) { - def('appkit/user/model'); - def('appkit/post/model'); - - let models = containerDebugAdapter.catalogEntriesByType('model'); + hooks.beforeEach(function () { + modulesToReset = []; + containerDebugAdapter = this.owner.lookup("container-debug-adapter:main"); + }); - assert.equal(models.length, 2, "All models are found"); - assert.equal(models[0], 'user', "the name is correct"); - assert.equal(models[1], 'post', "the name is correct"); -}); - -test("Pods podModulePrefix support", function(assert) { - App.podModulePrefix = 'my-prefix'; - - def('my-prefix/user/model'); - def('my-prefix/users/user/model'); - - let models = containerDebugAdapter.catalogEntriesByType('model'); - - assert.equal(models.length, 2, "models discovered"); - assert.equal(models[0], 'user', "the name is correct"); - assert.equal(models[1], 'users/user', "the name is correct"); + hooks.afterEach(function () { + modulesToReset.forEach((moduleName) => { + requirejs.unsee(moduleName); + delete requirejs.entries[moduleName]; + }); + }); + + test("can access Container Debug Adapter which can catalog typical entries by type", function (assert) { + assert.equal( + containerDebugAdapter.canCatalogEntriesByType("model"), + true, + "canCatalogEntriesByType should return false for model" + ); + assert.equal( + containerDebugAdapter.canCatalogEntriesByType("template"), + false, + "canCatalogEntriesByType should return false for template" + ); + assert.equal( + containerDebugAdapter.canCatalogEntriesByType("controller"), + true, + "canCatalogEntriesByType should return true for controller" + ); + assert.equal( + containerDebugAdapter.canCatalogEntriesByType("route"), + true, + "canCatalogEntriesByType should return true for route" + ); + assert.equal( + containerDebugAdapter.canCatalogEntriesByType("view"), + true, + "canCatalogEntriesByType should return true for view" + ); + }); + + test("the default ContainerDebugAdapter catalogs controller entries", function (assert) { + def("test-app/controllers/foo"); + def("test-app/controllers/users/foo"); + + let controllers = containerDebugAdapter.catalogEntriesByType("controller"); + + assert.ok(controllers.includes("foo"), "foo controller was discovered"); + assert.ok( + controllers.includes("users/foo"), + "users/foo controller was discovered" + ); + }); + + test("Does not duplicate entries", function (assert) { + def("test-app/models/foo"); + def("test-app/more/models/foo"); + + let models = containerDebugAdapter.catalogEntriesByType("model"); + + assert.equal(models.length, 1, "Only one is returned"); + assert.equal(models[0], "foo", "the name is correct"); + }); + + test("Pods support", function (assert) { + def("test-app/user/model"); + def("test-app/post/model"); + + let models = containerDebugAdapter.catalogEntriesByType("model"); + + assert.equal(models.length, 2, "All models are found"); + assert.equal(models[0], "user", "the name is correct"); + assert.equal(models[1], "post", "the name is correct"); + }); + + test("Pods podModulePrefix support", function (assert) { + const app = this.owner.lookup("application:main"); + + app.podModulePrefix = "my-prefix"; + + def("my-prefix/user/model"); + def("my-prefix/users/user/model"); + + let models = containerDebugAdapter.catalogEntriesByType("model"); + + assert.equal(models.length, 2, "models discovered"); + assert.equal(models[0], "user", "the name is correct"); + assert.equal(models[1], "users/user", "the name is correct"); + }); });