Skip to content

Commit

Permalink
Streams searching for single target inside a template
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/elements/stream_element.js
#	src/tests/functional/stream_tests.js
  • Loading branch information
viniciusoyama committed Jun 11, 2024
1 parent 4bf703b commit 5c98a6a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/elements/stream_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,27 @@ export class StreamElement extends HTMLElement {
}

get targetElementsById() {
const elements = []
const element = this.ownerDocument?.getElementById(this.target)

Check failure on line 172 in src/elements/stream_element.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
if (element !== null) {
return [element]
} else {
return []
elements.push(element)
}
}

for (const template of this.ownerDocument?.querySelectorAll("template")) {

Check failure on line 177 in src/elements/stream_element.js

View workflow job for this annotation

GitHub Actions / build

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError
const elementInTemplate = template.content.getElementById(this.target)

if (elementInTemplate !== null) {
elements.push(elementInTemplate)
}
}

return elements
}

get targetElementsByQuery() {
let elements = [...this.ownerDocument?.querySelectorAll(this.targets)];

Check failure on line 189 in src/elements/stream_element.js

View workflow job for this annotation

GitHub Actions / build

'elements' is never reassigned. Use 'const' instead

Check failure on line 189 in src/elements/stream_element.js

View workflow job for this annotation

GitHub Actions / build

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError

Check failure on line 189 in src/elements/stream_element.js

View workflow job for this annotation

GitHub Actions / build

Extra semicolon

for (const template of this.ownerDocument?.querySelectorAll("template")) {

Check failure on line 191 in src/elements/stream_element.js

View workflow job for this annotation

GitHub Actions / build

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError
elements.push(...template.content.querySelectorAll(this.targets))
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests/fixtures/stream.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</div>

<template id="messages_template">
<div class="messages">
<div id="messages" class="messages">
<div class="message">Message inside template</div>
</div>
</template>
Expand Down
23 changes: 22 additions & 1 deletion src/tests/functional/stream_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test.beforeEach(async ({ page }) => {
await readEventLogs(page)
})

test("receiving a stream message", async ({ page }) => {
test("receiving a stream message for single target", async ({ page }) => {
const messages = await page.locator("#messages .message")

assert.deepEqual(await messages.allTextContents(), ["First"])
Expand All @@ -25,6 +25,27 @@ test("receiving a stream message", async ({ page }) => {
assert.deepEqual(await messages.allTextContents(), ["First", "Hello world!"])
})

test("receiving a stream message for single target in a template", async ({ page }) => {
const templateNode = await page.locator("#messages_template")

const fetchMessagesInTemplateNode = (node) => {
const messagesNode = node.content.querySelector("#messages")
return messagesNode.innerText.trim().split(/\s{2,}/g);

Check failure on line 33 in src/tests/functional/stream_tests.js

View workflow job for this annotation

GitHub Actions / build

Extra semicolon

}

let messagesInTemplate = await templateNode.evaluate(fetchMessagesInTemplateNode)

assert.deepEqual(messagesInTemplate, ["Message inside template"])

await page.click("#append-target button")
await nextBeat()

messagesInTemplate = await templateNode.evaluate(fetchMessagesInTemplateNode)

assert.deepEqual(messagesInTemplate, ["Message inside template", "Hello world!"])
})

test("dispatches a turbo:before-stream-render event", async ({ page }) => {
await page.click("#append-target button")
await nextEventNamed(page, "turbo:submit-end")
Expand Down

0 comments on commit 5c98a6a

Please sign in to comment.