Skip to content

Commit e4d545c

Browse files
committed
Add tests for table_of_contents option
1 parent 75304c6 commit e4d545c

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

src/BookReader/options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ export const DEFAULT_OPTIONS = {
180180
*/
181181
data: [],
182182

183+
/** @type {import('../plugins/plugin.chapters.js').TocEntry[]} */
184+
table_of_contents: null,
185+
183186
/** Advanced methods for page rendering */
184187
/** @type {() => number} */
185188
getNumLeafs: null,

src/plugins/plugin.chapters.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import '@internetarchive/icon-toc/icon-toc';
1414
jQuery.extend(BookReader.defaultOptions, {
1515
olHost: 'https://openlibrary.org',
1616
enableChaptersPlugin: true,
17-
/** @type {TocEntry[]} */
18-
table_of_contents: null,
1917
bookId: '',
2018
});
2119

@@ -47,8 +45,8 @@ BookReader.prototype._chapterInit = async function() {
4745
this._tocEntries = rawTableOfContents
4846
.map(rawTOCEntry => (Object.assign({}, rawTOCEntry, {
4947
pageIndex: (
50-
rawTOCEntry.pagenum ? this.book.getPageIndex(rawTOCEntry.pagenum) :
51-
rawTOCEntry.leaf ? this.book.leafNumToIndex(rawTOCEntry.leaf) :
48+
typeof(rawTOCEntry.leaf) == 'number' ? this.book.leafNumToIndex(rawTOCEntry.leaf) :
49+
rawTOCEntry.pagenum ? this.book.getPageIndex(rawTOCEntry.pagenum) :
5250
undefined
5351
),
5452
})));

tests/jest/plugins/plugin.chapters.test.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import BookReader from "@/src/BookReader.js";
44
import "@/src/plugins/plugin.chapters.js";
55
import { BookModel } from "@/src/BookReader/BookModel";
66
import { deepCopy } from "../utils";
7+
/** @typedef {import('@/src/plugins/plugin.chapters').TocEntry} TocEntry */
78

9+
/** @type {TocEntry[]} */
810
const SAMPLE_TOC = [{
911
"pagenum": "3",
1012
"level": 1,
@@ -34,23 +36,34 @@ const SAMPLE_TOC = [{
3436
"pageIndex": 40,
3537
}];
3638

39+
/** @type {TocEntry[]} */
3740
const SAMPLE_TOC_UNDEF = [
3841
{
39-
"pagenum": "undefined",
4042
"level": 1,
4143
"label": "CHAPTER I",
4244
"type": { "key": "/type/toc_item" },
4345
"title": "THE COUNTRY AND THE MISSION 1",
4446
},
4547
{
46-
"pagenum": "undefined",
4748
"level": 1,
4849
"label": "CHAPTER II",
4950
"type": { "key": "/type/toc_item" },
5051
"title": "THE COUNTRY AND THE MISSION 2",
5152
},
5253
];
5354

55+
/** @type {TocEntry[]} */
56+
const SAMPLE_TOC_OPTION = [
57+
{
58+
"level": 1,
59+
"title": "THE COUNTRY AND THE MISSION 1",
60+
},
61+
{
62+
"level": 1,
63+
"title": "THE COUNTRY AND THE MISSION 2",
64+
},
65+
];
66+
5467
afterEach(() => {
5568
sinon.restore();
5669
});
@@ -92,14 +105,49 @@ describe("BRChaptersPlugin", () => {
92105
},
93106
getOpenLibraryRecord: async () => ({
94107
"title": "The Adventures of Sherlock Holmes",
95-
"table_of_contents": deepCopy(SAMPLE_TOC_UNDEF),
108+
"table_of_contents": deepCopy(SAMPLE_TOC_OPTION),
96109
"ocaid": "adventureofsherl0000unse",
97110
}),
98111
_chaptersRender: sinon.stub(),
99112
};
100113
await BookReader.prototype._chapterInit.call(fakeBR);
101114
expect(fakeBR._chaptersRender.callCount).toBe(1);
102115
});
116+
117+
test("does not fetch open library record if table of contents in options", async () => {
118+
const fakeBR = {
119+
options: {
120+
table_of_contents: deepCopy(SAMPLE_TOC_UNDEF),
121+
},
122+
bind: sinon.stub(),
123+
getOpenLibraryRecord: sinon.stub(),
124+
_chaptersRender: sinon.stub(),
125+
};
126+
await BookReader.prototype._chapterInit.call(fakeBR);
127+
expect(fakeBR.getOpenLibraryRecord.callCount).toBe(0);
128+
expect(fakeBR._chaptersRender.callCount).toBe(1);
129+
});
130+
131+
test("converts leafs and pagenums to page index", async () => {
132+
const table_of_contents = deepCopy(SAMPLE_TOC_UNDEF);
133+
table_of_contents[0].leaf = 0;
134+
table_of_contents[1].pagenum = '17';
135+
const fakeBR = {
136+
options: {
137+
table_of_contents,
138+
},
139+
bind: sinon.stub(),
140+
book: {
141+
leafNumToIndex: (leaf) => leaf + 1,
142+
getPageIndex: (str) => parseFloat(str),
143+
},
144+
_chaptersRender: sinon.stub(),
145+
};
146+
await BookReader.prototype._chapterInit.call(fakeBR);
147+
expect(fakeBR._chaptersRender.callCount).toBe(1);
148+
expect(fakeBR._tocEntries[0].pageIndex).toBe(1);
149+
expect(fakeBR._tocEntries[1].pageIndex).toBe(17);
150+
});
103151
});
104152

105153
describe('_chaptersRender', () => {

0 commit comments

Comments
 (0)