Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringCalculatorPage autogenerates app element IDs #53

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') }
}