This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
areconnectedthroughproperties.js
300 lines (233 loc) · 7.15 KB
/
areconnectedthroughproperties.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals window, document, Event */
import areConnectedThroughProperties from '../../src/utils/areconnectedthroughproperties';
import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
describe( 'areConnectedThroughProperties()', () => {
it( 'should return `false` if one of the value is primitive #1', () => {
const el1 = [ 'foo' ];
const el2 = 'foo';
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should return `false` if one of the value is primitive #2', () => {
const el1 = 0;
const el2 = [ 0 ];
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should return `false` if both of the values are primitives', () => {
const el1 = null;
const el2 = null;
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should return `false` if both values are plain objects', () => {
const el1 = {};
const el2 = {};
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should return `true` if both objects references to the same object', () => {
const el1 = {};
const el2 = el1;
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
} );
it( 'should return `true` if both values share a common reference #1', () => {
const foo = {};
const el1 = { foo };
const el2 = { foo };
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
} );
it( 'should return `true` if both values share a common reference #2', () => {
const foo = [];
const el1 = [ foo ];
const el2 = [ foo ];
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
} );
it( 'should return `true` if the first structure is deep inside the second structure', () => {
const el1 = {};
const el2 = {
foo: 1,
bar: [ 1, 2, 3, new Map( [
[ {}, new Set( [ 1, 2, 3 ] ) ],
[ undefined, new Set( [
Symbol( 'foo' ),
null,
{ x: [ el1 ] }
] ) ]
] ) ]
};
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
} );
it( 'should return `true` if the second structure is deep inside the first structure', () => {
const el2 = {};
const el1 = {
foo: 1,
bar: [ 1, 2, 3, new Map( [
[ {}, new Set( [ 1, 2, 3 ] ) ],
[ undefined, new Set( [
Symbol( 'foo' ),
null,
{ x: [ el2 ] }
] ) ]
] ) ]
};
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
} );
it( 'should return `true` if both structures have a common reference', () => {
const foo = {};
const el1 = {
foo: 1,
bar: [ 1, 2, 3, new Map( [
[ {}, new Set( [ 1, 2, 3 ] ) ],
[ undefined, new Set( [
Symbol( 'foo' ),
null,
{ x: [ foo ] }
] ) ]
] ) ]
};
const el2 = {
foo: 1,
bar: [ 1, 2, 3, new Map( [
[ {}, new Set( [ 1, 2, 3 ] ) ],
[ undefined, new Set( [
Symbol( 'foo' ),
null,
{ x: [ foo ] }
] ) ]
] ) ]
};
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
} );
it( 'should return `false` if the structures is not connected #1', () => {
const el1 = {};
const el2 = {
foo: 1,
bar: [ 1, 2, 3, new Map( [
[ {}, new Set( [ 1, 2, 3 ] ) ],
[ undefined, new Set( [
Symbol( 'foo' ),
null,
{ x: [] }
] ) ]
] ) ]
};
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should return `false` if the structures is not connected #2', () => {
const el1 = {
foo: 1,
bar: [ 1, 2, 3, new Map( [
[ {}, new Set( [ 1, 2, 3 ] ) ],
[ undefined, new Set( [
Symbol( 'foo' ),
null,
{ x: [] }
] ) ]
] ) ]
};
const el2 = {
foo: 1,
bar: [ 1, 2, 3, new Map( [
[ {}, new Set( [ 1, 2, 3 ] ) ],
[ undefined, new Set( [
Symbol( 'foo' ),
null,
{ x: [] }
] ) ]
] ) ]
};
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should work well with nested objects #1', () => {
const el1 = {};
el1.foo = el1;
const el2 = {};
el2.foo = el2;
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should work well with nested objects #2', () => {
const el1 = {};
el1.foo = el1;
const el2 = {};
el2.foo = {
foo: el2,
bar: el1
};
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
} );
it( 'should skip DOM objects', () => {
const evt = new Event( 'click' );
const el1 = { window, document, evt };
const el2 = { window, document, evt };
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should skip date and regexp objects', () => {
const date = new Date();
const regexp = /123/;
const el1 = { date, regexp };
const el2 = { date, regexp };
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should skip excluded properties', () => {
const shared = { foo: [] };
const el1 = { shared };
const el2 = { shared };
expect( areConnectedThroughProperties( el1, el2, new Set( [ shared ] ) ) ).to.be.false;
} );
it( 'should skip excluded properties #2', () => {
const shared = {};
const sharedNotExcluded = {};
const el1 = { shared, sharedNotExcluded };
const el2 = { shared, sharedNotExcluded };
expect( areConnectedThroughProperties( el1, el2, new Set( [ shared ] ) ) ).to.be.true;
} );
it( 'should skip the `defaultValue` key since its commonly shared between editors', () => {
const shared = {};
const el1 = { defaultValue: shared };
const el2 = { defaultValue: shared };
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.false;
} );
it( 'should skip the `defaultValue` key since its commonly shared between editors #2', () => {
const shared = {};
const el1 = { defaultValue: shared, shared };
const el2 = { defaultValue: shared, shared };
expect( areConnectedThroughProperties( el1, el2 ) ).to.be.true;
} );
describe( 'integration tests', () => {
afterEach( () => {
delete Editor.builtinPlugins;
delete Editor.defaultConfig;
} );
it( 'should return false for two different editors', () => {
const editor1 = new Editor( {} );
const editor2 = new Editor( {} );
expect( areConnectedThroughProperties( editor1, editor2 ) ).to.be.false;
} );
it( 'should return false for two different editors sharing builtin plugins', () => {
class FakePlugin {}
Editor.builtinPlugins = [ FakePlugin ];
const editor1 = new Editor();
const editor2 = new Editor();
expect( areConnectedThroughProperties( editor1, editor2 ) ).to.be.false;
} );
it( 'should return false for two different editors inheriting default configuration', () => {
Editor.defaultConfig = {
foo: {
bar: []
}
};
const editor1 = new Editor();
const editor2 = new Editor();
expect( areConnectedThroughProperties( editor1, editor2 ) ).to.be.false;
} );
it( 'should return false for two different editors sharing builtin plugins', () => {
Editor.builtinPlugins = [
class Foo {}
];
const editor1 = new Editor();
const editor2 = new Editor();
expect( areConnectedThroughProperties( editor1, editor2 ) ).to.be.false;
} );
} );
} );