@@ -4,6 +4,7 @@ import { Paginator } from '../paginate/Paginator';
44import { paprize_isInitialized , paprize_isReady } from '../window' ;
55import type { SectionComponents } from './sectionComponents' ;
66import { globalStyleId } from '../constants' ;
7+ import { jsonDataReader } from './utils' ;
78
89vi . mock ( '../paginate/Paginator' , ( ) => {
910 const paginate = vi . fn ( ) ;
@@ -22,6 +23,7 @@ vi.mock('./utils', async () => {
2223 sectionFooterHeight : 5 ,
2324 } ) ,
2425 createSectionPageHeightPlugin : vi . fn ( ) . mockReturnValue ( { } ) ,
26+ jsonDataReader : vi . fn ( ) ,
2527 } ;
2628} ) ;
2729
@@ -53,7 +55,7 @@ describe('ReportBuilder', () => {
5355
5456 it . for ( [ true , false ] ) (
5557 'tryAddSection should add a section and dispatch sectionCreated' ,
56- ( withsuspense ) => {
58+ async ( withsuspense ) => {
5759 const sectionCreated = vi . fn ( ) ;
5860 rb . monitor . addEventListener ( 'sectionCreated' , sectionCreated ) ;
5961
@@ -62,7 +64,11 @@ describe('ReportBuilder', () => {
6264 suspense : withsuspense ? [ Promise . resolve ( ) ] : [ ] ,
6365 } ;
6466
65- const added = rb . tryAddSection ( testOptions , components , ( ) => { } ) ;
67+ const added = await rb . tryAddSection (
68+ testOptions ,
69+ components ,
70+ ( ) => { }
71+ ) ;
6672 expect ( added ) . toBe ( true ) ;
6773
6874 expect ( sectionCreated ) . toHaveBeenCalled ( ) ;
@@ -74,9 +80,9 @@ describe('ReportBuilder', () => {
7480 }
7581 ) ;
7682
77- it ( 'tryAddSection should return false if section id already exists' , ( ) => {
78- rb . tryAddSection ( options , components , ( ) => { } ) ;
79- const result = rb . tryAddSection ( options , components , ( ) => { } ) ;
83+ it ( 'tryAddSection should return false if section id already exists' , async ( ) => {
84+ await rb . tryAddSection ( options , components , ( ) => { } ) ;
85+ const result = await rb . tryAddSection ( options , components , ( ) => { } ) ;
8086 expect ( result ) . toBe ( false ) ;
8187 } ) ;
8288
@@ -115,7 +121,7 @@ describe('ReportBuilder', () => {
115121
116122 const onPaginationCompleted = vi . fn ( ) ;
117123
118- const added = rb . tryAddSection (
124+ const added = await rb . tryAddSection (
119125 options ,
120126 testComponents ,
121127 onPaginationCompleted
@@ -160,7 +166,7 @@ describe('ReportBuilder', () => {
160166 const section1 = 'se1' ;
161167 const section2 = 'se2' ;
162168
163- rb . tryAddSection (
169+ await rb . tryAddSection (
164170 {
165171 ...options ,
166172 id : section1 ,
@@ -169,7 +175,7 @@ describe('ReportBuilder', () => {
169175 components ,
170176 onPaginationCompleted
171177 ) ;
172- const added = rb . tryAddSection (
178+ const added = await rb . tryAddSection (
173179 {
174180 ...options ,
175181 id : section2 ,
@@ -209,7 +215,7 @@ describe('ReportBuilder', () => {
209215 const paginateMock = vi . mocked ( Paginator . paginate ) ;
210216 paginateMock . mockReturnValue ( [ '<div>page1</div>' ] ) ;
211217
212- rb . tryAddSection ( options , components , vi . fn ( ) ) ;
218+ await rb . tryAddSection ( options , components , vi . fn ( ) ) ;
213219
214220 const section1Promise = rb . schedulePagination ( ) ;
215221 await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
@@ -228,7 +234,7 @@ describe('ReportBuilder', () => {
228234 const paginateMock = vi . mocked ( Paginator . paginate ) ;
229235 paginateMock . mockReturnValue ( [ '<div>page1</div>' ] ) ;
230236
231- rb . tryAddSection ( options , components , vi . fn ( ) ) ;
237+ await rb . tryAddSection ( options , components , vi . fn ( ) ) ;
232238
233239 const section1Promise = rb . schedulePagination ( ) ;
234240 const section2Promise = rb . schedulePagination ( ) ;
@@ -246,7 +252,7 @@ describe('ReportBuilder', () => {
246252 const sectionId = 'test-section' ;
247253 const onPaginationCompleted = vi . fn ( ) ;
248254
249- rb . tryAddSection (
255+ await rb . tryAddSection (
250256 {
251257 id : sectionId ,
252258 size : { width : '100px' , height : '200px' } ,
@@ -260,6 +266,39 @@ describe('ReportBuilder', () => {
260266
261267 expect ( result . sections . length ) . toBe ( 0 ) ;
262268 } ) ;
269+
270+ it ( 'getJsonData should call jsonDataReader once' , async ( ) => {
271+ const data = {
272+ a : 'a' ,
273+ } ;
274+ vi . mocked ( jsonDataReader ) . mockResolvedValue ( data ) ;
275+
276+ const result1 = await rb . getJsonData ( { a : 'b' } ) ;
277+ const result2 = await rb . getJsonData ( { a : 'b' } ) ;
278+
279+ expect ( result1 ) . toMatchObject ( data ) ;
280+ expect ( result2 ) . toMatchObject ( data ) ;
281+ expect ( vi . mocked ( jsonDataReader ) ) . toHaveBeenCalledOnce ( ) ;
282+ } ) ;
283+
284+ it ( 'getJsonData should return default value when jsonDataReader is not available' , async ( ) => {
285+ const defaultData = {
286+ a : 'a' ,
287+ } ;
288+ vi . mocked ( jsonDataReader ) . mockResolvedValue ( undefined ) ;
289+
290+ const result = await rb . getJsonData ( defaultData ) ;
291+
292+ expect ( result ) . toMatchObject ( defaultData ) ;
293+ } ) ;
294+
295+ it ( 'getJsonData should return null without default value and jsonDataReader' , async ( ) => {
296+ vi . mocked ( jsonDataReader ) . mockResolvedValue ( null ) ;
297+
298+ const result = await rb . getJsonData ( ) ;
299+
300+ expect ( result ) . toBeNull ( ) ;
301+ } ) ;
263302} ) ;
264303
265304describe ( 'ReportBuilder constructor' , ( ) => {
0 commit comments