-
Notifications
You must be signed in to change notification settings - Fork 204
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
Tests and other tweaks to shared/settings.js #406
Conversation
Codecov Report
@@ Coverage Diff @@
## master #406 +/- ##
==========================================
+ Coverage 89.63% 89.65% +0.02%
==========================================
Files 123 123
Lines 4910 4910
Branches 846 846
==========================================
+ Hits 4401 4402 +1
+ Misses 509 508 -1
Continue to review full report at Codecov.
|
src/annotator/config.js
Outdated
@@ -18,7 +18,7 @@ function config(window_) { | |||
|
|||
// Parse config from `<script class="js-hypothesis-config">` tags | |||
try { | |||
Object.assign(options, settings(window_.document)); | |||
Object.assign(options, settings.jsonConfigsFrom(window_.document)); |
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 think this is much clearer, jsonConfigsFrom(document)
tells me that this is reading the JSON objs from the document (as opposed to the variety of other options/settings that the client reads from documents), without me having to go look in settings.js
@@ -12,17 +12,22 @@ function assign(dest, src) { | |||
} | |||
|
|||
/** | |||
* Return application configuration information from the host page. |
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.
This was actually wrong - the function isn't always used to read settings from the host page
* | ||
* Exposes shared application settings, read from script tags with the |
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.
This is also misleading. It's a shared utility function, yes. But "shared application settings" is misleading. Not every setting read from a js-hypothesis-config is actually shared between the three legs of the app (annotator, boot and sidebar), nor is every setting that is shared read from this function.
* If there are multiple `js-hypothesis-config` tags in the document then merge | ||
* them into a single returned object (when multiple scripts contain the same | ||
* setting names, scripts further down in the document override those further | ||
* up). |
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've left the exception that this function throws (on invalid JSON) still undocumented, as I intend to send a PR to stop iut from throwing the exception anyway
appendJSHypothesisConfig(document, '["a", "b", "c"]'); | ||
}); | ||
|
||
it('returns the array, parsed into an object', function() { |
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 should fix this in a separate pr, it should ignore top-level arrays (but log a warning)
appendJSHypothesisConfig(document, '"hi"'); | ||
}); | ||
|
||
it('returns the string, parsed into an object', function() { |
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 should fix this in a separate pr, it should ignore top-level strings (but log a warning)
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.
Heh. LOLJS :)
1981f17
to
9390463
Compare
appendJSHypothesisConfig(document, 'this is not valid json'); | ||
}); | ||
|
||
it('throws a SyntaxError', function() { |
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 should fix this in a follow up pr: it should log a warning, and ignore the invalid json, not throw
Unhandled errors in the sidebar code get reported to us via Raven, so if you catch the error in the context of the sidebar and log an error, that could actually have the effect of hiding the problem from us. |
src/shared/settings.js
Outdated
* setting names, scripts further down in the document override those further | ||
* up). | ||
* | ||
* @param {Document|Element} document - The root element to search for. |
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.
The end of the sentence ("... search for script tags") is missing here.
src/shared/test/settings-test.js
Outdated
afterEach('remove js-hypothesis-config tags', function() { | ||
var elements = document.querySelectorAll('.js-settings-test'); | ||
for (var i=0; i < elements.length; i++) { | ||
elements[i].parentNode.removeChild(elements[i]); |
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.
You can shorten this to elements[i].remove()
. Some older code uses removeChild()
because Element#remove
was not supported in older versions of PhantomJS.
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.
LGTM. Renaming and updated descriptions look sensible to me. I suggested a docstring fix and a slightly simpler way to remove elements from the DOM.
Add a new set of unit tests that test more cases.
9390463
to
5a33ce1
Compare
Done both suggestions |
Another little thing that I've extracted out of config code refactoring work:
Rewrite the
shared/settings.js
unit tests with more detailed onesHave
shared/settings.js
export an object with ajsonConfigsFrom(document)
function rather than just exporting a top-level function.The way I'm intending for
shared/settings.js
to be used is as a container for helper / utility functions that are called by both theannotator
and thesidebar
config code, and I expect this to contain more than just one function in the future, so I'm making space for it.Also "settings" in the client can be read from lots of places: the URL, js-hypothesis-config tags in the host page, or in the app.html, window.hypothesisConfig() in the host page, the annotator+html link... This
jsonConfigsFrom(document)
is really just a utility function for parsing JSON tags out of (any) document, it doesn't return "the settings" as a whole, so I think it needs a more specific name thansettings()
Also rewrote the function's docstring
I'd like to do more work to fix a couple of issues that the tests found:
Stop this function from throwing JSON parsing errors. In every case where we use the function I think the desired behaviour would be to catch and log the error (in practice in some cases we do that and in some cases we don't catch it and crash), so the function should do the catch and log itself
Stop this function from returning nonsense when a json tag doesn't contain a top-level object, as a few of the tests show
But I've run out of time for this week so I'll submit a separate pr for those changes later