@@ -3,6 +3,7 @@ import { faker } from "@faker-js/faker";
33
44import { render , fireEvent , waitFor } from "@testing-library/preact" ;
55import Annotate from "../components/Annotate" ;
6+ import { Span } from "../annotate" ;
67
78const RANGE = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ;
89const LABELS = RANGE . map ( ( ) => faker . random . word ( ) ) ;
@@ -27,6 +28,7 @@ describe("Annotate", () => {
2728 labels : LABELS ,
2829 initialSpans : [ ] ,
2930 onUpdateSpans : jest . fn ( ) ,
31+ registerSpanChangeCallback : jest . fn ( ) ,
3032 docs : DOCUMENTS ,
3133 } )
3234 ) ;
@@ -47,6 +49,7 @@ describe("Annotate", () => {
4749 labels : LABELS ,
4850 initialSpans : [ ] ,
4951 onUpdateSpans,
52+ registerSpanChangeCallback : jest . fn ( ) ,
5053 docs,
5154 } )
5255 ) ;
@@ -71,4 +74,41 @@ describe("Annotate", () => {
7174 const span = await findByTitle ( LABELS [ 0 ] ) ;
7275 expect ( span . textContent ) . toEqual ( targetText ) ;
7376 } ) ;
77+
78+ test ( "should render spans when Python model changes" , async ( ) => {
79+ let spanChangeCallback = ( spans : Span [ ] [ ] ) => {
80+ console . log ( spans ) ;
81+ } ;
82+ const onUpdateSpans = jest . fn ( ) ;
83+ const docs = [ faker . lorem . sentence ( 20 ) ] ;
84+ const { findByTitle } = render (
85+ h ( Annotate , {
86+ labels : LABELS ,
87+ initialSpans : [ ] ,
88+ onUpdateSpans,
89+ registerSpanChangeCallback : ( callback ) => {
90+ spanChangeCallback = callback ;
91+ } ,
92+ docs,
93+ } )
94+ ) ;
95+
96+ const targetText = docs [ 0 ] . slice ( 8 , 12 ) ;
97+
98+ await waitFor ( ( ) => expect ( spanChangeCallback ) . not . toBeNull ( ) ) ;
99+
100+ spanChangeCallback ( [
101+ [
102+ {
103+ start : 8 ,
104+ end : 12 ,
105+ text : targetText ,
106+ label : { color : "red" , text : LABELS [ 0 ] } ,
107+ } ,
108+ ] ,
109+ ] ) ;
110+
111+ const span = await findByTitle ( LABELS [ 0 ] ) ;
112+ expect ( span . textContent ) . toEqual ( targetText ) ;
113+ } ) ;
74114} ) ;
0 commit comments