@@ -4,8 +4,13 @@ import { connect } from 'mongodb-data-service';
44import AppRegistry , {
55 createActivateHelpers ,
66} 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' ;
812import { MongoDBInstance } from 'mongodb-instance-model' ;
13+ import type { EventEmitter } from 'events' ;
914import { once } from 'events' ;
1015import sinon from 'sinon' ;
1116import chai , { expect } from 'chai' ;
@@ -110,6 +115,15 @@ function waitForState(store, cb, timeout?: number) {
110115 return waitForStates ( store , [ cb ] , timeout ) ;
111116}
112117
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+
113127const mockFieldStoreService = {
114128 updateFieldsFromDocuments ( ) { } ,
115129 updateFieldsFromSchema ( ) { } ,
@@ -503,7 +517,7 @@ describe('store', function () {
503517 } ) ;
504518
505519 it ( 'sets the error for the document' , function ( done ) {
506- hadronDoc . on ( 'remove-error' , ( { message } ) => {
520+ hadronDoc . on ( DocumentEvents . RemoveError , ( { message } ) => {
507521 expect ( message ) . to . equal ( 'error happened' ) ;
508522 done ( ) ;
509523 } ) ;
@@ -547,11 +561,11 @@ describe('store', function () {
547561 done ( ) ;
548562 } , store ) ;
549563
550- hadronDoc . on ( 'update-blocked' , ( ) => {
564+ hadronDoc . on ( DocumentEvents . UpdateBlocked , ( ) => {
551565 done ( new Error ( "Didn't expect update to be blocked." ) ) ;
552566 } ) ;
553567
554- hadronDoc . on ( 'update-error' , ( errorMessage ) => {
568+ hadronDoc . on ( DocumentEvents . UpdateError , ( errorMessage ) => {
555569 done (
556570 new Error (
557571 `Didn't expect update to error. Errored with message: ${ errorMessage } `
@@ -586,11 +600,11 @@ describe('store', function () {
586600 setTimeout ( ( ) => done ( ) , 100 ) ;
587601 } , store ) ;
588602
589- hadronDoc . on ( 'update-blocked' , ( ) => {
603+ hadronDoc . on ( DocumentEvents . UpdateBlocked , ( ) => {
590604 done ( new Error ( "Didn't expect update to be blocked." ) ) ;
591605 } ) ;
592606
593- hadronDoc . on ( 'update-error' , ( errorMessage ) => {
607+ hadronDoc . on ( DocumentEvents . UpdateError , ( errorMessage ) => {
594608 done (
595609 new Error (
596610 `Didn't expect update to error. Errored with message: ${ errorMessage } `
@@ -613,7 +627,7 @@ describe('store', function () {
613627 } ) ;
614628
615629 it ( 'sets the error for the document' , function ( done ) {
616- hadronDoc . on ( 'update-error' , ( { message } ) => {
630+ hadronDoc . on ( DocumentEvents . UpdateError , ( { message } ) => {
617631 expect ( message ) . to . equal (
618632 'Unable to update, no changes have been made.'
619633 ) ;
@@ -636,7 +650,7 @@ describe('store', function () {
636650 } ) ;
637651
638652 it ( 'sets the error for the document' , function ( done ) {
639- hadronDoc . on ( 'update-error' , ( { message } ) => {
653+ hadronDoc . on ( DocumentEvents . UpdateError , ( { message } ) => {
640654 expect ( message ) . to . equal ( 'error happened' ) ;
641655 done ( ) ;
642656 } ) ;
@@ -655,7 +669,7 @@ describe('store', function () {
655669 } ) ;
656670
657671 it ( 'sets the update blocked for the document' , function ( done ) {
658- hadronDoc . on ( 'update-blocked' , ( ) => {
672+ hadronDoc . on ( DocumentEvents . UpdateBlocked , ( ) => {
659673 done ( ) ;
660674 } ) ;
661675
@@ -728,7 +742,7 @@ describe('store', function () {
728742 const invalidHadronDoc = new HadronDocument ( doc ) ;
729743 ( invalidHadronDoc as any ) . getId = null ;
730744
731- invalidHadronDoc . on ( 'update-error' , ( { message } ) => {
745+ invalidHadronDoc . on ( DocumentEvents . UpdateError , ( { message } ) => {
732746 expect ( message ) . to . equal (
733747 'An error occured when attempting to update the document: this.getId is not a function'
734748 ) ;
@@ -765,7 +779,10 @@ describe('store', function () {
765779 } ) ;
766780
767781 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+ ) ;
769786
770787 await store . updateDocument ( hadronDoc ) ;
771788 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 () {
9981015 } ) ;
9991016
10001017 it ( 'sets the error for the document' , function ( done ) {
1001- hadronDoc . on ( 'update-error' , ( { message } ) => {
1018+ hadronDoc . on ( DocumentEvents . UpdateError , ( { message } ) => {
10021019 expect ( message ) . to . equal ( 'error happened' ) ;
10031020 done ( ) ;
10041021 } ) ;
@@ -1085,7 +1102,10 @@ describe('store', function () {
10851102 } ) ;
10861103
10871104 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+ ) ;
10891109
10901110 await store . replaceDocument ( hadronDoc ) ;
10911111 expect ( ( await updateErrorEvent ) [ 0 ] ) . to . match ( / U p d a t e b l o c k e d / ) ;
0 commit comments