-
Notifications
You must be signed in to change notification settings - Fork 1
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
Unobserve attributes #3
Open
semmel
wants to merge
4
commits into
HenriqueLimas:master
Choose a base branch
from
semmel:unobserve-attributes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export default function onAttributeChanged(element, callback) { | ||
element.__handleAttributeChangedCallback__.push(callback); | ||
element.__handleAttributeChangedCallback__.add(callback); | ||
return () => element.__handleAttributeChangedCallback__.delete(callback); | ||
} |
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 |
---|---|---|
|
@@ -17,7 +17,11 @@ | |
"release": "npm run build && npm run cp:package && npm run test:all && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish dist" | ||
}, | ||
"author": "Henrique Limas <[email protected]>", | ||
"keywords": ["osagai", "webcomponents", "functional-programming"], | ||
"keywords": [ | ||
"osagai", | ||
"webcomponents", | ||
"functional-programming" | ||
], | ||
"license": "ISC", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
|
@@ -27,11 +31,11 @@ | |
"electric-cli": "^3.0.4", | ||
"jasmine-core": "^3.3.0", | ||
"jest": "^23.6.0", | ||
"karma": "^4.0.0", | ||
"karma": "^4.4.1", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-jasmine": "^2.0.1", | ||
"karma-rollup-preprocessor": "^6.1.2", | ||
"prettier": "1.15.3", | ||
"karma-rollup-preprocessor": "^7.0.2", | ||
"prettier": "^1.15.3", | ||
"regenerator-runtime": "^0.13.1", | ||
"rollup": "^0.67.3", | ||
"rollup-plugin-commonjs": "^9.2.0", | ||
|
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,69 @@ | ||
import define from "../../lib/define"; | ||
import { onAttributeChanged } from "../../lib/lifecycles/index.js"; | ||
|
||
describe("onAttributeChange", function() { | ||
beforeEach(() => { | ||
const | ||
Component = () => () => `<span>dummy text content</span>`; | ||
|
||
Component.observedAttributes = ['foo']; | ||
define("attribute-test-element", Component); | ||
|
||
const | ||
element = document.createElement("attribute-test-element"); | ||
element.setAttribute("foo", "init"); | ||
document.body.appendChild(element); | ||
}); | ||
|
||
afterEach(() => { | ||
document.querySelector('attribute-test-element').remove(); | ||
}); | ||
|
||
it("should call the callback with the assigned attribute value", (done) => { | ||
const | ||
element = document.querySelector('attribute-test-element'); | ||
|
||
onAttributeChanged(element, ({name, current, old}) => { | ||
if (name !== "foo") { | ||
done.fail(new Error(`"${name}" != "foo" wrong attribute name`)); | ||
} | ||
else if (current !== "bar") { | ||
done.fail(new Error(`"${current}" != "bar" wrong current value`)); | ||
} | ||
else if (old !== "init") { | ||
done.fail(new Error(`"${old}" != "init" wrong previous value`)); | ||
} | ||
else { | ||
done(); | ||
} | ||
}); | ||
|
||
element.setAttribute("foo", "bar"); | ||
}); | ||
|
||
it("should not call the callback after it is unsubscribed", (done) => { | ||
const | ||
element = document.querySelector('attribute-test-element'); | ||
|
||
const | ||
unsubscribe = | ||
onAttributeChanged(element, ({current}) => { | ||
if (current !== "first") { | ||
done.fail( | ||
new Error(`"${current}" !== "first", unexpected attribute value`) | ||
); | ||
} | ||
else { | ||
unsubscribe(); | ||
element.setAttribute("foo", "second"); | ||
setTimeout(done, 20); | ||
} | ||
}); | ||
|
||
element.setAttribute("foo", "first"); | ||
|
||
if (typeof unsubscribe !== "function") { | ||
done.fail(new Error(`${typeof unsubscribe} != "function", return value is not an unsubscriber`)); | ||
} | ||
}); | ||
}); |
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.
We could use a mock function and check if that function was called or not called in the other test. In that way the test will be simpler and we don't need to throw errors
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.
Sorry, I don't get it 🤔. Could you describe it more?