@@ -4,7 +4,9 @@ import BookReader from "@/src/BookReader.js";
44import "@/src/plugins/plugin.chapters.js" ;
55import { BookModel } from "@/src/BookReader/BookModel" ;
66import { deepCopy } from "../utils" ;
7+ /** @typedef {import('@/src/plugins/plugin.chapters').TocEntry } TocEntry */
78
9+ /** @type {TocEntry[] } */
810const SAMPLE_TOC = [ {
911 "pagenum" : "3" ,
1012 "level" : 1 ,
@@ -34,23 +36,34 @@ const SAMPLE_TOC = [{
3436 "pageIndex" : 40 ,
3537} ] ;
3638
39+ /** @type {TocEntry[] } */
3740const 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+
5467afterEach ( ( ) => {
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