Skip to content

Commit

Permalink
Merge pull request #50 from mbland/init-with-app-elem
Browse files Browse the repository at this point in the history
Pass the <div id="app"> to initApp()
  • Loading branch information
mbland committed Dec 16, 2023
2 parents 2b86a65 + bd161b7 commit 2d9a631
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions strcalc/src/main/frontend/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import Placeholder from './placeholder'
* demonstrate how to design much larger applications for testability.
* @param {Window} window - the browser window object
* @param {Document} document - a Document or DocumentFragment
* @param {Element} appElem - the parent Element containing all app components
*/
export default function initApp(window, document) {
Placeholder.init(window, document)
export default function initApp(window, document, appElem) {
Placeholder.init(window, document, appElem)
}
2 changes: 1 addition & 1 deletion strcalc/src/main/frontend/components/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('initial state after calling initApp', () => {
test('contains the "Hello, World!" placeholder', async () => {
const page = new StringCalculatorPage()

initApp(window, page.document)
initApp(window, page.document, page.appElem)

const e = page.placeholder()
expect(e.textContent).toContain('Hello, World!')
Expand Down
5 changes: 3 additions & 2 deletions strcalc/src/main/frontend/components/placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export default class Placeholder {
* Initializes the Placeholder within the document.
* @param {Window} window - the browser window object
* @param {Document} document - a Document or DocumentFragment
* @param {Element} appElem - the parent Element containing all app components
*/
static init(window, document) {
document.querySelector('#app').appendChild(Template({
static init(window, document, appElem) {
appElem.appendChild(Template({
message: 'Hello, World!',
url: 'https://en.wikipedia.org/wiki/%22Hello,_World!%22_program'
}))
Expand Down
6 changes: 3 additions & 3 deletions strcalc/src/main/frontend/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import initApp from './components/init'
* - main.test.js uses PageLoader to validate that initApp() fires on
* DOMContentLoaded.
* - init.test.js tests the initApp() method directly.
* @param {Window} window - the browser window object
* @param {Document} document - a Document or DocumentFragment
*/
document.addEventListener('DOMContentLoaded', () => initApp(window, document))
document.addEventListener('DOMContentLoaded', () => {
initApp(window, document, document.querySelector('#app'))
})
4 changes: 3 additions & 1 deletion strcalc/src/main/frontend/test/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import { fragment } from './helpers'
*/
export default class StringCalculatorPage {
document
appElem
#select

constructor(doc) {
this.document = doc !== undefined ? doc : fragment('<div id="app"></div>')
this.#select = selector => this.document.querySelector(`#app ${selector}`)
this.appElem = this.document.querySelector('#app')
this.#select = s => this.document.querySelector(`#${this.appElem.id} ${s}`)
}

placeholder() { return this.#select('.placeholder a') }
Expand Down

0 comments on commit 2d9a631

Please sign in to comment.