1
1
import { TestAnchor , TestOverlay } from '../../test/helpers/test-classes.js' ;
2
+ import { Container } from '../decorators/container.js' ;
3
+ import { DiffAction } from '../functions/diff/diff.js' ;
2
4
import { App } from '../models/app/app.model.js' ;
5
+ import { OverlayDataRepository , OverlayDataRepositoryFactory } from './overlay-data.repository.js' ;
3
6
import { AOverlay } from './overlay.abstract.js' ;
7
+ import { OverlayService , OverlayServiceFactory } from './overlay.service.js' ;
4
8
5
9
describe ( 'Overlay UT' , ( ) => {
10
+ beforeEach ( ( ) => {
11
+ Container . registerFactory ( OverlayDataRepository , OverlayDataRepositoryFactory ) ;
12
+ Container . get ( OverlayDataRepository , { args : [ true , [ ] , [ ] ] } ) ;
13
+
14
+ Container . registerFactory ( OverlayService , OverlayServiceFactory ) ;
15
+ } ) ;
16
+
17
+ afterEach ( ( ) => {
18
+ Container . reset ( ) ;
19
+ } ) ;
20
+
21
+ describe ( 'addAnchor()' , ( ) => {
22
+ it ( 'should create dependency between overlay and anchor parents' , ( ) => {
23
+ const app = new App ( 'test' ) ;
24
+ const anchor1 = new TestAnchor ( 'anchor-1' , app ) ;
25
+ app [ 'anchors' ] . push ( anchor1 ) ;
26
+
27
+ const overlay1 = new TestOverlay ( 'overlay-1' , { } , [ anchor1 ] ) ;
28
+
29
+ // App to Overlay dependency.
30
+ expect ( app [ 'dependencies' ] ) . toMatchInlineSnapshot ( `
31
+ [
32
+ {
33
+ "from": "app=test",
34
+ "relationship": undefined,
35
+ "to": "test-overlay=overlay-1",
36
+ },
37
+ ]
38
+ ` ) ;
39
+ expect (
40
+ app [ 'dependencies' ] [ 0 ] . hasMatchingBehavior ( 'MODEL_NAME' , DiffAction . DELETE , 'overlayId' , DiffAction . DELETE ) ,
41
+ ) . toBe ( true ) ;
42
+
43
+ // Overlay to App dependency.
44
+ expect ( overlay1 [ 'dependencies' ] ) . toMatchInlineSnapshot ( `
45
+ [
46
+ {
47
+ "from": "test-overlay=overlay-1",
48
+ "relationship": undefined,
49
+ "to": "app=test",
50
+ },
51
+ ]
52
+ ` ) ;
53
+ expect (
54
+ overlay1 [ 'dependencies' ] [ 0 ] . hasMatchingBehavior ( 'overlayId' , DiffAction . ADD , 'MODEL_NAME' , DiffAction . ADD ) ,
55
+ ) . toBe ( true ) ;
56
+ expect (
57
+ overlay1 [ 'dependencies' ] [ 0 ] . hasMatchingBehavior ( 'overlayId' , DiffAction . ADD , 'MODEL_NAME' , DiffAction . UPDATE ) ,
58
+ ) . toBe ( true ) ;
59
+ } ) ;
60
+ } ) ;
61
+
6
62
describe ( 'diff()' , ( ) => {
7
- it ( 'should produce an add diff without previous' , async ( ) => {
8
- const overlay = new TestOverlay ( 'overlay-1' , { } , [ ] ) ;
63
+ it ( 'should produce an add diff' , async ( ) => {
64
+ const overlayDataRepository = await Container . get ( OverlayDataRepository ) ;
65
+ const overlayService = await Container . get ( OverlayService ) ;
9
66
10
- const diffs = await overlay . diff ( ) ;
67
+ const overlay1 = new TestOverlay ( 'overlay-1' , { } , [ ] ) ;
68
+ overlayService . addOverlay ( overlay1 ) ;
69
+
70
+ const diffs = await overlayDataRepository . diff ( ) ;
11
71
expect ( diffs ) . toMatchInlineSnapshot ( `
12
72
[
13
73
{
@@ -19,11 +79,36 @@ describe('Overlay UT', () => {
19
79
` ) ;
20
80
} ) ;
21
81
22
- it ( 'should produce an update diff with previous' , async ( ) => {
23
- const overlay1 = new TestOverlay ( 'overlay-1' , { key1 : 'value1' , key2 : 'value2' } , [ ] ) ;
24
- const overlay2 = new TestOverlay ( 'overlay-1' , { key1 : 'value1' , key2 : 'value2.1' , key3 : 'value3' } , [ ] ) ;
82
+ it ( 'should produce a delete diff' , async ( ) => {
83
+ const overlay1 = new TestOverlay ( 'overlay-1' , { } , [ ] ) ;
84
+
85
+ const overlayDataRepository = await Container . get ( OverlayDataRepository , { args : [ true , [ ] , [ overlay1 ] ] } ) ;
86
+ const overlayService = await Container . get ( OverlayService ) ;
87
+
88
+ overlayService . removeOverlay ( overlay1 ) ;
89
+
90
+ const diffs = await overlayDataRepository . diff ( ) ;
91
+ expect ( diffs ) . toMatchInlineSnapshot ( `
92
+ [
93
+ {
94
+ "action": "delete",
95
+ "field": "overlayId",
96
+ "value": "overlay-1",
97
+ },
98
+ ]
99
+ ` ) ;
100
+ } ) ;
101
+
102
+ it ( 'should produce an update diff of flat properties' , async ( ) => {
103
+ const overlay1_0 = new TestOverlay ( 'overlay-1' , { key1 : 'value1' , key2 : 'value2' } , [ ] ) ;
104
+
105
+ const overlayDataRepository = await Container . get ( OverlayDataRepository , { args : [ true , [ ] , [ overlay1_0 ] ] } ) ;
106
+ const overlayService = await Container . get ( OverlayService ) ;
107
+
108
+ const overlay1_1 = new TestOverlay ( 'overlay-1' , { key1 : 'value1' , key2 : 'value2.1' , key3 : 'value3' } , [ ] ) ;
109
+ overlayService . addOverlay ( overlay1_1 ) ;
25
110
26
- const diffs = await overlay2 . diff ( overlay1 ) ;
111
+ const diffs = await overlayDataRepository . diff ( ) ;
27
112
expect ( diffs ) . toMatchInlineSnapshot ( `
28
113
[
29
114
{
@@ -45,63 +130,109 @@ describe('Overlay UT', () => {
45
130
]
46
131
` ) ;
47
132
} ) ;
133
+
134
+ it ( 'should produce an update diff of nested properties' , async ( ) => {
135
+ const overlay1_0 = new TestOverlay (
136
+ 'overlay-1' ,
137
+ { key1 : { 'key1.1' : 'value1.1' } , key2 : { 'key2.1' : 'value2.1' } } ,
138
+ [ ] ,
139
+ ) ;
140
+
141
+ const overlayDataRepository = await Container . get ( OverlayDataRepository , { args : [ true , [ ] , [ overlay1_0 ] ] } ) ;
142
+ const overlayService = await Container . get ( OverlayService ) ;
143
+
144
+ const overlay1_1 = new TestOverlay (
145
+ 'overlay-1' ,
146
+ { key1 : { 'key1.1' : 'value1.3' , 'key1.2' : 'value1.2' } , key2 : { 'key2.1' : 'value2.1' } } ,
147
+ [ ] ,
148
+ ) ;
149
+ overlayService . addOverlay ( overlay1_1 ) ;
150
+
151
+ const diffs = await overlayDataRepository . diff ( ) ;
152
+ expect ( diffs ) . toMatchInlineSnapshot ( `
153
+ [
154
+ {
155
+ "action": "update",
156
+ "field": "properties",
157
+ "value": {
158
+ "key": "key1",
159
+ "value": {
160
+ "key1.1": "value1.3",
161
+ "key1.2": "value1.2",
162
+ },
163
+ },
164
+ },
165
+ ]
166
+ ` ) ;
167
+ } ) ;
168
+
169
+ it ( 'should produce an update diff of array properties' , async ( ) => {
170
+ const overlay1_0 = new TestOverlay ( 'overlay-1' , { key1 : [ 'value1.1' , 'value1.2' ] } , [ ] ) ;
171
+
172
+ const overlayDataRepository = await Container . get ( OverlayDataRepository , { args : [ true , [ ] , [ overlay1_0 ] ] } ) ;
173
+ const overlayService = await Container . get ( OverlayService ) ;
174
+
175
+ const overlay1_1 = new TestOverlay ( 'overlay-1' , { key1 : [ 'value1.3' , 'value1.4' ] } , [ ] ) ;
176
+ overlayService . addOverlay ( overlay1_1 ) ;
177
+
178
+ const diffs = await overlayDataRepository . diff ( ) ;
179
+ expect ( diffs ) . toMatchInlineSnapshot ( `
180
+ [
181
+ {
182
+ "action": "update",
183
+ "field": "properties",
184
+ "value": {
185
+ "key": "key1",
186
+ "value": [
187
+ "value1.3",
188
+ "value1.4",
189
+ ],
190
+ },
191
+ },
192
+ ]
193
+ ` ) ;
194
+ } ) ;
48
195
} ) ;
49
196
50
197
describe ( 'removeAnchor()' , ( ) => {
51
- it ( 'should remove one anchor parent when multiple anchors have same parent' , ( ) => {
198
+ it ( 'should remove dependency between overlay and anchor parents' , ( ) => {
199
+ const app1 = new App ( 'test1' ) ;
200
+ const anchor1 = new TestAnchor ( 'anchor-1' , app1 ) ;
201
+ app1 [ 'anchors' ] . push ( anchor1 ) ;
202
+
203
+ const app2 = new App ( 'test2' ) ;
204
+ const anchor2 = new TestAnchor ( 'anchor-2' , app2 ) ;
205
+ app2 [ 'anchors' ] . push ( anchor2 ) ;
206
+
207
+ const overlay1 = new TestOverlay ( 'overlay-1' , { } , [ anchor1 , anchor2 ] ) ;
208
+
209
+ expect ( overlay1 [ 'dependencies' ] . find ( ( d ) => d . to . getContext ( ) === app1 . getContext ( ) ) ) . toBeTruthy ( ) ;
210
+ expect ( overlay1 [ 'dependencies' ] . find ( ( d ) => d . to . getContext ( ) === app2 . getContext ( ) ) ) . toBeTruthy ( ) ;
211
+
212
+ overlay1 . removeAnchor ( anchor1 ) ;
213
+
214
+ expect ( overlay1 [ 'dependencies' ] . find ( ( d ) => d . to . getContext ( ) === app1 . getContext ( ) ) ) . toBeFalsy ( ) ;
215
+ expect ( overlay1 [ 'dependencies' ] . find ( ( d ) => d . to . getContext ( ) === app2 . getContext ( ) ) ) . toBeTruthy ( ) ;
216
+ } ) ;
217
+
218
+ it ( 'should only remove one dependency with parent when multiple anchors have same parent' , ( ) => {
52
219
const app = new App ( 'test' ) ;
53
220
const anchor1 = new TestAnchor ( 'anchor-1' , app ) ;
221
+ app [ 'anchors' ] . push ( anchor1 ) ;
54
222
const anchor2 = new TestAnchor ( 'anchor-2' , app ) ;
223
+ app [ 'anchors' ] . push ( anchor2 ) ;
55
224
56
225
const overlay1 = new TestOverlay ( 'overlay-1' , { } , [ anchor1 , anchor2 ] ) ;
57
- expect ( app [ 'dependencies' ] ) . toMatchInlineSnapshot ( `
58
- [
59
- {
60
- "from": "app=test",
61
- "relationship": undefined,
62
- "to": "test-overlay=overlay-1",
63
- },
64
- {
65
- "from": "app=test",
66
- "relationship": undefined,
67
- "to": "test-overlay=overlay-1",
68
- },
69
- ]
70
- ` ) ;
71
- expect ( overlay1 [ 'dependencies' ] ) . toMatchInlineSnapshot ( `
72
- [
73
- {
74
- "from": "test-overlay=overlay-1",
75
- "relationship": undefined,
76
- "to": "app=test",
77
- },
78
- {
79
- "from": "test-overlay=overlay-1",
80
- "relationship": undefined,
81
- "to": "app=test",
82
- },
83
- ]
84
- ` ) ;
226
+ expect ( overlay1 . getAnchors ( ) . length ) . toBe ( 2 ) ;
227
+
228
+ expect ( app [ 'dependencies' ] . filter ( ( d ) => d . to . getContext ( ) === overlay1 . getContext ( ) ) . length ) . toBe ( 2 ) ;
229
+ expect ( overlay1 [ 'dependencies' ] . filter ( ( d ) => d . to . getContext ( ) === app . getContext ( ) ) . length ) . toBe ( 2 ) ;
85
230
86
231
overlay1 . removeAnchor ( anchor1 ) ;
87
- expect ( app [ 'dependencies' ] ) . toMatchInlineSnapshot ( `
88
- [
89
- {
90
- "from": "app=test",
91
- "relationship": undefined,
92
- "to": "test-overlay=overlay-1",
93
- },
94
- ]
95
- ` ) ;
96
- expect ( overlay1 [ 'dependencies' ] ) . toMatchInlineSnapshot ( `
97
- [
98
- {
99
- "from": "test-overlay=overlay-1",
100
- "relationship": undefined,
101
- "to": "app=test",
102
- },
103
- ]
104
- ` ) ;
232
+ expect ( overlay1 . getAnchors ( ) . length ) . toBe ( 1 ) ;
233
+
234
+ expect ( app [ 'dependencies' ] . filter ( ( d ) => d . to . getContext ( ) === overlay1 . getContext ( ) ) . length ) . toBe ( 1 ) ;
235
+ expect ( overlay1 [ 'dependencies' ] . filter ( ( d ) => d . to . getContext ( ) === app . getContext ( ) ) . length ) . toBe ( 1 ) ;
105
236
} ) ;
106
237
} ) ;
107
238
0 commit comments