Commit d2b3e198 authored by Eduardo's avatar Eduardo

Apply suggestions from code review

Co-authored-by: 's avatarRobert Knight <robertknight@gmail.com>
parent 0c124b54
...@@ -14,7 +14,7 @@ import { getBoundingClientRect } from '../highlighter'; ...@@ -14,7 +14,7 @@ import { getBoundingClientRect } from '../highlighter';
/** /**
* @typedef BucketSet * @typedef BucketSet
* @prop {Bucket} above - A single bucket containing all the annotation * @prop {Bucket} above - A single bucket containing all the annotation
* tags which anchors are offscreen upwards * tags whose anchors are offscreen upwards
* @prop {Bucket} below - A single bucket containing all the annotation * @prop {Bucket} below - A single bucket containing all the annotation
* tags which anchors are offscreen downwards * tags which anchors are offscreen downwards
* @prop {Bucket[]} buckets - On-screen buckets * @prop {Bucket[]} buckets - On-screen buckets
......
...@@ -63,7 +63,7 @@ describe('annotator/util/buckets', () => { ...@@ -63,7 +63,7 @@ describe('annotator/util/buckets', () => {
}); });
/** @param {number} index */ /** @param {number} index */
const getTag = index => { const tagForAnchor = index => {
return fakeAnchors[index].annotation.$tag; return fakeAnchors[index].annotation.$tag;
}; };
...@@ -125,32 +125,44 @@ describe('annotator/util/buckets', () => { ...@@ -125,32 +125,44 @@ describe('annotator/util/buckets', () => {
describe('anchorBuckets', () => { describe('anchorBuckets', () => {
it('puts anchors that are above the screen into the `above` bucket', () => { it('puts anchors that are above the screen into the `above` bucket', () => {
const bucketSet = anchorBuckets(fakeAnchors); const bucketSet = anchorBuckets(fakeAnchors);
assert.deepEqual([...bucketSet.above.tags], [getTag(0), getTag(1)]); assert.deepEqual(
[...bucketSet.above.tags],
[tagForAnchor(0), tagForAnchor(1)]
);
}); });
it('puts anchors that are below the screen into the `below` bucket', () => { it('puts anchors that are below the screen into the `below` bucket', () => {
const bucketSet = anchorBuckets(fakeAnchors); const bucketSet = anchorBuckets(fakeAnchors);
assert.deepEqual([...bucketSet.below.tags], [getTag(4), getTag(5)]); assert.deepEqual(
[...bucketSet.below.tags],
[tagForAnchor(4), tagForAnchor(5)]
);
}); });
it('puts on-screen anchors into a buckets', () => { it('puts on-screen anchors into a buckets', () => {
const bucketSet = anchorBuckets(fakeAnchors); const bucketSet = anchorBuckets(fakeAnchors);
assert.deepEqual([...bucketSet.buckets[0].tags], [getTag(2), getTag(3)]); assert.deepEqual(
[...bucketSet.buckets[0].tags],
[tagForAnchor(2), tagForAnchor(3)]
);
}); });
it('puts anchors into separate buckets if more than 60px separates their boxes', () => { it('puts anchors into separate buckets if more than 60px separates their boxes', () => {
fakeAnchors[2].highlights = [201, 15]; // bottom 216 fakeAnchors[2].highlights = [201, 15]; // bottom 216
fakeAnchors[3].highlights = [301, 15]; // top 301 - more than 60px from 216 fakeAnchors[3].highlights = [301, 15]; // top 301 - more than 60px from 216
const bucketSet = anchorBuckets(fakeAnchors); const bucketSet = anchorBuckets(fakeAnchors);
assert.deepEqual([...bucketSet.buckets[0].tags], [getTag(2)]); assert.deepEqual([...bucketSet.buckets[0].tags], [tagForAnchor(2)]);
assert.deepEqual([...bucketSet.buckets[1].tags], [getTag(3)]); assert.deepEqual([...bucketSet.buckets[1].tags], [tagForAnchor(3)]);
}); });
it('puts overlapping anchors into a shared bucket', () => { it('puts overlapping anchors into a shared bucket', () => {
fakeAnchors[2].highlights = [201, 200]; // Bottom 401 fakeAnchors[2].highlights = [201, 200]; // Bottom 401
fakeAnchors[3].highlights = [285, 100]; // Bottom 385 fakeAnchors[3].highlights = [285, 100]; // Bottom 385
const bucketSet = anchorBuckets(fakeAnchors); const bucketSet = anchorBuckets(fakeAnchors);
assert.deepEqual([...bucketSet.buckets[0].tags], [getTag(2), getTag(3)]); assert.deepEqual(
[...bucketSet.buckets[0].tags],
[tagForAnchor(2), tagForAnchor(3)]
);
}); });
it('positions the bucket at vertical midpoint of the box containing all bucket anchors', () => { it('positions the bucket at vertical midpoint of the box containing all bucket anchors', () => {
...@@ -185,9 +197,18 @@ describe('annotator/util/buckets', () => { ...@@ -185,9 +197,18 @@ describe('annotator/util/buckets', () => {
fakeAnchors[0], fakeAnchors[0],
fakeAnchors[1], fakeAnchors[1],
]); ]);
assert.deepEqual([...bucketSet.above.tags], [getTag(0), getTag(1)]); assert.deepEqual(
assert.deepEqual([...bucketSet.buckets[0].tags], [getTag(2), getTag(3)]); [...bucketSet.above.tags],
assert.deepEqual([...bucketSet.below.tags], [getTag(4), getTag(5)]); [tagForAnchor(0), tagForAnchor(1)]
);
assert.deepEqual(
[...bucketSet.buckets[0].tags],
[tagForAnchor(2), tagForAnchor(3)]
);
assert.deepEqual(
[...bucketSet.below.tags],
[tagForAnchor(4), tagForAnchor(5)]
);
}); });
it('returns only above- and below-screen anchors if none are on-screen', () => { it('returns only above- and below-screen anchors if none are on-screen', () => {
...@@ -199,11 +220,14 @@ describe('annotator/util/buckets', () => { ...@@ -199,11 +220,14 @@ describe('annotator/util/buckets', () => {
const bucketSet = anchorBuckets(fakeAnchors); const bucketSet = anchorBuckets(fakeAnchors);
assert.equal(bucketSet.buckets.length, 0); assert.equal(bucketSet.buckets.length, 0);
// Above-screen // Above-screen
assert.deepEqual([...bucketSet.above.tags], [getTag(0), getTag(1)]); assert.deepEqual(
[...bucketSet.above.tags],
[tagForAnchor(0), tagForAnchor(1)]
);
// Below-screen // Below-screen
assert.deepEqual( assert.deepEqual(
[...bucketSet.below.tags], [...bucketSet.below.tags],
[getTag(2), getTag(3), getTag(4), getTag(5)] [tagForAnchor(2), tagForAnchor(3), tagForAnchor(4), tagForAnchor(5)]
); );
}); });
}); });
......
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