Skip to content

Commit

Permalink
test morph stream action events
Browse files Browse the repository at this point in the history
  • Loading branch information
omarluq committed Mar 14, 2024
1 parent 276ee38 commit f02bfb2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
39 changes: 17 additions & 22 deletions src/core/streams/actions/morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ import { dispatch } from "../../../util"
export default function morph(streamElement) {
const morphStyle = streamElement.hasAttribute("children-only") ? "innerHTML" : "outerHTML"
streamElement.targetElements.forEach((element) => {
try {
Idiomorph.morph(element, streamElement.templateContent, {
morphStyle: morphStyle,
ignoreActiveValue: true,
callbacks: {
beforeNodeAdded,
beforeNodeMorphed,
beforeAttributeUpdated,
beforeNodeRemoved,
afterNodeMorphed,
},
})
} catch (e) {
console.error(e)
}
Idiomorph.morph(element, streamElement.templateContent, {
morphStyle: morphStyle,
callbacks: {
beforeNodeAdded,
beforeNodeMorphed,
beforeAttributeUpdated,
beforeNodeRemoved,
afterNodeMorphed
}
})
})
}

Expand All @@ -34,10 +29,10 @@ function beforeNodeMorphed(target, newElement) {
if (target instanceof HTMLElement && !target.hasAttribute("data-turbo-permanent")) {
const event = dispatch("turbo:before-morph-element", {
cancelable: true,
target,
detail: {
target,
newElement,
},
newElement
}
})
return !event.defaultPrevented
}
Expand All @@ -50,8 +45,8 @@ function beforeAttributeUpdated(attributeName, target, mutationType) {
target,
detail: {
attributeName,
mutationType,
},
mutationType
}
})
return !event.defaultPrevented
}
Expand All @@ -61,8 +56,8 @@ function afterNodeMorphed(target, newElement) {
dispatch("turbo:morph-element", {
target,
detail: {
newElement,
},
newElement
}
})
}
}
2 changes: 1 addition & 1 deletion src/core/streams/stream_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ export const StreamActions = {

morph() {
morph(this)
},
}
}
16 changes: 16 additions & 0 deletions src/tests/fixtures/morph_stream_action.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html id="html">
<head>
<meta charset="utf-8">
<title>Morph Stream Action</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
<script src="/src/tests/fixtures/test.js"></script>
<meta name="turbo-refresh-method" content="replace">
</head>

<body>
<div id="message_1">
<div>Morph me</div>
</div>
</body>
</html>
48 changes: 48 additions & 0 deletions src/tests/functional/morph_stream_action_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test, expect } from "@playwright/test"
import { nextEventOnTarget, noNextEventOnTarget } from "../helpers/page"

test("dispatches a turbo:before-morph-element & turbo:morph-element for each morph stream action", async ({ page }) => {
await page.goto("/src/tests/fixtures/morph_stream_action.html")

await page.evaluate(() => {
window.Turbo.renderStreamMessage(`
<turbo-stream action="morph" target="message_1">
<template>
<div id="message_1">
<h1>Morphed</h1>
</div>
</template>
</turbo-stream>
`)
})

await nextEventOnTarget(page, "message_1", "turbo:before-morph-element")
await nextEventOnTarget(page, "message_1", "turbo:morph-element")
await expect(page.locator("#message_1")).toHaveText("Morphed")
})

test("preventing a turbo:before-morph-element prevents the morph", async ({ page }) => {
await page.goto("/src/tests/fixtures/morph_stream_action.html")

await page.evaluate(() => {
addEventListener("turbo:before-morph-element", (event) => {
event.preventDefault()
})
})

await page.evaluate(() => {
window.Turbo.renderStreamMessage(`
<turbo-stream action="morph" target="message_1">
<template>
<div id="message_1">
<h1>Morphed</h1>
</div>
</template>
</turbo-stream>
`)
})

await nextEventOnTarget(page, "message_1", "turbo:before-morph-element")
await noNextEventOnTarget(page, "message_1", "turbo:morph-element")
await expect(page.locator("#message_1")).toHaveText("Morph me")
})

0 comments on commit f02bfb2

Please sign in to comment.