Skip to content

Commit

Permalink
Merge pull request #52 from mbland/pass-initapp-params-as-object
Browse files Browse the repository at this point in the history
Pass initApp() params as object
  • Loading branch information
mbland committed Dec 17, 2023
2 parents 26e7f3f + 75681ad commit 051bde7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 27 deletions.
9 changes: 4 additions & 5 deletions strcalc/src/main/frontend/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import Placeholder from './placeholder'
*
* This is a teaching example that contains minimal business logic in order to
* 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
* @param {object} params - parameters made available to all initializers
* @param {Element} params.appElem - parent Element containing all components
*/
export default function initApp(window, document, appElem) {
Placeholder.init(window, document, appElem)
export default function initApp(params) {
[Placeholder].forEach(c => c.init(params))
}
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 @@ -15,7 +15,7 @@ describe('initial state after calling initApp', () => {
test('contains the "Hello, World!" placeholder', async () => {
const page = StringCalculatorPage.new('app-init')

initApp(window, document, page.appElem, {})
initApp({ window, document, appElem: page.appElem })

const e = page.placeholder()
expect(e.textContent).toContain('Hello, World!')
Expand Down
7 changes: 3 additions & 4 deletions strcalc/src/main/frontend/components/placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import Template from './placeholder.hbs'
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
* @param {object} params - parameters made available to all initializers
* @param {Element} params.appElem - parent Element containing all components
*/
static init(window, document, appElem) {
static init({ appElem }) {
appElem.appendChild(Template({
message: 'Hello, World!',
url: 'https://en.wikipedia.org/wiki/%22Hello,_World!%22_program'
Expand Down
11 changes: 8 additions & 3 deletions strcalc/src/main/frontend/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import initApp from './components/init'
* DOMContentLoaded.
* - init.test.js tests the initApp() method directly.
*/
document.addEventListener('DOMContentLoaded', () => {
initApp(window, document, document.querySelector('#app'))
})
document.addEventListener(
'DOMContentLoaded',
() => {
const appElem = document.querySelector('#app')
initApp({ appElem })
},
{ once: true }
)
14 changes: 0 additions & 14 deletions strcalc/src/main/frontend/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@
* @module test-helpers
*/

/**
* Produces a DocumentFragment from an HTML string.
*
* Useful for running tests on Document-like objects that don't impact the
* actual global Document.
* @param {string} innerHtml - HTML from which to produce a DocumentFragment
* @returns {DocumentFragment} - DocumentFragment containing innerHtml elements
*/
export function fragment(innerHtml) {
const t = document.createElement('template')
t.innerHTML = innerHtml
return t.content
}

/**
* Enables tests to load page URLs both in the browser and in Node using JSDom.
*/
Expand Down

0 comments on commit 051bde7

Please sign in to comment.