Simplify reading query and annotations

There was some code complexity here aimed at ensuring that _either_
config.annotations or config.query would be set but never both. It isn't
really necessary for the code to maintain this condition, especially
given that the way that both settings are read from the URL fragment
means that both cannot be present at once anyway.
parent 2a99b84b
...@@ -25,15 +25,8 @@ function configFrom(window_) { ...@@ -25,15 +25,8 @@ function configFrom(window_) {
// //
// In environments where the config has not been injected into the DOM, // In environments where the config has not been injected into the DOM,
// we try to retrieve it from the URL here. // we try to retrieve it from the URL here.
var query = settings.query(window_.location.href); config.query = settings.query(window_.location.href);
if (query) { config.annotations = settings.annotations(window_.location.href);
config.query = query;
} else {
var annotations = settings.annotations(window_.location.href);
if (annotations) {
config.annotations = annotations;
}
}
// If the client is injected by the browser extension, ignore // If the client is injected by the browser extension, ignore
// the rest of the host page config. // the rest of the host page config.
......
...@@ -30,8 +30,8 @@ describe('annotator.config', function() { ...@@ -30,8 +30,8 @@ describe('annotator.config', function() {
beforeEach('reset fakeSettings', function() { beforeEach('reset fakeSettings', function() {
fakeSettings.app = sinon.stub().returns('IFRAME_URL'); fakeSettings.app = sinon.stub().returns('IFRAME_URL');
fakeSettings.annotations = sinon.stub(); fakeSettings.annotations = sinon.stub().returns(null);
fakeSettings.query = sinon.stub(); fakeSettings.query = sinon.stub().returns(null);
fakeSettings.configFuncSettingsFrom = sinon.stub().returns({}); fakeSettings.configFuncSettingsFrom = sinon.stub().returns({});
}); });
...@@ -178,8 +178,7 @@ describe('annotator.config', function() { ...@@ -178,8 +178,7 @@ describe('annotator.config', function() {
configFrom(fakeWindow()); configFrom(fakeWindow());
assert.calledOnce(fakeSettings.annotations); assert.calledOnce(fakeSettings.annotations);
assert.calledWithExactly( assert.calledWithExactly(fakeSettings.annotations, 'LOCATION_HREF');
fakeSettings.annotations, 'LOCATION_HREF');
}); });
context("when there's a direct-linked annotation ID", function() { context("when there's a direct-linked annotation ID", function() {
...@@ -193,24 +192,31 @@ describe('annotator.config', function() { ...@@ -193,24 +192,31 @@ describe('annotator.config', function() {
}); });
context("when there's no direct-linked annotation ID", function() { context("when there's no direct-linked annotation ID", function() {
it("doesn't add any .annotations setting to the config", function() { it('sets config.annotations to null', function() {
assert.isFalse(configFrom(fakeWindow()).hasOwnProperty('annotations')); assert.isNull(configFrom(fakeWindow()).annotations);
}); });
});
context("when there's no annotations query", function() { it("extracts the query from the parent page's URL", function() {
it("doesn't add any .query setting to the config", function() { configFrom(fakeWindow());
assert.isFalse(configFrom(fakeWindow()).hasOwnProperty('query'));
}); assert.calledOnce(fakeSettings.query);
assert.calledWithExactly(fakeSettings.query, 'LOCATION_HREF');
});
context("when there's no annotations query", function() {
it('sets config.query to null', function() {
assert.isNull(configFrom(fakeWindow()).query);
}); });
});
context("when there's an annotations query", function() { context("when there's an annotations query", function() {
beforeEach(function() { beforeEach(function() {
fakeSettings.query.returns('QUERY'); fakeSettings.query.returns('QUERY');
}); });
it('adds the query to the config', function() { it('adds the query to the config', function() {
assert.equal(configFrom(fakeWindow()).query, 'QUERY'); assert.equal(configFrom(fakeWindow()).query, 'QUERY');
});
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment