forked from ezsystems/PlatformUIBundle
/
ez-relation-editview-tests.js
678 lines (584 loc) · 26.2 KB
/
ez-relation-editview-tests.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
* Copyright (C) eZ Systems AS. All rights reserved.
* For full copyright and license information view LICENSE file distributed with this source code.
*/
YUI.add('ez-relation-editview-tests', function (Y) {
var viewTest,
registerTest,
getFieldTest,
getEmptyFieldTest,
universalDiscoveryRelationTest,
Assert = Y.Assert;
viewTest = new Y.Test.Case({
name: "eZ Relation View test",
_getFieldDefinition: function (required) {
return {
isRequired: required,
fieldSettings: {},
};
},
setUp: function () {
this.destinationContent = new Y.Mock();
this.destinationContentToJSON = {
anythingJSONed: 'somethingJSONed',
resources: {
MainLocation: true
}
};
Y.Mock.expect(this.destinationContent, {
method: 'toJSON',
returns: this.destinationContentToJSON
});
Y.Mock.expect(this.destinationContent, {
method: 'get',
args: ['contentId'],
returns: 45
});
this.fieldDefinitionIdentifier= "niceField";
this.fieldDefinition = {
fieldType: "ezobjectrelation",
identifier: this.fieldDefinitionIdentifier,
isRequired: false,
fieldSettings: {},
};
this.field = {
fieldValue: {
destinationContentId: 45,
destinationContentHref: '/api/ezp/v2/content/objects/45',
}
};
this.jsonContent = {};
this.jsonContentType = {};
this.jsonVersion = {};
this.loadingError = false;
this.content = new Y.Mock();
this.version = new Y.Mock();
this.contentType = new Y.Mock();
Y.Mock.expect(this.content, {
method: 'toJSON',
returns: this.jsonContent
});
Y.Mock.expect(this.version, {
method: 'toJSON',
returns: this.jsonVersion
});
Y.Mock.expect(this.contentType, {
method: 'toJSON',
returns: this.jsonContentType
});
this.view = new Y.eZ.RelationEditView({
container: '.container',
field: this.field,
fieldDefinition: this.fieldDefinition,
content: this.content,
version: this.version,
contentType: this.contentType,
destinationContent: this.destinationContent,
translating: false,
});
this.view.set('destinationContent', this.destinationContent);
this.view.set('loadingError', this.loadingError);
},
tearDown: function () {
this.view.destroy();
},
_testAvailableVariables: function (required, expectRequired) {
var fieldDefinition = this._getFieldDefinition(required),
that = this;
this.view.set('fieldDefinition', fieldDefinition);
this.view.template = function (variables) {
Y.Assert.isObject(variables, "The template should receive some variables");
Y.Assert.areEqual(10, Y.Object.keys(variables).length, "The template should receive 10 variables");
Y.Assert.areSame(
that.jsonContent, variables.content,
"The content should be available in the field edit view template"
);
Y.Assert.areSame(
that.jsonVersion, variables.version,
"The version should be available in the field edit view template"
);
Y.Assert.areSame(
that.jsonContentType, variables.contentType,
"The contentType should be available in the field edit view template"
);
Y.Assert.areSame(
fieldDefinition, variables.fieldDefinition,
"The fieldDefinition should be available in the field edit view template"
);
Y.Assert.areSame(
that.field, variables.field,
"The field should be available in the field edit view template"
);
Y.Assert.areSame(
that.destinationContentToJSON, variables.destinationContent,
"The field should be available in the field edit view template"
);
Y.Assert.areSame(
that.view.get('loadingError'), variables.loadingError,
"The field should be available in the field edit view template"
);
Y.Assert.areSame(
!that.field.fieldValue.destinationContentId, variables.isEmpty,
"The field should be available in the field edit view template"
);
Y.Assert.isFalse(
variables.isNotTranslatable,
"The isNotTranslatable should be available in the field edit view template"
);
Y.Assert.areSame(expectRequired, variables.isRequired);
return '';
};
this.view.render();
},
"Test not required field": function () {
this._testAvailableVariables(false, false);
},
"Test required field": function () {
this._testAvailableVariables(true, true);
},
"Test validate no constraints": function () {
var fieldDefinition = this._getFieldDefinition(false);
this.view.set('fieldDefinition', fieldDefinition);
this.view.set('destinationContent', null);
this.view.render();
this.view.validate();
Y.Assert.isTrue(
this.view.isValid(),
"An empty relation is valid"
);
},
"Test validate required": function () {
var fieldDefinition = this._getFieldDefinition(true);
this.view.set('fieldDefinition', fieldDefinition);
this.view._set('destinationContent', null);
this.view.validate();
this.view.render();
Y.Assert.isFalse(
this.view.isValid(),
"An empty relation is NOT valid"
);
},
"Should fill the destinationcontentId attribute from the field": function () {
Y.Assert.areSame(
this.view.get('field').fieldValue.destinationContentId,
this.view.get('destinationContentId'),
"The destinationContentId of the field value should be the same than the attribute"
);
},
"Should fire the loadFieldRelatedContent event": function () {
var loadContentEvent = false;
this.view.on('loadFieldRelatedContent', Y.bind(function (e) {
Y.Assert.areSame(
this.field.fieldValue.destinationContentHref,
e.destinationContentId,
"destinationContentId is the same than the one in the field"
);
loadContentEvent = true;
}, this));
this.view.set('active', true);
Y.Assert.isTrue(loadContentEvent, "loadContentEvent should be called when changing active value");
},
"Should NOT fire the loadFieldRelatedContent event if field is empty": function () {
var loadContentEvent = false,
that = this;
this.view.on('loadFieldRelatedContent', function (e) {
Y.Assert.areSame(
that.fieldDefinitionIdentifier,
e.fieldDefinitionIdentifier,
"fieldDefinitionIdentifier is the same than the one in the field"
);
loadContentEvent = true;
});
this.view._set('destinationContentId', null);
this.view.set('active', true);
Y.Assert.isFalse(loadContentEvent, "loadContentEvent should NOT be called when changing active value");
},
"Should run the UniversalDiscoveryWidget": function () {
var that = this;
this.view.on('contentDiscover', function (e) {
that.resume(function () {
Y.Assert.isObject(e.config, "contentDiscover config should be an object");
Y.Assert.isFunction(e.config.contentDiscoveredHandler, "config should have a function named contentDiscoveredHandler");
Y.Assert.isFunction(e.config.cancelDiscoverHandler, "config should have a function named cancelDiscoverHandler");
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should run the UniversalDiscoveryWidget starting at selectionRoot": function () {
var locationId = 'whatever/location/id';
this.fieldDefinition.fieldSettings.selectionRootHref = locationId;
this.view.on('contentDiscover', this.next(function (e) {
Assert.areEqual(
locationId, e.config.startingLocationId,
"The startLocationId parameter should be set"
);
}, this));
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should prevent default behaviour of the tap event for select button": function () {
var that = this;
this.view.render();
this.view.get('container').once('tap', function (e) {
that.resume(function () {
Y.Assert.isTrue(
!!e.prevented,
"The tap event should have been prevented"
);
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should prevent default behaviour of the tap event for remove button": function () {
var that = this;
this.view.render();
this.view.get('container').once('tap', function (e) {
that.resume(function () {
Y.Assert.isTrue(
!!e.prevented,
"The tap event should have been prevented"
);
});
});
this.view.get('container').one('.ez-relation-remove').simulateGesture('tap');
this.wait();
},
"Should remove relation when tap on remove button": function () {
var that = this;
this.view.render();
this.view.get('container').once('tap', function (e) {
that.resume(function () {
Y.Assert.isNull(
this.view.get('destinationContentId'),
"destinationContentId should be null"
);
Y.Assert.isNull(
this.view.get('destinationContent'),
"destinationContent should be null"
);
Y.Assert.isTrue(
this.view.get('container').one('.ez-relation-remove').get('disabled'),
'Remove button should be disabled'
);
});
});
this.view.get('container').one('.ez-relation-remove').simulateGesture('tap');
this.wait();
},
"Should fill the relation with the universal discovery widget selection": function () {
var that = this,
contentInfoMock = new Y.Mock(),
fakeEventFacade = {selection: {contentInfo: contentInfoMock}};
Y.Mock.expect(contentInfoMock, {
property: 'name',
value: 'contentInfoModel'
});
Y.Mock.expect(contentInfoMock, {
method: 'toJSON',
returns: {
name: 'me',
publishedDate: 'yesterday',
lastModificationDate: 'tomorrow',
resources: {}
}
});
Y.Mock.expect(contentInfoMock, {
method: 'get',
args: ['contentId'],
returns: 51
});
this.view.on('contentDiscover', function (e) {
that.resume(function () {
e.config.contentDiscoveredHandler.call(this, fakeEventFacade);
Y.Assert.areSame(
contentInfoMock.get('contentId'),
this.view.get('destinationContentId'),
'destinationContentId should match the contentId of the selected relation'
);
Y.Assert.areSame(
contentInfoMock,
this.view.get('destinationContent'),
'destinationContent should match the content of the selected relation'
);
Y.Assert.isFalse(
this.view.get('container').one('.ez-relation-remove').get(('disabled')),
'Remove button should be disabled'
);
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should validate when the universal discovery is canceled (empty relation)": function () {
var that = this,
fieldDefinition = this._getFieldDefinition(true);
this.view.set('fieldDefinition', fieldDefinition);
this.view._set('destinationContentId', null);
this.view.on('contentDiscover', function (e) {
that.resume(function () {
e.config.cancelDiscoverHandler.call(this);
Y.Assert.areSame(this.view.get('errorStatus'),
'this.field.is.required domain=fieldedit',
'errorStatus should be truthy'
);
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should validate when the universal discovery is canceled": function () {
var that = this,
fieldDefinition = this._getFieldDefinition(false);
this.view.set('fieldDefinition', fieldDefinition);
this.view._set('destinationContentId', 51);
this.view.on('contentDiscover', function (e) {
that.resume(function () {
e.config.cancelDiscoverHandler.call(this);
Y.Assert.isFalse(this.view.get('errorStatus'),'errorStatus should be false');
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should render the view when the destinationContent attribute changes": function () {
var templateCalled = false,
origTpl = this.view.template;
this.view.template = function (variables) {
templateCalled = true;
return origTpl.apply(this, arguments);
};
this.view.set('destinationContent', this.destinationContent);
Y.Assert.isTrue(templateCalled, "The template has not been used");
},
"Should render the view when the loadingError attribute changes": function () {
var templateCalled = false,
origTpl = this.view.template;
this.view.template = function (variables) {
templateCalled = true;
return origTpl.apply(this, arguments);
};
this.view.set('loadingError', true);
Y.Assert.isTrue(templateCalled, "The template has not been used");
},
});
Y.Test.Runner.setName("eZ Relation Edit View tests");
Y.Test.Runner.add(viewTest);
getFieldTest = new Y.Test.Case(
Y.merge(Y.eZ.Test.GetFieldTests, {
fieldDefinition: {isRequired: false},
ViewConstructor: Y.eZ.RelationEditView,
value: {destinationContentId: 45},
newValue: 45,
_setNewValue: function () {
this.view._set("destinationContentId", this.newValue);
},
_assertCorrectFieldValue: function (fieldValue, msg) {
Y.Assert.isObject(fieldValue, 'fieldValue should be an object');
Y.Assert.areEqual(this.newValue, fieldValue.destinationContentId, msg);
},
})
);
Y.Test.Runner.add(getFieldTest);
getEmptyFieldTest = new Y.Test.Case(
Y.merge(Y.eZ.Test.GetFieldTests, {
fieldDefinition: {isRequired: false},
ViewConstructor: Y.eZ.RelationEditView,
value: {destinationContentId: null},
newValue: null,
_setNewValue: function () {
this.view._set("destinationContentId", this.newValue);
},
_assertCorrectFieldValue: function (fieldValue, msg) {
Y.Assert.isObject(fieldValue, 'fieldValue should be an object');
Y.Assert.areEqual(this.newValue, fieldValue.destinationContentId, msg);
},
})
);
Y.Test.Runner.add(getEmptyFieldTest);
universalDiscoveryRelationTest = new Y.Test.Case({
name: "eZ Relation universal discovery relation test",
_getFieldDefinition: function (required) {
return {
isRequired: required,
fieldSettings: {},
};
},
setUp: function () {
this.relatedContents = [];
this.fieldDefinitionIdentifier= "relationField";
this.fieldDefinition = {
fieldType: "ezobjectrelation",
identifier: this.fieldDefinitionIdentifier,
isRequired: false,
fieldSettings: {
selectionContentTypes: ['allowed_content_type_identifier']
}
};
this.field = {fieldValue: {destinationContentId: 45}};
this.jsonContent = {};
this.jsonContentType = {};
this.jsonVersion = {};
this.content = new Y.Mock();
this.version = new Y.Mock();
this.contentType = new Y.Mock();
Y.Mock.expect(this.content, {
method: 'toJSON',
returns: this.jsonContent
});
Y.Mock.expect(this.version, {
method: 'toJSON',
returns: this.jsonVersion
});
Y.Mock.expect(this.contentType, {
method: 'toJSON',
returns: this.jsonContentType
});
this.view = new Y.eZ.RelationEditView({
container: '.container',
field: this.field,
fieldDefinition: this.fieldDefinition,
content: this.content,
version: this.version,
contentType: this.contentType,
relatedContents: this.relatedContents,
});
},
tearDown: function () {
this.view.destroy();
delete this.view;
},
"Should validate when the universal discovery is canceled (empty relation)": function () {
var that = this,
fieldDefinition = this._getFieldDefinition(true);
this.view.set('fieldDefinition', fieldDefinition);
this.view._set('destinationContentId', null);
this.view.on('contentDiscover', function (e) {
that.resume(function () {
e.config.cancelDiscoverHandler.call(this);
Y.Assert.areSame(
this.view.get('errorStatus'),
'this.field.is.required domain=fieldedit',
'errorStatus should be true'
);
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should validate when the universal discovery is canceled": function () {
var that = this,
fieldDefinition = this._getFieldDefinition(false);
this.view.set('fieldDefinition', fieldDefinition);
this.view.on('contentDiscover', function (e) {
that.resume(function () {
e.config.cancelDiscoverHandler.call(this);
Y.Assert.isFalse(this.view.get('errorStatus'),'errorStatus should be false');
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should fill the relation with the universal discovery widget selection": function () {
var that = this,
contentInfoMock1 = new Y.Mock(),
contentInfoMock2 = new Y.Mock(),
fakeEventFacade = {selection: [{contentInfo: contentInfoMock1}, {contentInfo: contentInfoMock2}]},
contentId;
this.view._set('destinationContentId', 51);
Y.Mock.expect(contentInfoMock1, {
method: 'toJSON',
returns: {name: 'me', publishedDate: 'yesterday', lastModificationDate: 'tomorrow'}
});
Y.Mock.expect(contentInfoMock1, {
method: 'get',
args: ['contentId'],
returns: 42
});
Y.Mock.expect(contentInfoMock2, {
method: 'toJSON',
returns: {name: 'me', publishedDate: 'yesterday', lastModificationDate: 'tomorrow'}
});
Y.Mock.expect(contentInfoMock2, {
method: 'get',
args: ['contentId'],
returns: 51
});
this.view.on('contentDiscover', function (e) {
that.resume(function () {
Y.Array.each(fakeEventFacade.selection, function (selection) {
if ( that.view.get('destinationContentId') === selection.contentInfo.get('contentId')) {
contentId = selection.contentInfo.get('contentId');
}
});
e.config.contentDiscoveredHandler.call(this, fakeEventFacade);
Y.ArrayAssert.itemsAreEqual(
contentId,
51,
'destinationContentId should match the contentId of the selected relation'
);
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should run the UniversalDiscoveryWidget": function () {
var that = this,
allowedContentType = new Y.Mock(),
notAllowedContentType = new Y.Mock();
Y.Mock.expect(allowedContentType, {
method: 'get',
args: ['identifier'],
returns: this.fieldDefinition.fieldSettings.selectionContentTypes[0]
});
Y.Mock.expect(notAllowedContentType, {
method: 'get',
args: ['identifier'],
returns: 'not_allowed_content_type_identifier'
});
this.view.on('contentDiscover', function (e) {
that.resume(function () {
Y.Assert.isObject(e.config, "contentDiscover config should be an object");
Y.Assert.isFunction(e.config.contentDiscoveredHandler, "config should have a function named contentDiscoveredHandler");
Y.Assert.isFunction(e.config.cancelDiscoverHandler, "config should have a function named cancelDiscoverHandler");
Y.Assert.isFunction(e.config.isSelectable, "config should have a function named isSelectable");
Y.Assert.isTrue(
e.config.isSelectable({contentType: allowedContentType}),
"isSelectable should return TRUE if selected content's content type is on allowed content types list"
);
Y.Assert.isFalse(
e.config.isSelectable({contentType: notAllowedContentType}),
"isSelectable should return FALSE if selected content's content type is not on allowed content types list"
);
Assert.isUndefined(
e.config.startingLocationId,
"The starting Location id parameter should not be set"
);
});
});
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
"Should run the UniversalDiscoveryWidget starting at selectionRoot": function () {
var locationId = 'whatever/location/id';
this.fieldDefinition.fieldSettings.selectionContentTypes = [];
this.fieldDefinition.fieldSettings.selectionRootHref = locationId;
this.view.on('contentDiscover', this.next(function (e) {
Assert.areEqual(
locationId,
e.config.startingLocationId,
"The starting Location id parameter should be set"
);
}, this));
this.view.get('container').one('.ez-relation-discover').simulateGesture('tap');
this.wait();
},
});
Y.Test.Runner.add(universalDiscoveryRelationTest);
registerTest = new Y.Test.Case(Y.eZ.EditViewRegisterTest);
registerTest.name = "Relation Edit View registration test";
registerTest.viewType = Y.eZ.RelationEditView;
registerTest.viewKey = "ezobjectrelation";
Y.Test.Runner.add(registerTest);
}, '', {requires: ['test', 'getfield-tests', 'node-event-simulate', 'editviewregister-tests', 'ez-relation-editview']});