@@ -223,4 +223,45 @@ describe('mongoose-patch-history', () => {
223223 ) . then ( ( ) => done ( ) ) . catch ( done )
224224 } )
225225 } )
226+
227+ describe ( 'jsonpatch.compare' , ( ) => {
228+ let Organization
229+ let Person
230+
231+ before ( ( ) => {
232+ Organization = mongoose . model ( 'Organization' , new mongoose . Schema ( {
233+ name : String
234+ } ) )
235+
236+ const PersonSchema = new mongoose . Schema ( {
237+ name : String ,
238+ organization : {
239+ type : mongoose . Schema . Types . ObjectId ,
240+ ref : 'Organization'
241+ }
242+ } )
243+
244+ PersonSchema . plugin ( patchHistory , { mongoose, name : 'roomPatches' } )
245+ Person = mongoose . model ( 'Person' , PersonSchema )
246+ } )
247+
248+ it ( 'is able to handle ObjectId references correctly' , ( done ) => {
249+ Organization . create ( { text : 'Home' } )
250+ . then ( ( o1 ) => join ( o1 , Organization . create ( { text : 'Work' } ) ) )
251+ . then ( ( [ o1 , o2 ] ) => join ( o1 , o2 , Person . create ( { name : 'Bob' , organization : o1 . _id } ) ) )
252+ . then ( ( [ o1 , o2 , p ] ) => join ( o1 , o2 , p . set ( { organization : o2 . _id } ) . save ( ) ) )
253+ . then ( ( [ o1 , o2 , p ] ) => join ( o1 , o2 , p . patches . find ( { ref : p . id } ) ) )
254+ . then ( ( [ o1 , o2 , patches ] ) => {
255+ const pathFilter = ( path ) => ( elem ) => elem . path === path
256+ const firstOrganizationOperation = patches [ 0 ] . ops . find ( pathFilter ( '/organization' ) )
257+ const secondOrganizationOperation = patches [ 1 ] . ops . find ( pathFilter ( '/organization' ) )
258+ assert . equal ( patches . length , 2 )
259+ assert ( firstOrganizationOperation )
260+ assert ( secondOrganizationOperation )
261+ assert . equal ( firstOrganizationOperation . value , o1 . _id . toString ( ) )
262+ assert . equal ( secondOrganizationOperation . value , o2 . _id . toString ( ) )
263+ } )
264+ . then ( done ) . catch ( done )
265+ } )
266+ } )
226267} )
0 commit comments