|
| 1 | +import { Action } from "../../../core/action" |
1 | 2 | import { LogControllerTestCase } from "../../cases/log_controller_test_case"
|
2 | 3 |
|
3 | 4 | export default class ActionTests extends LogControllerTestCase {
|
4 | 5 | identifier = "c"
|
5 | 6 | fixtureHTML = `
|
6 |
| - <div data-controller="c" data-action="keydown@window->c#log"> |
7 |
| - <button data-action="c#log"><span>Log</span></button> |
8 |
| - <div id="outer" data-action="click->c#log"> |
| 7 | + <div data-controller="c" data-action="keydown@window->c#log focus@outside->c#log"> |
| 8 | + <button id="outer-sibling-button" data-action="c#log"><span>Log</span></button> |
| 9 | + <div id="outer" data-action="hover@outside->c#log click->c#log"> |
9 | 10 | <div id="inner" data-controller="c" data-action="click->c#log keyup@window->c#log"></div>
|
10 | 11 | </div>
|
11 | 12 | <div id="multiple" data-action="click->c#log click->c#log2 mousedown->c#log"></div>
|
12 | 13 | </div>
|
13 |
| - <div id="outside"></div> |
| 14 | + <div id="outside"> |
| 15 | + <button type="button" id="outside-inner-button">Outside inner button</button> |
| 16 | + </div> |
14 | 17 | <svg id="svgRoot" data-controller="c" data-action="click->c#log">
|
15 | 18 | <circle id="svgChild" data-action="mousedown->c#log" cx="5" cy="5" r="5">
|
16 | 19 | </svg>
|
@@ -66,4 +69,37 @@ export default class ActionTests extends LogControllerTestCase {
|
66 | 69 | await this.triggerEvent("#svgChild", "mousedown")
|
67 | 70 | this.assertActions({ name: "log", eventType: "click" }, { name: "log", eventType: "mousedown" })
|
68 | 71 | }
|
| 72 | + |
| 73 | + async "test global 'outside' action that excludes outside elements"() { |
| 74 | + await this.triggerEvent("#outer-sibling-button", "focus") |
| 75 | + |
| 76 | + this.assertNoActions() |
| 77 | + |
| 78 | + await this.triggerEvent("#outside-inner-button", "focus") |
| 79 | + await this.triggerEvent("#svgRoot", "focus") |
| 80 | + |
| 81 | + this.assertActions({ name: "log", eventType: "focus" }, { name: "log", eventType: "focus" }) |
| 82 | + |
| 83 | + // validate that the action token string correctly resolves to the original action |
| 84 | + const attributeName = "data-action" |
| 85 | + const element = document.getElementById("outer") as Element |
| 86 | + const [content] = (element.getAttribute("data-action") || "").split(" ") |
| 87 | + const action = Action.forToken({ content, element, index: 0, attributeName }, this.application.schema) |
| 88 | + |
| 89 | + this.assert.equal("hover@outside->c#log", `${action}`) |
| 90 | + } |
| 91 | + |
| 92 | + async "test global 'outside' action that excludes the element with attached action"() { |
| 93 | + // an event from inside the controlled element but outside the element with the action |
| 94 | + await this.triggerEvent("#inner", "hover") |
| 95 | + |
| 96 | + // an event on the element with the action |
| 97 | + await this.triggerEvent("#outer", "hover") |
| 98 | + |
| 99 | + this.assertNoActions() |
| 100 | + |
| 101 | + // an event inside the controlled element but outside the element with the action |
| 102 | + await this.triggerEvent("#outer-sibling-button", "hover") |
| 103 | + this.assertActions({ name: "log", eventType: "hover" }) |
| 104 | + } |
69 | 105 | }
|
0 commit comments