Skip to content

Commit b7bb3e3

Browse files
committed
Improve append and remove child helpers
You can now define the parent element in append You can now remove a child by a query selector
1 parent 0ed35a6 commit b7bb3e3

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,19 @@ Same as `tape` `t.only`.
138138

139139
The HTMLElement element where your test should work inside.
140140

141-
### `await t.appendChild(el, [msg])`
141+
### `await t.appendChild([parentElOrQuery], el, [msg])`
142142

143-
Takes an element, append it and then waits for onload. Asserts when complete with a `msg`.
143+
Takes an element `el`, append it and then waits for onload. You can also pass a different parent element or query selector `parentElOrQuery` to append to. Asserts when complete with a `msg`.
144144

145145
```js
146146
const newDiv = document.createElement('div')
147147
newDiv.innerText = 'New div to append'
148148
await t.appendChild(newDiv, 'Appended newDiv')
149149
```
150150

151-
### `await t.removeChild(el, [msg])`
151+
### `await t.removeChild(elementOrQuerySelector, [msg])`
152152

153-
Takes a loaded element `el` and removes it from the test element and then waits for onunload. Asserts when complete with a `msg`.
153+
Takes a loaded element `el` or query selector and removes it from its parent element and then waits for onunload. Asserts when complete with a `msg`.
154154

155155
### `await t.sleep(ms, [msg])`
156156

example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ vhs('A simple appended div in a simple html component', async t => {
5353
const newDiv = document.createElement('div')
5454
newDiv.innerText = 'New div to append'
5555
await t.appendChild(newDiv)
56+
await t.removeChild(newDiv, 'Removed child from test element')
5657
})

index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,16 @@ function create (delay, fn) {
120120
}
121121
return t.delay().then(() => t.pass(msg))
122122
},
123-
appendChild (el, msg = 'Appended child to test element') {
124-
t.element.appendChild(el)
125-
return t.onload(el, msg).then(t.delay)
123+
appendChild (parentElOrQuery, el, msg = 'Appended child to test element') {
124+
let parentEl = arguments.length >= 3 ? toElement(parentElOrQuery) : t.element
125+
let childEl = arguments.length >= 3 ? el : parentElOrQuery
126+
parentEl.appendChild(childEl)
127+
return t.onload(childEl, msg).then(t.delay)
126128
},
127-
removeChild (el, msg = 'Removed child from test element') {
128-
const unloadP = t.onload(el, msg).then(t.delay)
129-
t.element.removeChild(el)
130-
return unloadP
129+
removeChild (stringOrElement, msg = 'Removed child from its parent element') {
130+
const el = toElement(stringOrElement)
131+
el.remove()
132+
return t.onunload(el, msg).then(t.delay)
131133
},
132134
once (emitter, name, msg) {
133135
// t is expected to be an event emitter

0 commit comments

Comments
 (0)