/
loading_tests.ts
61 lines (50 loc) · 2.2 KB
/
loading_tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { ApplicationTestCase } from "../../cases/application_test_case"
import { LogController } from "../../controllers/log_controller"
class UnloadableController extends LogController {
static get shouldLoad() {
return false
}
}
class LoadableController extends LogController {
static get shouldLoad() {
return true
}
}
class AfterLoadController extends LogController {
static afterLoad(identifier: string, application: any) {
const newElement = document.createElement("div")
newElement.classList.add("after-load-test")
newElement.setAttribute(application.schema.controllerAttribute, identifier)
application.element.append(newElement)
document.dispatchEvent(new CustomEvent("test", { detail: { identifier, application } }))
}
}
export default class ApplicationTests extends ApplicationTestCase {
fixtureHTML = `<div data-controller="loadable"><div data-controller="unloadable">`
"test module with false shouldLoad should not load when registering"() {
this.application.register("unloadable", UnloadableController)
this.assert.equal(this.controllers.length, 0)
}
"test module with true shouldLoad should load when registering"() {
this.application.register("loadable", LoadableController)
this.assert.equal(this.controllers.length, 1)
}
"test module with afterLoad method should be triggered when registered"() {
// set up an event listener to track the params passed into the AfterLoadController
let data: { application?: any; identifier?: string } = {}
document.addEventListener("test", (({ detail }: CustomEvent) => {
data = detail
}) as EventListener)
this.assert.equal(data.identifier, undefined)
this.assert.equal(data.application, undefined)
this.application.register("after-load", AfterLoadController)
// check the DOM element has been added based on params provided
this.assert.equal(this.findElements('[data-controller="after-load"]').length, 1)
// check that static method was correctly called with the params
this.assert.equal(data.identifier, "after-load")
this.assert.equal(data.application, this.application)
}
get controllers() {
return this.application.controllers as LogController[]
}
}