Commit fa2f4135 authored by Robert Knight's avatar Robert Knight

Add tests for population of `target` field on new annotations

Add missing tests for this critical property of new annotations. In the
process revise some variable names in `createAnnotation` that were
misleading.
parent 91f96795
...@@ -546,15 +546,16 @@ export default class Guest extends Delegator { ...@@ -546,15 +546,16 @@ export default class Guest extends Delegator {
annotation.document = info.metadata; annotation.document = info.metadata;
annotation.uri = info.uri; annotation.uri = info.uri;
// `selectors` is an array of arrays: each item is an array of selectors
// identifying a distinct target.
const root = this.element; const root = this.element;
const selectors = await Promise.all( const rangeSelectors = await Promise.all(
ranges.map(range => this.anchoring.describe(root, range)) ranges.map(range => this.anchoring.describe(root, range))
); );
annotation.target = selectors.map(selector => ({ annotation.target = rangeSelectors.map(selectors => ({
source: info.uri, source: info.uri,
selector,
// In the Hypothesis API the field containing the selectors is called
// `selector`, despite being a list.
selector: selectors,
})); }));
this.publish('beforeAnnotationCreated', [annotation]); this.publish('beforeAnnotationCreated', [annotation]);
......
...@@ -63,6 +63,7 @@ describe('Guest', () => { ...@@ -63,6 +63,7 @@ describe('Guest', () => {
}; };
htmlAnchoring = { htmlAnchoring = {
anchor: sinon.stub(), anchor: sinon.stub(),
describe: sinon.stub(),
}; };
rangeUtil = { rangeUtil = {
itemsForRange: sinon.stub().returns([]), itemsForRange: sinon.stub().returns([]),
...@@ -626,7 +627,7 @@ describe('Guest', () => { ...@@ -626,7 +627,7 @@ describe('Guest', () => {
}); });
describe('#createAnnotation', () => { describe('#createAnnotation', () => {
it('adds metadata to the annotation object', async () => { it('adds document metadata to the annotation', async () => {
const guest = createGuest(); const guest = createGuest();
const annotation = {}; const annotation = {};
...@@ -636,6 +637,27 @@ describe('Guest', () => { ...@@ -636,6 +637,27 @@ describe('Guest', () => {
assert.deepEqual(annotation.document, fakeDocumentMeta.metadata); assert.deepEqual(annotation.document, fakeDocumentMeta.metadata);
}); });
it('adds selectors for selected ranges to the annotation', async () => {
const guest = createGuest();
const fakeRange = {};
guest.selectedRanges = [fakeRange];
const selectorA = { type: 'TextPositionSelector', start: 0, end: 10 };
const selectorB = { type: 'TextQuoteSelector', exact: 'foobar' };
htmlAnchoring.anchor.resolves({});
htmlAnchoring.describe.returns([selectorA, selectorB]);
const annotation = await guest.createAnnotation({});
assert.calledWith(htmlAnchoring.describe, guest.element, fakeRange);
assert.deepEqual(annotation.target, [
{
source: fakeDocumentMeta.uri(),
selector: [selectorA, selectorB],
},
]);
});
it('merges properties of input object into returned annotation', async () => { it('merges properties of input object into returned annotation', async () => {
const guest = createGuest(); const guest = createGuest();
let annotation = { foo: 'bar' }; let annotation = { foo: 'bar' };
......
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