Commit f7e9cede authored by Robert Knight's avatar Robert Knight

Improve TextQuoteAnchor tests

For consistency, and because it is useful/straightforward to do, all of the
`TextQuoteAnchor` tests now mock `matchQuote` but not `TextRange`,
except for one integration test that is labeled as such.
parent 935594b9
...@@ -250,7 +250,7 @@ describe('annotator/anchoring/types', () => { ...@@ -250,7 +250,7 @@ describe('annotator/anchoring/types', () => {
} }
describe('#fromRange', () => { describe('#fromRange', () => {
it('returns a TextQuoteAnchor instance', () => { it('returns expected TextQuoteAnchor', () => {
const quote = 'our fathers'; const quote = 'our fathers';
const text = container.textContent; const text = container.textContent;
const pos = text.indexOf(quote); const pos = text.indexOf(quote);
...@@ -272,14 +272,22 @@ describe('annotator/anchoring/types', () => { ...@@ -272,14 +272,22 @@ describe('annotator/anchoring/types', () => {
}); });
describe('#fromSelector', () => { describe('#fromSelector', () => {
it('returns a TextQuoteAnchor instance', () => { it('returns expected TextQuoteAnchor', () => {
const anchor = TextQuoteAnchor.fromSelector(container, { const selector = {
type: 'TextQuoteSelector', type: 'TextQuoteSelector',
exact: 'Liberty', exact: 'Liberty',
prefix: 'a new nation, conceived in ', prefix: 'a new nation, conceived in ',
suffix: ', and dedicated to the proposition that', suffix: ', and dedicated to the proposition that',
}); };
const anchor = TextQuoteAnchor.fromSelector(container, selector);
assert.instanceOf(anchor, TextQuoteAnchor); assert.instanceOf(anchor, TextQuoteAnchor);
assert.equal(anchor.exact, selector.exact);
assert.deepEqual(anchor.context, {
prefix: selector.prefix,
suffix: selector.suffix,
});
}); });
}); });
...@@ -296,14 +304,38 @@ describe('annotator/anchoring/types', () => { ...@@ -296,14 +304,38 @@ describe('annotator/anchoring/types', () => {
}); });
describe('#toRange', () => { describe('#toRange', () => {
it('returns a valid DOM Range', () => { it('calls `matchQuote` with expected arguments', () => {
$imports.$restore({ fakeMatchQuote.returns({
'./match-quote': true, start: 10,
end: 20,
score: 1.0,
});
const quoteAnchor = new TextQuoteAnchor(container, 'Liberty', {
prefix: 'expected-prefix',
suffix: 'expected-suffix',
});
quoteAnchor.toRange({ hint: 42 });
assert.calledWith(fakeMatchQuote, container.textContent, 'Liberty', {
hint: 42,
prefix: 'expected-prefix',
suffix: 'expected-suffix',
});
});
it('returns `Range` representing match found by `matchQuote`', () => {
fakeMatchQuote.returns({
start: 10,
end: 20,
score: 1.0,
}); });
const quoteAnchor = new TextQuoteAnchor(container, 'Liberty'); const quoteAnchor = new TextQuoteAnchor(container, 'Liberty');
const range = quoteAnchor.toRange(); const range = quoteAnchor.toRange();
assert.instanceOf(range, Range); assert.instanceOf(range, Range);
assert.equal(range.toString(), 'Liberty'); assert.equal(range.toString(), container.textContent.slice(10, 20));
}); });
it('throws if the quote is not found', () => { it('throws if the quote is not found', () => {
...@@ -314,31 +346,33 @@ describe('annotator/anchoring/types', () => { ...@@ -314,31 +346,33 @@ describe('annotator/anchoring/types', () => {
); );
assert.throws(() => { assert.throws(() => {
quoteAnchor.toRange(); quoteAnchor.toRange();
}); }, 'Quote not found');
}); });
}); });
describe('#toPositionAnchor', () => { describe('#toPositionAnchor', () => {
it('returns a TextPositionAnchor instance', () => { it('returns expected TextPositionAnchor', () => {
$imports.$restore({ fakeMatchQuote.returns({
'./match-quote': true, start: 10,
end: 100,
score: 1.0,
}); });
const quoteAnchor = new TextQuoteAnchor(container, 'Liberty'); const quoteAnchor = new TextQuoteAnchor(container, 'Liberty');
const pos = quoteAnchor.toPositionAnchor(); const pos = quoteAnchor.toPositionAnchor();
assert.instanceOf(pos, TextPositionAnchor);
assert.deepEqual(pos, new TextPositionAnchor(container, 10, 100));
}); });
it('throws if the quote is not found', () => { it('throws if the quote is not found', () => {
$imports.$restore({ fakeMatchQuote.returns(null);
'./match-quote': true,
});
const quoteAnchor = new TextQuoteAnchor( const quoteAnchor = new TextQuoteAnchor(
container, container,
'some are more equal than others' 'five score and nine years ago'
); );
assert.throws(() => { assert.throws(() => {
quoteAnchor.toPositionAnchor(); quoteAnchor.toPositionAnchor();
}); }, 'Quote not found');
}); });
}); });
...@@ -353,6 +387,7 @@ describe('annotator/anchoring/types', () => { ...@@ -353,6 +387,7 @@ describe('annotator/anchoring/types', () => {
const range = document.createRange(); const range = document.createRange();
range.setStart(container.firstChild, 0); range.setStart(container.firstChild, 0);
range.setEnd(container.firstChild, 4); range.setEnd(container.firstChild, 4);
const anchor = TextQuoteAnchor.fromRange(container, range); const anchor = TextQuoteAnchor.fromRange(container, range);
assert.deepEqual(anchor.toSelector(), { assert.deepEqual(anchor.toSelector(), {
type: 'TextQuoteSelector', type: 'TextQuoteSelector',
...@@ -360,6 +395,7 @@ describe('annotator/anchoring/types', () => { ...@@ -360,6 +395,7 @@ describe('annotator/anchoring/types', () => {
suffix: ' score and seven years ago our f', suffix: ' score and seven years ago our f',
exact: 'Four', exact: 'Four',
}); });
const newRange = anchor.toRange(); const newRange = anchor.toRange();
assert.equal(newRange.toString(), 'Four'); assert.equal(newRange.toString(), 'Four');
}); });
......
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