@@ -40,4 +40,59 @@ describe('remote sync', () => {
4040 await session2 . dispose ( ) ;
4141 await repo . stopTab ( ) ;
4242 } , 10_000 ) ;
43+
44+ test ( 'synchronizes both blocks after user opens a connected browser tab' , async ( ) => {
45+ const connected$ = new BehaviorSubject < boolean > ( true ) ;
46+ const repo = Testbed . createRepo ( { connected$} ) ;
47+ const schema = s . obj ( { foo : s . str ( 'bar' ) } ) ;
48+ const id1 = repo . blockId ;
49+ const { session : session1 } = repo . sessions . make ( { id : id1 , schema} ) ;
50+ await until ( async ( ) => {
51+ try {
52+ await repo . getModelFromRemote ( id1 ) ;
53+ return true ;
54+ } catch {
55+ return false ;
56+ }
57+ } ) ;
58+ const model1 = await repo . getModelFromRemote ( id1 ) ;
59+ expect ( model1 . view ( ) ) . toEqual ( { foo : 'bar' } ) ;
60+ connected$ . next ( false ) ;
61+ const id2 = [ ...repo . col , repo . tab . browser . global . genId ( ) ] ;
62+ const { session : session3 } = repo . sessions . make ( { id : id2 , schema : s . obj ( { a : s . str ( 'b' ) } ) } ) ;
63+ session1 . model . api . obj ( [ ] ) . set ( {
64+ foo : 'baz' ,
65+ x : 'y' ,
66+ } ) ;
67+ const { session : session2 } = await repo . sessions . make ( { id : id1 , schema} ) ;
68+ await until ( ( ) => session2 . model . view ( ) ?. foo === 'baz' ) ;
69+ await tick ( 123 ) ;
70+ const model2 = await repo . getModelFromRemote ( id1 ) ;
71+ expect ( model2 . view ( ) ) . toEqual ( { foo : 'bar' } ) ;
72+ const repo2 = repo . tab . browser . createTab ( ) . createRepo ( ) ; // new tab
73+ await until ( async ( ) => {
74+ const model = await repo2 . getModelFromRemote ( id1 ) ;
75+ return model . view ( ) ?. foo === 'baz' ;
76+ } ) ;
77+ const model3 = await repo . getModelFromRemote ( id1 ) ;
78+ expect ( model3 . view ( ) ) . toEqual ( { foo : 'baz' , x : 'y' } ) ;
79+ await until ( async ( ) => {
80+ try {
81+ await repo . getModelFromRemote ( id2 ) ;
82+ return true ;
83+ } catch {
84+ return false ;
85+ }
86+ } ) ;
87+ const model4 = await repo2 . getModelFromRemote ( id2 ) ;
88+ expect ( model4 . view ( ) ) . toEqual ( { a : 'b' } ) ;
89+ const session4 = await repo2 . sessions . load ( { id : id2 , remote : { } } ) ;
90+ expect ( session4 . model . view ( ) ) . toEqual ( { a : 'b' } ) ;
91+ await session1 . dispose ( ) ;
92+ await session2 . dispose ( ) ;
93+ await session3 . dispose ( ) ;
94+ await session4 . dispose ( ) ;
95+ await repo . stopTab ( ) ;
96+ await repo2 . stopTab ( ) ;
97+ } , 10_000 ) ;
4398} ) ;
0 commit comments