@@ -100,6 +100,42 @@ describe('discriminator docs', function() {
100100 assert . equal ( event3 . __t , 'SignedUp' ) ;
101101 } ) ;
102102
103+ it ( 'Update discriminator key' , async function ( ) {
104+ let event = new ClickedLinkEvent ( { time : Date . now ( ) , url : 'google.com' } ) ;
105+ await event . save ( ) ;
106+
107+ event . __t = 'SignedUp' ;
108+ // ValidationError: ClickedLink validation failed: __t: Cast to String failed for value "SignedUp" (type string) at path "__t"
109+ // acquit:ignore:start
110+ await assert . rejects ( async ( ) => {
111+ // acquit:ignore:end
112+ await event . save ( ) ;
113+ // acquit:ignore:start
114+ } , / _ _ t : C a s t t o S t r i n g f a i l e d / ) ;
115+ // acquit:ignore:end
116+
117+ event = await ClickedLinkEvent . findByIdAndUpdate ( event . _id , { __t : 'SignedUp' } , { new : true } ) ;
118+ event . __t ; // 'ClickedLink', update was a no-op
119+ // acquit:ignore:start
120+ assert . equal ( event . __t , 'ClickedLink' ) ;
121+ // acquit:ignore:end
122+ } ) ;
123+
124+ it ( 'use overwriteDiscriminatorKey to change discriminator key' , async function ( ) {
125+ let event = new ClickedLinkEvent ( { time : Date . now ( ) , url : 'google.com' } ) ;
126+ await event . save ( ) ;
127+
128+ event = await ClickedLinkEvent . findByIdAndUpdate (
129+ event . _id ,
130+ { __t : 'SignedUp' } ,
131+ { overwriteDiscriminatorKey : true , new : true }
132+ ) ;
133+ event . __t ; // 'SignedUp', updated discriminator key
134+ // acquit:ignore:start
135+ assert . equal ( event . __t , 'SignedUp' ) ;
136+ // acquit:ignore:end
137+ } ) ;
138+
103139 /**
104140 * Discriminator models are special; they attach the discriminator key
105141 * to queries. In other words, `find()`, `count()`, `aggregate()`, etc.
0 commit comments