-
Notifications
You must be signed in to change notification settings - Fork 415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed value change observers not firing #697
Open
RBastianini
wants to merge
1
commit into
hotwired:main
Choose a base branch
from
RBastianini:680-change-observer-not-firing
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,92 @@ | ||
import ValueTests from "./value_tests" | ||
|
||
export default class ValueCaseInsensitiveTests extends ValueTests { | ||
identifier = "Test" | ||
|
||
fixtureHTML = ` | ||
<div data-controller="${this.identifier}" | ||
data-${this.identifier}-shadowed-boolean-value="true" | ||
data-${this.identifier}-numeric-value="123" | ||
data-${this.identifier}-string-value="ok" | ||
data-${this.identifier}-ids-value="[1,2,3]" | ||
data-${this.identifier}-options-value='{"one":[2,3]}' | ||
data-${this.identifier}-time-24hr-value="true"> | ||
</div> | ||
` | ||
|
||
async "test changed callbacks"() { | ||
this.assert.deepEqual(this.controller.loggedNumericValues, [123]) | ||
this.assert.deepEqual(this.controller.oldLoggedNumericValues, [0]) | ||
|
||
this.controller.numericValue = 0 | ||
await this.nextFrame | ||
this.assert.deepEqual(this.controller.loggedNumericValues, [123, 0]) | ||
this.assert.deepEqual(this.controller.oldLoggedNumericValues, [0, 123]) | ||
|
||
this.set("numeric-value", "1") | ||
await this.nextFrame | ||
this.assert.deepEqual(this.controller.loggedNumericValues, [123, 0, 1]) | ||
this.assert.deepEqual(this.controller.oldLoggedNumericValues, [0, 123, 0]) | ||
} | ||
|
||
async "test changed callbacks for object"() { | ||
this.assert.deepEqual(this.controller.optionsValues, [{ one: [2, 3] }]) | ||
this.assert.deepEqual(this.controller.oldOptionsValues, [{}]) | ||
|
||
this.controller.optionsValue = { person: { name: "John", age: 42, active: true } } | ||
await this.nextFrame | ||
this.assert.deepEqual(this.controller.optionsValues, [ | ||
{ one: [2, 3] }, | ||
{ person: { name: "John", age: 42, active: true } }, | ||
]) | ||
this.assert.deepEqual(this.controller.oldOptionsValues, [{}, { one: [2, 3] }]) | ||
|
||
this.set("options-value", "{}") | ||
await this.nextFrame | ||
this.assert.deepEqual(this.controller.optionsValues, [ | ||
{ one: [2, 3] }, | ||
{ person: { name: "John", age: 42, active: true } }, | ||
{}, | ||
]) | ||
this.assert.deepEqual(this.controller.oldOptionsValues, [ | ||
{}, | ||
{ one: [2, 3] }, | ||
{ person: { name: "John", age: 42, active: true } }, | ||
]) | ||
} | ||
|
||
async "test default values trigger changed callbacks"() { | ||
this.assert.deepEqual(this.controller.loggedMissingStringValues, [""]) | ||
this.assert.deepEqual(this.controller.oldLoggedMissingStringValues, [undefined]) | ||
|
||
this.controller.missingStringValue = "hello" | ||
await this.nextFrame | ||
this.assert.deepEqual(this.controller.loggedMissingStringValues, ["", "hello"]) | ||
this.assert.deepEqual(this.controller.oldLoggedMissingStringValues, [undefined, ""]) | ||
|
||
this.controller.missingStringValue = undefined as any | ||
await this.nextFrame | ||
this.assert.deepEqual(this.controller.loggedMissingStringValues, ["", "hello", ""]) | ||
this.assert.deepEqual(this.controller.oldLoggedMissingStringValues, [undefined, "", "hello"]) | ||
} | ||
|
||
has(name: string) { | ||
return this.element.hasAttribute(this.attr(name)) | ||
} | ||
|
||
get(name: string) { | ||
return this.element.getAttribute(this.attr(name)) | ||
} | ||
|
||
set(name: string, value: string) { | ||
return this.element.setAttribute(this.attr(name), value) | ||
} | ||
|
||
attr(name: string) { | ||
return `data-${this.identifier}-${name}` | ||
} | ||
|
||
get element() { | ||
return this.controller.element | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm not sure if this is the right fix for the issue raised in #680. The attribute methods on
Element
seem to be case-insensitive, which leads me to believe that we need to week this somewhere else.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.
If I get what you mean, they are case insensitive, meaning that you can read or write them with whatever casing and it would still work, but the mutation observer always reports the changed properties as lowercase strings. The consequence is that when we check if we should care about the property change, we always opt to discard the change event, because we instead stored the property with whatever casing was found in the document. I've not found this documented anywhere, but I tried both Chromium and Firefox and both did the same.
If I didn't get what you mean, could you point me in the direction where you think the fix should be made instead?
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.
Hey @RBastianini, I just realized that my comment wasn't very well thought out, sorry about that! Let me dig into this and come back with a more constructive answer. Thank you!