Skip to content

Commit

Permalink
Merge pull request #53 from mbland/stringcalculatorpage-auto-appelemids
Browse files Browse the repository at this point in the history
StringCalculatorPage autogenerates app element IDs
  • Loading branch information
mbland committed Dec 17, 2023
2 parents 051bde7 + ccc921b commit 5fb43ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
11 changes: 6 additions & 5 deletions strcalc/src/main/frontend/components/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import initApp from './init'
import { afterEach, describe, expect, test } from 'vitest'
import { afterAll, afterEach, describe, expect, test } from 'vitest'
import StringCalculatorPage from '../test/page'

// @vitest-environment jsdom
describe('initial state after calling initApp', () => {
afterEach(() => StringCalculatorPage.cleanup())
const page = StringCalculatorPage.new()

test('contains the "Hello, World!" placeholder', async () => {
const page = StringCalculatorPage.new('app-init')
afterEach(() => page.clear())
afterAll(() => page.remove())

initApp({ window, document, appElem: page.appElem })
test('contains the "Hello, World!" placeholder', async () => {
initApp({ appElem: page.appElem })

const e = page.placeholder()
expect(e.textContent).toContain('Hello, World!')
Expand Down
15 changes: 7 additions & 8 deletions strcalc/src/main/frontend/test/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @see https://www.selenium.dev/documentation/test_practices/design_strategies/
*/
export default class StringCalculatorPage {
static #pages = []
static #nextId = 0

appElem
#select
Expand All @@ -23,17 +23,16 @@ export default class StringCalculatorPage {
this.#select = sel => doc.querySelector(`#${appElem.id} ${sel}`)
}

static new(appElemId) {
static new() {
const appElem = document.createElement('div')
appElem.id = appElemId
appElem.id = `test-app-${this.#nextId++}`
document.body.appendChild(appElem)

const page = new StringCalculatorPage(appElem)
this.#pages.push(page)
return page
return new StringCalculatorPage(appElem)
}

static cleanup() { this.#pages.forEach(p => p.appElem.remove()) }
clear() { this.appElem.replaceChildren() }

remove() { this.appElem.remove() }

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

0 comments on commit 5fb43ea

Please sign in to comment.