@@ -37,24 +37,105 @@ describe('mongoose-dp', () => {
37
37
expect ( diffs [ 0 ] . v ) . toBe ( 1 ) ;
38
38
} ) ;
39
39
40
- // it('save array diffs properly', async () => {
41
- // await Post.create({ title: 'arrayCheck', subjects: [{ name: 'was' }] });
42
- // const post: PostDoc = (await Post.findOne({ title: 'arrayCheck' }).exec(): any);
43
- // post.subjects = [{ name: 'was' }, { name: 'first' }];
44
- // await post.save();
45
-
46
- // const post1: PostDoc = (await Post.findOne({ title: 'arrayCheck' }).exec(): any);
47
- // post1.subjects = [{ name: 'was' }, { name: 'first' }, { name: 'second' }];
48
- // await post1.save();
49
-
50
- // const post2: PostDoc = (await Post.findOne({ title: 'arrayCheck' }).exec(): any);
51
- // post2.subjects = [{ name: 'first' }, { name: 'second' }];
52
- // await post2.save();
53
-
54
- // const Diff = Post.diffModel();
55
- // const diffs = await Diff.findByDocId(post2._id);
56
- // expect(diffs).toMatchInli
57
- // const merged = await Diff.mergeDiffs(post2);
58
- // expect(merged).toBe();
59
- // });
40
+ describe ( 'save array diffs properly' , ( ) => {
41
+ it ( 'add new element' , async ( ) => {
42
+ await Post . create ( { title : 'newElement' , subjects : [ { name : 'test' } ] } ) ;
43
+ const post : PostDoc = ( await Post . findOne ( {
44
+ title : 'newElement' ,
45
+ } ) . exec ( ) : any ) ;
46
+ post . subjects = [ { name : 'test' } , { name : 'new test' } ] ;
47
+ await post . save ( ) ;
48
+
49
+ const Diff = Post . diffModel ( ) ;
50
+ const diffs = await Diff . findByDocId ( post . _id ) ;
51
+ expect ( diffs [ 0 ] . c ) . toMatchInlineSnapshot ( `
52
+ CoreMongooseArray [
53
+ Object {
54
+ "i": 1,
55
+ "it": Object {
56
+ "k": "N",
57
+ "r": Object {
58
+ "name": "new test",
59
+ },
60
+ },
61
+ "k": "A",
62
+ "p": Array [
63
+ "subjects",
64
+ ],
65
+ },
66
+ ]
67
+ ` ) ;
68
+ } ) ;
69
+
70
+ it ( 'edit existed element' , async ( ) => {
71
+ await Post . create ( {
72
+ title : 'existedElement' ,
73
+ subjects : [ { name : 'was' } ] ,
74
+ } ) ;
75
+ const post : PostDoc = ( await Post . findOne ( {
76
+ title : 'existedElement' ,
77
+ } ) . exec ( ) : any ) ;
78
+ post . subjects = [ { name : 'become' } ] ;
79
+ await post . save ( ) ;
80
+
81
+ const Diff = Post . diffModel ( ) ;
82
+ const diffs = await Diff . findByDocId ( post . _id ) ;
83
+ expect ( diffs [ 0 ] . c ) . toMatchInlineSnapshot ( `
84
+ CoreMongooseArray [
85
+ Object {
86
+ "k": "E",
87
+ "l": "was",
88
+ "p": Array [
89
+ "subjects",
90
+ "0",
91
+ "name",
92
+ ],
93
+ "r": "become",
94
+ },
95
+ ]
96
+ ` ) ;
97
+ } ) ;
98
+
99
+ it ( 'delete element' , async ( ) => {
100
+ await Post . create ( {
101
+ title : 'deleteElement' ,
102
+ subjects : [ { name : 'one' } , { name : 'two' } , { name : 'three' } ] ,
103
+ } ) ;
104
+ const post : PostDoc = ( await Post . findOne ( {
105
+ title : 'deleteElement' ,
106
+ } ) . exec ( ) : any ) ;
107
+ post . subjects = [ { name : 'one' } , { name : 'three' } ] ;
108
+ await post . save ( ) ;
109
+
110
+ const Diff = Post . diffModel ( ) ;
111
+ const diffs = await Diff . findByDocId ( post . _id ) ;
112
+ expect ( diffs [ 0 ] . c ) . toMatchInlineSnapshot ( `
113
+ CoreMongooseArray [
114
+ Object {
115
+ "i": 2,
116
+ "it": Object {
117
+ "k": "D",
118
+ "l": Object {
119
+ "name": "three",
120
+ },
121
+ },
122
+ "k": "A",
123
+ "p": Array [
124
+ "subjects",
125
+ ],
126
+ },
127
+ Object {
128
+ "k": "E",
129
+ "l": "two",
130
+ "p": Array [
131
+ "subjects",
132
+ "1",
133
+ "name",
134
+ ],
135
+ "r": "three",
136
+ },
137
+ ]
138
+ ` ) ;
139
+ } ) ;
140
+ } ) ;
60
141
} ) ;
0 commit comments