Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2027303
commit 2235047
Showing
3 changed files
with
121 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/@stimulus/core/src/tests/modules/legacy_target_tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ControllerTestCase } from "../cases/controller_test_case" | ||
import { TargetController } from "../controllers/target_controller" | ||
|
||
export default class LegacyTargetTests extends ControllerTestCase(TargetController) { | ||
fixtureHTML = ` | ||
<div data-controller="${this.identifier}"> | ||
<div data-target="${this.identifier}.alpha" id="alpha1"></div> | ||
<div data-target="${this.identifier}.alpha" id="alpha2"></div> | ||
<div data-target="${this.identifier}.beta" data-${this.identifier}-target="gamma" id="beta1"> | ||
<div data-target="${this.identifier}.gamma" id="gamma1"></div> | ||
</div> | ||
<div data-controller="${this.identifier}" id="child"> | ||
<div data-target="${this.identifier}.delta" id="delta1"></div> | ||
</div> | ||
<textarea data-target="${this.identifier}.input" id="input1"></textarea> | ||
</div> | ||
` | ||
|
||
"test TargetSet#find"() { | ||
this.assert.equal(this.controller.targets.find("alpha"), this.findElement("#alpha1")) | ||
} | ||
|
||
"test TargetSet#find prefers scoped target attributes"() { | ||
this.assert.equal(this.controller.targets.find("gamma"), this.findElement("#beta1")) | ||
} | ||
|
||
"test TargetSet#findAll"() { | ||
this.assert.deepEqual( | ||
this.controller.targets.findAll("alpha"), | ||
this.findElements("#alpha1", "#alpha2") | ||
) | ||
} | ||
|
||
"test TargetSet#findAll prioritizes scoped target attributes"() { | ||
this.assert.deepEqual( | ||
this.controller.targets.findAll("gamma"), | ||
this.findElements("#beta1", "#gamma1") | ||
) | ||
} | ||
|
||
"test TargetSet#findAll with multiple arguments"() { | ||
this.assert.deepEqual( | ||
this.controller.targets.findAll("alpha", "beta"), | ||
this.findElements("#alpha1", "#alpha2", "#beta1") | ||
) | ||
} | ||
|
||
"test TargetSet#has"() { | ||
this.assert.equal(this.controller.targets.has("gamma"), true) | ||
this.assert.equal(this.controller.targets.has("delta"), false) | ||
} | ||
|
||
"test TargetSet#find ignores child controller targets"() { | ||
this.assert.equal(this.controller.targets.find("delta"), null) | ||
this.findElement("#child").removeAttribute("data-controller") | ||
this.assert.equal(this.controller.targets.find("delta"), this.findElement("#delta1")) | ||
} | ||
|
||
"test linked target properties"() { | ||
this.assert.equal(this.controller.betaTarget, this.findElement("#beta1")) | ||
this.assert.deepEqual(this.controller.betaTargets, this.findElements("#beta1")) | ||
this.assert.equal(this.controller.hasBetaTarget, true) | ||
} | ||
|
||
"test inherited linked target properties"() { | ||
this.assert.equal(this.controller.alphaTarget, this.findElement("#alpha1")) | ||
this.assert.deepEqual(this.controller.alphaTargets, this.findElements("#alpha1", "#alpha2")) | ||
} | ||
|
||
"test singular linked target property throws an error when no target is found"() { | ||
this.findElement("#beta1").removeAttribute("data-target") | ||
this.assert.equal(this.controller.hasBetaTarget, false) | ||
this.assert.equal(this.controller.betaTargets.length, 0) | ||
this.assert.throws(() => this.controller.betaTarget) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2235047
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case anyone needs it, I made this regular expression to migrate: