-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modernize container-debug-adapter tests
- Loading branch information
Showing
1 changed file
with
97 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |