@@ -22,6 +22,7 @@ jest.dontMock('../ParseFile');
22
22
jest . dontMock ( '../ParseGeoPoint' ) ;
23
23
jest . dontMock ( '../ParseObject' ) ;
24
24
jest . dontMock ( '../ParseOp' ) ;
25
+ jest . dontMock ( '../ParseRelation' ) ;
25
26
jest . dontMock ( '../RESTController' ) ;
26
27
jest . dontMock ( '../SingleInstanceStateController' ) ;
27
28
jest . dontMock ( '../TaskQueue' ) ;
@@ -35,13 +36,48 @@ jest.dontMock('./test_helpers/mockXHR');
35
36
jest . useFakeTimers ( ) ;
36
37
37
38
const mockRelation = function ( parent , key ) {
38
- this . parentClass = parent . className ;
39
- this . parentId = parent . id ;
39
+ // The parent and key fields will be populated by the parent
40
+ if ( parent ) {
41
+ this . parentClass = parent . className ;
42
+ this . parentId = parent . id ;
43
+ }
40
44
this . key = key ;
41
45
} ;
42
46
mockRelation . prototype . add = function ( obj ) {
43
47
this . targetClassName = obj . className ;
44
48
} ;
49
+ mockRelation . prototype . toJSON = function ( ) {
50
+ return {
51
+ __type : 'Relation' ,
52
+ className : this . targetClassName
53
+ } ;
54
+ } ;
55
+ mockRelation . prototype . _ensureParentAndKey = function ( parent , key ) {
56
+ this . key = this . key || key ;
57
+ if ( this . key !== key ) {
58
+ throw new Error (
59
+ 'Internal Error. Relation retrieved from two different keys.'
60
+ ) ;
61
+ }
62
+ if ( this . parent ) {
63
+ if ( this . parent . className !== parent . className ) {
64
+ throw new Error (
65
+ 'Internal Error. Relation retrieved from two different Objects.'
66
+ ) ;
67
+ }
68
+ if ( this . parent . id ) {
69
+ if ( this . parent . id !== parent . id ) {
70
+ throw new Error (
71
+ 'Internal Error. Relation retrieved from two different Objects.'
72
+ ) ;
73
+ }
74
+ } else if ( parent . id ) {
75
+ this . parent = parent ;
76
+ }
77
+ } else {
78
+ this . parent = parent ;
79
+ }
80
+ } ;
45
81
jest . setMock ( '../ParseRelation' , mockRelation ) ;
46
82
47
83
const mockQuery = function ( className ) {
@@ -514,6 +550,17 @@ describe('ParseObject', () => {
514
550
expect ( rel . targetClassName ) . toBe ( 'Person' ) ;
515
551
} ) ;
516
552
553
+ it ( 'can be cloned with relation (#381)' , ( ) => {
554
+ const relationJSON = { __type : 'Relation' , className : 'Bar' } ;
555
+ const o = ParseObject . fromJSON ( {
556
+ objectId : '7777777777' ,
557
+ className : 'Foo' ,
558
+ aRelation : relationJSON ,
559
+ } ) ;
560
+ const o2 = o . clone ( ) ;
561
+ expect ( o2 . _getSaveJSON ( ) . aRelation ) . toEqual ( relationJSON ) ;
562
+ } ) ;
563
+
517
564
it ( 'can detect dirty object children' , ( ) => {
518
565
const o = new ParseObject ( 'Person' ) ;
519
566
o . _finishFetch ( {
0 commit comments