Skip to content

Commit

Permalink
Chore: unit tests for Thumbnail Sidebar clicks (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan authored and Conrad Chan committed Feb 1, 2019
1 parent 5b65ecf commit 597600b
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/lib/ThumbnailsSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,11 @@ class ThumbnailsSidebar {
*/
setCurrentPage(pageNumber) {
const parsedPageNumber = parseInt(pageNumber, 10);
if (!parsedPageNumber || parsedPageNumber < 1 || parsedPageNumber > this.pdfViewer.pagesCount) {
return;
}

this.currentPage = parsedPageNumber;

this.applyCurrentPageSelection();
if (parsedPageNumber >= 1 && parsedPageNumber <= this.pdfViewer.pagesCount) {
this.currentPage = parsedPageNumber;
this.applyCurrentPageSelection();
}
}

/**
Expand Down
113 changes: 113 additions & 0 deletions src/lib/__tests__/ThumbnailsSidebar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,117 @@ describe('ThumbnailsSidebar', () => {
});
});
});

describe('thumbnailClickHandler()', () => {
let targetEl;
let evt;

beforeEach(() => {
stubs.onClickHandler = sandbox.stub();
stubs.preventDefault = sandbox.stub();
stubs.stopImmediatePropagation = sandbox.stub();

targetEl = document.createElement('div');
targetEl.classList.add('bp-thumbnail');
targetEl.dataset.bpPageNum = '3';

evt = {
target: targetEl,
preventDefault: stubs.preventDefault,
stopImmediatePropagation: stubs.stopImmediatePropagation
};

thumbnailsSidebar.onClickHandler = stubs.onClickHandler;
});

it('should call the onClickHandler if target is a thumbnail element', () => {
thumbnailsSidebar.thumbnailClickHandler(evt);

expect(stubs.onClickHandler).to.be.calledWith(3);
expect(stubs.preventDefault).to.be.called;
expect(stubs.stopImmediatePropagation).to.be.called;
});

it('should not call the onClickHandler if target is not thumbnail element', () => {
targetEl.classList.remove('bp-thumbnail');
thumbnailsSidebar.thumbnailClickHandler(evt);

expect(stubs.onClickHandler).not.to.be.called;
expect(stubs.preventDefault).to.be.called;
expect(stubs.stopImmediatePropagation).to.be.called;
});
});

describe('setCurrentPage()', () => {
beforeEach(() => {
stubs.applyCurrentPageSelection = sandbox.stub(thumbnailsSidebar, 'applyCurrentPageSelection');
thumbnailsSidebar.pdfViewer = { pagesCount: 10 };
});

const paramaterizedTests = [
{ name: 'pageNumber is undefined', pageNumber: undefined },
{ name: 'pageNumber is less than 1', pageNumber: 0 },
{ name: 'pageNumber is greater than last page', pageNumber: 11 }
];

paramaterizedTests.forEach(({ name, pageNumber }) => {
it(`should do nothing if ${name}`, () => {
thumbnailsSidebar.setCurrentPage(pageNumber);

expect(thumbnailsSidebar.currentPage).to.be.undefined;
expect(stubs.applyCurrentPageSelection).not.to.be.called;
});
});

it('should set the currentPage and apply current page selection', () => {
thumbnailsSidebar.setCurrentPage(3);

expect(thumbnailsSidebar.currentPage).to.be.equal(3);
expect(stubs.applyCurrentPageSelection).to.be.called;
});
});

describe('applyCurrentPageSelection()', () => {
let thumbnails;

beforeEach(() => {
stubs.addClass = sandbox.stub();
stubs.removeClass = sandbox.stub();

// eslint-disable-next-line
const createTestThumbnail = (pageNum) => {
const thumbnail = document.createElement('div');
thumbnail.dataset.bpPageNum = pageNum;
thumbnail.classList.add = stubs.addClass;
thumbnail.classList.remove = stubs.removeClass;
return thumbnail;
};

const thumbnail1 = createTestThumbnail('1');
const thumbnail2 = createTestThumbnail('2');
const thumbnail3 = createTestThumbnail('3');

thumbnails = [thumbnail1, thumbnail2, thumbnail3];

thumbnailsSidebar.currentThumbnails = thumbnails;
});

it('should remove the is selected class from all thumbnails', () => {
thumbnailsSidebar.currentPage = 10;

thumbnailsSidebar.applyCurrentPageSelection();

expect(stubs.removeClass).to.be.calledThrice;
expect(stubs.addClass).not.to.be.called;
});

it('should remove the is selected class from all thumbnails', () => {
thumbnailsSidebar.currentPage = 2;

thumbnailsSidebar.applyCurrentPageSelection();

expect(stubs.removeClass).to.be.calledTwice;
expect(stubs.addClass).to.be.calledOnce;
});
});
});

0 comments on commit 597600b

Please sign in to comment.