Commit 72208f96 authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Avoid leaving elements from bucket-bar test

These changes enforce that the bucket-bar is destroyed and allows
multiple creations of the bucket bar, if needed.
parent cebe7dbd
...@@ -5,15 +5,18 @@ describe('BucketBar', () => { ...@@ -5,15 +5,18 @@ describe('BucketBar', () => {
const sandbox = sinon.createSandbox(); const sandbox = sinon.createSandbox();
let fakeAnnotator; let fakeAnnotator;
let fakeBucketUtil; let fakeBucketUtil;
let bucketBar; let bucketBars;
let bucketProps; let bucketProps;
const createBucketBar = function (options) { const createBucketBar = function (options) {
const element = document.createElement('div'); const element = document.createElement('div');
return new BucketBar(element, fakeAnnotator, options); const bucketBar = new BucketBar(element, fakeAnnotator, options);
bucketBars.push(bucketBar);
return bucketBar;
}; };
beforeEach(() => { beforeEach(() => {
bucketBars = [];
bucketProps = {}; bucketProps = {};
fakeAnnotator = { fakeAnnotator = {
anchors: [], anchors: [],
...@@ -38,20 +41,20 @@ describe('BucketBar', () => { ...@@ -38,20 +41,20 @@ describe('BucketBar', () => {
}); });
afterEach(() => { afterEach(() => {
bucketBar?.destroy(); bucketBars.forEach(bucketBar => bucketBar.destroy());
$imports.$restore(); $imports.$restore();
sandbox.restore(); sandbox.restore();
}); });
it('should render buckets for existing anchors when constructed', () => { it('should render buckets for existing anchors when constructed', () => {
bucketBar = createBucketBar(); const bucketBar = createBucketBar();
assert.calledWith(fakeBucketUtil.anchorBuckets, fakeAnnotator.anchors); assert.calledWith(fakeBucketUtil.anchorBuckets, fakeAnnotator.anchors);
assert.ok(bucketBar.element.querySelector('.FakeBuckets')); assert.ok(bucketBar.element.querySelector('.FakeBuckets'));
}); });
describe('updating buckets', () => { describe('updating buckets', () => {
it('should update buckets when the window is resized', () => { it('should update buckets when the window is resized', () => {
bucketBar = createBucketBar(); createBucketBar();
fakeBucketUtil.anchorBuckets.resetHistory(); fakeBucketUtil.anchorBuckets.resetHistory();
window.dispatchEvent(new Event('resize')); window.dispatchEvent(new Event('resize'));
...@@ -60,7 +63,7 @@ describe('BucketBar', () => { ...@@ -60,7 +63,7 @@ describe('BucketBar', () => {
}); });
it('should update buckets when the window is scrolled', () => { it('should update buckets when the window is scrolled', () => {
bucketBar = createBucketBar(); createBucketBar();
fakeBucketUtil.anchorBuckets.resetHistory(); fakeBucketUtil.anchorBuckets.resetHistory();
window.dispatchEvent(new Event('scroll')); window.dispatchEvent(new Event('scroll'));
...@@ -69,10 +72,10 @@ describe('BucketBar', () => { ...@@ -69,10 +72,10 @@ describe('BucketBar', () => {
}); });
it('should select annotations when Buckets component invokes callback', () => { it('should select annotations when Buckets component invokes callback', () => {
const fakeAnnotations = ['hi', 'there']; const bucketBar = createBucketBar();
bucketBar = createBucketBar();
bucketBar._update(); bucketBar._update();
const fakeAnnotations = ['hi', 'there'];
bucketProps.onSelectAnnotations(fakeAnnotations, true); bucketProps.onSelectAnnotations(fakeAnnotations, true);
assert.calledWith(fakeAnnotator.selectAnnotations, fakeAnnotations, true); assert.calledWith(fakeAnnotator.selectAnnotations, fakeAnnotations, true);
}); });
...@@ -89,19 +92,11 @@ describe('BucketBar', () => { ...@@ -89,19 +92,11 @@ describe('BucketBar', () => {
document.body.appendChild(scrollableEls1); document.body.appendChild(scrollableEls1);
document.body.appendChild(scrollableEls2); document.body.appendChild(scrollableEls2);
}); });
afterEach(() => { afterEach(() => {
// Explicitly call `destroy` before removing scrollable elements
// from document to test the scrollable-remove-events path of
// the `destroy` method. Otherwise, this afterEach will execute
// before the test-global one that calls `destroy`, and there will
// be no scrollable elements left in the document.
bucketBar.destroy();
scrollableEls.forEach(el => el.remove()); scrollableEls.forEach(el => el.remove());
}); });
it('should update buckets when any scrollable scrolls', () => { it('should update buckets when any scrollable scrolls', () => {
bucketBar = createBucketBar({ createBucketBar({
scrollables: ['.scrollable-1', '.scrollable-2'], scrollables: ['.scrollable-1', '.scrollable-2'],
}); });
fakeBucketUtil.anchorBuckets.resetHistory(); fakeBucketUtil.anchorBuckets.resetHistory();
...@@ -113,6 +108,7 @@ describe('BucketBar', () => { ...@@ -113,6 +108,7 @@ describe('BucketBar', () => {
}); });
it('should not update if another update is pending', () => { it('should not update if another update is pending', () => {
const bucketBar = createBucketBar();
bucketBar._updatePending = true; bucketBar._updatePending = true;
bucketBar.update(); bucketBar.update();
assert.notCalled(window.requestAnimationFrame); assert.notCalled(window.requestAnimationFrame);
......
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