@@ -86,7 +86,7 @@ afterEach(() => {
86
86
} )
87
87
88
88
it ( 'creates, edits, and publishes a document' , async ( ) => {
89
- const doc = createDocumentHandle ( { documentId : 'doc-single' , documentType : 'article' } )
89
+ const doc = { documentId : 'doc-single' , documentType : 'article' , source }
90
90
const documentState = getDocumentState ( instance , doc )
91
91
92
92
// Initially the document is undefined
@@ -122,7 +122,7 @@ it('creates, edits, and publishes a document', async () => {
122
122
} )
123
123
124
124
it ( 'edits existing documents' , async ( ) => {
125
- const doc = createDocumentHandle ( { documentId : 'existing-doc' , documentType : 'article' } )
125
+ const doc = { documentId : 'existing-doc' , documentType : 'article' , source }
126
126
const state = getDocumentState ( instance , doc )
127
127
128
128
// not subscribed yet so the value is undefined
@@ -151,16 +151,16 @@ it('edits existing documents', async () => {
151
151
} )
152
152
153
153
it ( 'sets optimistic changes synchronously' , async ( ) => {
154
- const doc = createDocumentHandle ( { documentId : 'optimistic' , documentType : 'article' } )
154
+ const doc = { documentId : 'optimistic' , documentType : 'article' }
155
155
156
- const state1 = getDocumentState ( instance1 , doc )
157
- const state2 = getDocumentState ( instance2 , doc )
156
+ const state1 = getDocumentState ( instance1 , { ... doc , source : source1 } )
157
+ const state2 = getDocumentState ( instance2 , { ... doc , source : source2 } )
158
158
159
159
const unsubscribe1 = state1 . subscribe ( )
160
160
const unsubscribe2 = state2 . subscribe ( )
161
161
162
162
// wait until the value is primed in the store
163
- await resolveDocument ( instance1 , doc )
163
+ await resolveDocument ( instance1 , { ... doc , source : source1 } )
164
164
165
165
// then the actions are synchronous
166
166
expect ( state1 . getCurrent ( ) ) . toBeNull ( )
@@ -204,9 +204,9 @@ it('sets optimistic changes synchronously', async () => {
204
204
} )
205
205
206
206
it ( 'propagates changes between two instances' , async ( ) => {
207
- const doc = createDocumentHandle ( { documentId : 'doc-collab' , documentType : 'article' } )
208
- const state1 = getDocumentState ( instance1 , doc )
209
- const state2 = getDocumentState ( instance2 , doc )
207
+ const doc = { documentId : 'doc-collab' , documentType : 'article' }
208
+ const state1 = getDocumentState ( instance1 , { ... doc , source : source1 } )
209
+ const state2 = getDocumentState ( instance2 , { ... doc , source : source2 } )
210
210
211
211
const state1Unsubscribe = state1 . subscribe ( )
212
212
const state2Unsubscribe = state2 . subscribe ( )
@@ -237,9 +237,9 @@ it('propagates changes between two instances', async () => {
237
237
} )
238
238
239
239
it ( 'handles concurrent edits and resolves conflicts' , async ( ) => {
240
- const doc = createDocumentHandle ( { documentId : 'doc-concurrent' , documentType : 'article' } )
241
- const state1 = getDocumentState ( instance1 , doc )
242
- const state2 = getDocumentState ( instance2 , doc )
240
+ const doc = { documentId : 'doc-concurrent' , documentType : 'article' }
241
+ const state1 = getDocumentState ( instance1 , { ... doc , source : source1 } )
242
+ const state2 = getDocumentState ( instance2 , { ... doc , source : source2 } )
243
243
244
244
const state1Unsubscribe = state1 . subscribe ( )
245
245
const state2Unsubscribe = state2 . subscribe ( )
@@ -281,7 +281,7 @@ it('handles concurrent edits and resolves conflicts', async () => {
281
281
} )
282
282
283
283
it ( 'unpublishes and discards a document' , async ( ) => {
284
- const doc = createDocumentHandle ( { documentId : 'doc-pub-unpub' , documentType : 'article' } )
284
+ const doc = { documentId : 'doc-pub-unpub' , documentType : 'article' , source }
285
285
const documentState = getDocumentState ( instance , doc )
286
286
const unsubscribe = documentState . subscribe ( )
287
287
@@ -312,7 +312,7 @@ it('unpublishes and discards a document', async () => {
312
312
} )
313
313
314
314
it ( 'deletes a document' , async ( ) => {
315
- const doc = createDocumentHandle ( { documentId : 'doc-delete' , documentType : 'article' } )
315
+ const doc = { documentId : 'doc-delete' , documentType : 'article' , source }
316
316
317
317
const documentState = getDocumentState ( instance , doc )
318
318
const unsubscribe = documentState . subscribe ( )
@@ -333,7 +333,7 @@ it('deletes a document', async () => {
333
333
} )
334
334
335
335
it ( 'cleans up document state when there are no subscribers' , async ( ) => {
336
- const doc = createDocumentHandle ( { documentId : 'doc-cleanup' , documentType : 'article' } )
336
+ const doc = { documentId : 'doc-cleanup' , documentType : 'article' , source }
337
337
const documentState = getDocumentState ( instance , doc )
338
338
339
339
// Subscribe to the document state.
@@ -356,7 +356,7 @@ it('cleans up document state when there are no subscribers', async () => {
356
356
} )
357
357
358
358
it ( 'fetches documents if there are no active subscriptions for the actions applied' , async ( ) => {
359
- const doc = createDocumentHandle ( { documentId : 'existing-doc' , documentType : 'article' } )
359
+ const doc = { documentId : 'existing-doc' , documentType : 'article' , source }
360
360
361
361
const { getCurrent} = getDocumentState ( instance , doc )
362
362
expect ( getCurrent ( ) ) . toBeUndefined ( )
@@ -411,7 +411,7 @@ it('fetches documents if there are no active subscriptions for the actions appli
411
411
} )
412
412
413
413
it ( 'batches edit transaction into one outgoing transaction' , async ( ) => {
414
- const doc = createDocumentHandle ( { documentId : crypto . randomUUID ( ) , documentType : 'article' } )
414
+ const doc = { documentId : crypto . randomUUID ( ) , documentType : 'article' , source }
415
415
416
416
const unsubscribe = getDocumentState ( instance , doc ) . subscribe ( )
417
417
@@ -438,7 +438,7 @@ it('batches edit transaction into one outgoing transaction', async () => {
438
438
} )
439
439
440
440
it ( 'provides the consistency status via `getDocumentSyncStatus`' , async ( ) => {
441
- const doc = createDocumentHandle ( { documentId : crypto . randomUUID ( ) , documentType : 'article' } )
441
+ const doc = { documentId : crypto . randomUUID ( ) , documentType : 'article' , source }
442
442
443
443
const syncStatus = getDocumentSyncStatus ( instance , doc )
444
444
expect ( syncStatus . getCurrent ( ) ) . toBeUndefined ( )
@@ -482,15 +482,18 @@ it('reverts failed outgoing transaction locally', async () => {
482
482
} )
483
483
484
484
const revertedEventPromise = new Promise < TransactionRevertedEvent > ( ( resolve ) => {
485
- const unsubscribe = subscribeDocumentEvents ( instance , ( e ) => {
486
- if ( e . type === 'reverted' ) {
487
- resolve ( e )
488
- unsubscribe ( )
489
- }
485
+ const unsubscribe = subscribeDocumentEvents ( instance , {
486
+ onEvent : ( e ) => {
487
+ if ( e . type === 'reverted' ) {
488
+ resolve ( e )
489
+ unsubscribe ( )
490
+ }
491
+ } ,
492
+ source,
490
493
} )
491
494
} )
492
495
493
- const doc = createDocumentHandle ( { documentId : crypto . randomUUID ( ) , documentType : 'article' } )
496
+ const doc = { documentId : crypto . randomUUID ( ) , documentType : 'article' , source }
494
497
495
498
const { getCurrent, subscribe} = getDocumentState ( instance , doc )
496
499
const unsubscribe = subscribe ( )
@@ -546,15 +549,18 @@ it('reverts failed outgoing transaction locally', async () => {
546
549
547
550
it ( 'removes a queued transaction if it fails to apply' , async ( ) => {
548
551
const actionErrorEventPromise = new Promise < ActionErrorEvent > ( ( resolve ) => {
549
- const unsubscribe = subscribeDocumentEvents ( instance , ( e ) => {
550
- if ( e . type === 'error' ) {
551
- resolve ( e )
552
- unsubscribe ( )
553
- }
552
+ const unsubscribe = subscribeDocumentEvents ( instance , {
553
+ onEvent : ( e ) => {
554
+ if ( e . type === 'error' ) {
555
+ resolve ( e )
556
+ unsubscribe ( )
557
+ }
558
+ } ,
559
+ source,
554
560
} )
555
561
} )
556
562
557
- const doc = createDocumentHandle ( { documentId : crypto . randomUUID ( ) , documentType : 'article' } )
563
+ const doc = { documentId : crypto . randomUUID ( ) , documentType : 'article' , source }
558
564
const state = getDocumentState ( instance , doc )
559
565
const unsubscribe = state . subscribe ( )
560
566
@@ -587,10 +593,11 @@ it('returns allowed true when no permission errors occur', async () => {
587
593
client . observable . request = vi . fn ( ) . mockReturnValue ( of ( datasetAcl ) )
588
594
589
595
// Create a document and subscribe to it.
590
- const doc = createDocumentHandle ( {
596
+ const doc = {
591
597
documentId : 'doc-perm-allowed' ,
592
598
documentType : 'article' ,
593
- } )
599
+ source,
600
+ }
594
601
const state = getDocumentState ( instance , doc )
595
602
const unsubscribe = state . subscribe ( )
596
603
await applyDocumentActions ( instance , { actions : [ createDocument ( doc ) ] , source} ) . then ( ( r ) =>
@@ -606,6 +613,7 @@ it('returns allowed true when no permission errors occur', async () => {
606
613
patches : [ { set : { title : 'New Title' } } ] ,
607
614
} ,
608
615
] ,
616
+ source,
609
617
} )
610
618
// Wait briefly to allow permissions calculation.
611
619
await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
@@ -638,7 +646,7 @@ it('returns allowed false with reasons when permission errors occur', async () =
638
646
vi . mocked ( client . request ) . mockResolvedValue ( datasetAcl )
639
647
640
648
const doc = createDocumentHandle ( { documentId : 'doc-perm-denied' , documentType : 'article' } )
641
- const result = await resolvePermissions ( instance , { actions : [ createDocument ( doc ) ] } )
649
+ const result = await resolvePermissions ( instance , { actions : [ createDocument ( doc ) ] , source } )
642
650
643
651
const message = 'You do not have permission to create a draft for document "doc-perm-denied".'
644
652
expect ( result ) . toMatchObject ( {
@@ -659,17 +667,19 @@ it('fetches dataset ACL and updates grants in the document store state', async (
659
667
const book = createDocumentHandle ( { documentId : crypto . randomUUID ( ) , documentType : 'book' } )
660
668
const author = createDocumentHandle ( { documentId : crypto . randomUUID ( ) , documentType : 'author' } )
661
669
662
- expect ( await resolvePermissions ( instance , { actions : [ createDocument ( book ) ] } ) ) . toEqual ( {
670
+ expect ( await resolvePermissions ( instance , { actions : [ createDocument ( book ) ] , source } ) ) . toEqual ( {
663
671
allowed : true ,
664
672
} )
665
- expect ( await resolvePermissions ( instance , { actions : [ createDocument ( author ) ] } ) ) . toMatchObject ( {
673
+ expect (
674
+ await resolvePermissions ( instance , { actions : [ createDocument ( author ) ] , source} ) ,
675
+ ) . toMatchObject ( {
666
676
allowed : false ,
667
677
message : expect . stringContaining ( 'You do not have permission to create a draft for document' ) ,
668
678
} )
669
679
} )
670
680
671
681
it ( 'returns a promise that resolves when a document has been loaded in the store (useful for suspense)' , async ( ) => {
672
- const doc = createDocumentHandle ( { documentId : crypto . randomUUID ( ) , documentType : 'article' } )
682
+ const doc = { documentId : crypto . randomUUID ( ) , documentType : 'article' , source }
673
683
674
684
expect ( await resolveDocument ( instance , doc ) ) . toBe ( null )
675
685
@@ -691,7 +701,7 @@ it('returns a promise that resolves when a document has been loaded in the store
691
701
692
702
it ( 'emits an event for each action after an outgoing transaction has been accepted' , async ( ) => {
693
703
const handler = vi . fn ( )
694
- const unsubscribe = subscribeDocumentEvents ( instance , handler )
704
+ const unsubscribe = subscribeDocumentEvents ( instance , { onEvent : handler , source } )
695
705
696
706
const documentId = crypto . randomUUID ( )
697
707
const doc = createDocumentHandle ( { documentId, documentType : 'article' } )
0 commit comments