Skip to content

Commit

Permalink
Ensure that the Application.start static method uses overridden class (
Browse files Browse the repository at this point in the history
…#603)

- When using const application = MyApplication.start() - it now will use the actual MyApplication class instead of the non-extended Application class
- Fixes #586
  • Loading branch information
lb- committed Nov 18, 2022
1 parent af88dbf commit 88b2641
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/application.ts
Expand Up @@ -17,7 +17,7 @@ export class Application implements ErrorHandler {
debug = false

static start(element?: Element, schema?: Schema): Application {
const application = new Application(element, schema)
const application = new this(element, schema)
application.start()
return application
}
Expand Down
51 changes: 51 additions & 0 deletions src/tests/modules/core/extending_application_tests.ts
@@ -0,0 +1,51 @@
import { Application } from "../../../core/application"
import { DOMTestCase } from "../../cases/dom_test_case"
import { ActionDescriptorFilter } from "src/core/action_descriptor"

const mockCallback: { (label: string): void; lastCall: string | null } = (label: string) => {
mockCallback.lastCall = label
}
mockCallback.lastCall = null

class TestApplicationWithCustomBehavior extends Application {
registerActionOption(name: string, filter: ActionDescriptorFilter): void {
mockCallback(`registerActionOption:${name}`)
super.registerActionOption(name, filter)
}
}

export default class ExtendingApplicationTests extends DOMTestCase {
application!: Application

async runTest(testName: string) {
try {
// use the documented way to start & reference only the returned application instance
this.application = TestApplicationWithCustomBehavior.start(this.fixtureElement)
await super.runTest(testName)
} finally {
this.application.stop()
}
}

async setup() {
mockCallback.lastCall = null
}

async teardown() {
mockCallback.lastCall = null
}

async "test extended class method is supported when using MyApplication.start()"() {
this.assert.equal(mockCallback.lastCall, null)

const mockTrue = () => true
this.application.registerActionOption("kbd", mockTrue)
this.assert.equal(this.application.actionDescriptorFilters["kbd"], mockTrue)
this.assert.equal(mockCallback.lastCall, "registerActionOption:kbd")

const mockFalse = () => false
this.application.registerActionOption("xyz", mockFalse)
this.assert.equal(this.application.actionDescriptorFilters["xyz"], mockFalse)
this.assert.equal(mockCallback.lastCall, "registerActionOption:xyz")
}
}

0 comments on commit 88b2641

Please sign in to comment.