@@ -4,8 +4,13 @@ import { connect } from 'mongodb-data-service';
4
4
import AppRegistry , {
5
5
createActivateHelpers ,
6
6
} from '@mongodb-js/compass-app-registry' ;
7
- import HadronDocument , { Element } from 'hadron-document' ;
7
+ import HadronDocument , {
8
+ DocumentEvents ,
9
+ Element ,
10
+ type DocumentEventsType ,
11
+ } from 'hadron-document' ;
8
12
import { MongoDBInstance } from 'mongodb-instance-model' ;
13
+ import type { EventEmitter } from 'events' ;
9
14
import { once } from 'events' ;
10
15
import sinon from 'sinon' ;
11
16
import chai , { expect } from 'chai' ;
@@ -110,6 +115,15 @@ function waitForState(store, cb, timeout?: number) {
110
115
return waitForStates ( store , [ cb ] , timeout ) ;
111
116
}
112
117
118
+ function onceDocumentEvent (
119
+ doc : HadronDocument ,
120
+ event : DocumentEventsType
121
+ ) : Promise < unknown [ ] > {
122
+ // The once function was not meant for strongly typed events, so we need to
123
+ // do some additional type casting.
124
+ return once ( doc as unknown as EventEmitter , event as string ) ;
125
+ }
126
+
113
127
const mockFieldStoreService = {
114
128
updateFieldsFromDocuments ( ) { } ,
115
129
updateFieldsFromSchema ( ) { } ,
@@ -503,7 +517,7 @@ describe('store', function () {
503
517
} ) ;
504
518
505
519
it ( 'sets the error for the document' , function ( done ) {
506
- hadronDoc . on ( 'remove-error' , ( { message } ) => {
520
+ hadronDoc . on ( DocumentEvents . RemoveError , ( { message } ) => {
507
521
expect ( message ) . to . equal ( 'error happened' ) ;
508
522
done ( ) ;
509
523
} ) ;
@@ -547,11 +561,11 @@ describe('store', function () {
547
561
done ( ) ;
548
562
} , store ) ;
549
563
550
- hadronDoc . on ( 'update-blocked' , ( ) => {
564
+ hadronDoc . on ( DocumentEvents . UpdateBlocked , ( ) => {
551
565
done ( new Error ( "Didn't expect update to be blocked." ) ) ;
552
566
} ) ;
553
567
554
- hadronDoc . on ( 'update-error' , ( errorMessage ) => {
568
+ hadronDoc . on ( DocumentEvents . UpdateError , ( errorMessage ) => {
555
569
done (
556
570
new Error (
557
571
`Didn't expect update to error. Errored with message: ${ errorMessage } `
@@ -586,11 +600,11 @@ describe('store', function () {
586
600
setTimeout ( ( ) => done ( ) , 100 ) ;
587
601
} , store ) ;
588
602
589
- hadronDoc . on ( 'update-blocked' , ( ) => {
603
+ hadronDoc . on ( DocumentEvents . UpdateBlocked , ( ) => {
590
604
done ( new Error ( "Didn't expect update to be blocked." ) ) ;
591
605
} ) ;
592
606
593
- hadronDoc . on ( 'update-error' , ( errorMessage ) => {
607
+ hadronDoc . on ( DocumentEvents . UpdateError , ( errorMessage ) => {
594
608
done (
595
609
new Error (
596
610
`Didn't expect update to error. Errored with message: ${ errorMessage } `
@@ -613,7 +627,7 @@ describe('store', function () {
613
627
} ) ;
614
628
615
629
it ( 'sets the error for the document' , function ( done ) {
616
- hadronDoc . on ( 'update-error' , ( { message } ) => {
630
+ hadronDoc . on ( DocumentEvents . UpdateError , ( { message } ) => {
617
631
expect ( message ) . to . equal (
618
632
'Unable to update, no changes have been made.'
619
633
) ;
@@ -636,7 +650,7 @@ describe('store', function () {
636
650
} ) ;
637
651
638
652
it ( 'sets the error for the document' , function ( done ) {
639
- hadronDoc . on ( 'update-error' , ( { message } ) => {
653
+ hadronDoc . on ( DocumentEvents . UpdateError , ( { message } ) => {
640
654
expect ( message ) . to . equal ( 'error happened' ) ;
641
655
done ( ) ;
642
656
} ) ;
@@ -655,7 +669,7 @@ describe('store', function () {
655
669
} ) ;
656
670
657
671
it ( 'sets the update blocked for the document' , function ( done ) {
658
- hadronDoc . on ( 'update-blocked' , ( ) => {
672
+ hadronDoc . on ( DocumentEvents . UpdateBlocked , ( ) => {
659
673
done ( ) ;
660
674
} ) ;
661
675
@@ -728,7 +742,7 @@ describe('store', function () {
728
742
const invalidHadronDoc = new HadronDocument ( doc ) ;
729
743
( invalidHadronDoc as any ) . getId = null ;
730
744
731
- invalidHadronDoc . on ( 'update-error' , ( { message } ) => {
745
+ invalidHadronDoc . on ( DocumentEvents . UpdateError , ( { message } ) => {
732
746
expect ( message ) . to . equal (
733
747
'An error occured when attempting to update the document: this.getId is not a function'
734
748
) ;
@@ -765,7 +779,10 @@ describe('store', function () {
765
779
} ) ;
766
780
767
781
it ( 'rejects the update and emits update-error' , async function ( ) {
768
- const updateErrorEvent = once ( hadronDoc , 'update-error' ) ;
782
+ const updateErrorEvent = onceDocumentEvent (
783
+ hadronDoc ,
784
+ DocumentEvents . UpdateError
785
+ ) ;
769
786
770
787
await store . updateDocument ( hadronDoc ) ;
771
788
expect ( ( await updateErrorEvent ) [ 0 ] ) . to . match ( / U p d a t e b l o c k e d / ) ;
@@ -998,7 +1015,7 @@ describe('store', function () {
998
1015
} ) ;
999
1016
1000
1017
it ( 'sets the error for the document' , function ( done ) {
1001
- hadronDoc . on ( 'update-error' , ( { message } ) => {
1018
+ hadronDoc . on ( DocumentEvents . UpdateError , ( { message } ) => {
1002
1019
expect ( message ) . to . equal ( 'error happened' ) ;
1003
1020
done ( ) ;
1004
1021
} ) ;
@@ -1085,7 +1102,10 @@ describe('store', function () {
1085
1102
} ) ;
1086
1103
1087
1104
it ( 'rejects the update and emits update-error' , async function ( ) {
1088
- const updateErrorEvent = once ( hadronDoc , 'update-error' ) ;
1105
+ const updateErrorEvent = onceDocumentEvent (
1106
+ hadronDoc ,
1107
+ DocumentEvents . UpdateError
1108
+ ) ;
1089
1109
1090
1110
await store . replaceDocument ( hadronDoc ) ;
1091
1111
expect ( ( await updateErrorEvent ) [ 0 ] ) . to . match ( / U p d a t e b l o c k e d / ) ;
0 commit comments