This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
form_test.js
executable file
·838 lines (677 loc) · 27.4 KB
/
form_test.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
'use strict';
/* global contacts */
/* global ConfirmDialog */
/* global LazyLoader */
/* global MockContactAllFields */
/* global MockContactsSearch */
/* global MockExtServices */
/* global Mockfb */
/* global MockFormDom */
/* global MocksHelper */
/* global MockMozContactsObj */
/* global MockThumbnailImage */
/* global utils */
require('/shared/test/unit/mocks/mock_contact_all_fields.js');
require('/shared/js/text_normalizer.js');
require('/shared/js/lazy_loader.js');
require('/shared/js/contacts/import/utilities/misc.js');
require('/shared/js/contacts/utilities/dom.js');
require('/shared/js/contacts/utilities/templates.js');
require('/shared/js/contacts/utilities/event_listeners.js');
//Avoiding lint checking the DOM file renaming it to .html
requireApp('communications/contacts/test/unit/mock_form_dom.js.html');
requireApp('communications/contacts/js/contacts_tag.js');
requireApp('communications/contacts/js/views/form.js');
requireApp('communications/contacts/test/unit/mock_navigation.js');
requireApp('communications/contacts/test/unit/mock_contacts.js');
requireApp('communications/contacts/test/unit/mock_mozContacts.js');
requireApp('communications/contacts/test/unit/mock_external_services.js');
requireApp('communications/contacts/test/unit/mock_fb.js');
requireApp('communications/contacts/test/unit/mock_contacts_search.js');
requireApp('communications/contacts/test/unit/mock_confirm_dialog.js');
requireApp('communications/contacts/test/unit/mock_image_thumbnail.js');
require('/shared/test/unit/mocks/mock_contact_photo_helper.js');
var subject,
realL10n,
Contacts,
realFb,
realThumbnailImage,
mockContact,
footer,
ActivityHandler;
var mocksForm = new MocksHelper([
'Contacts',
'ConfirmDialog',
'ContactPhotoHelper'
]).init();
suite('Render contact form', function() {
suiteSetup(function() {
realL10n = navigator.mozL10n;
navigator.mozL10n = {
get: function get(key) {
return key;
},
DateTimeFormat: function() {
this.localeFormat = function(date, format) {
return date;
};
}
};
mocksForm.suiteSetup();
Contacts.extServices = MockExtServices;
realFb = window.fb;
window.fb = Mockfb;
realThumbnailImage = utils.thumbnailImage;
utils.thumbnailImage = MockThumbnailImage;
document.body.innerHTML = MockFormDom;
footer = document.querySelector('footer');
subject = contacts.Form;
ActivityHandler = {
currentlyHandling: false
};
subject.init(Contacts.getTags());
});
suiteTeardown(function() {
window.fb = realFb;
utils.thumbnailImage = realThumbnailImage;
navigator.mozL10n = realL10n;
mocksForm.suiteTeardown();
document.body.innerHTML = '';
});
setup(function() {
mockContact = new MockContactAllFields();
});
teardown(function() {
window.fb.setIsFbContact(false);
window.fb.setIsFbLinked(false);
});
function assertSaveState(value) {
var element = document.body.querySelector('#save-button');
assert.equal(element.getAttribute('disabled'), value);
}
function assertCarrierState(ele, value) {
var carrierField = ele.querySelector('input[data-field="carrier"]');
var state = carrierField.getAttribute('disabled');
assert.isTrue(value === state);
}
function assertAddDateState(value) {
assert.equal(document.getElementById('add-new-date').disabled, value);
}
suite('Render add form', function() {
test('without params', function() {
subject.render();
var toCheck = ['phone', 'address', 'email', 'note', 'date'];
for (var i = 0; i < toCheck.length; i++) {
var element = 'add-' + toCheck[i];
var cont = document.body.innerHTML;
assert.isTrue(cont.indexOf(element + '-0') > -1);
assertEmpty(element + '-0');
assert.isTrue(cont.indexOf(element + '-1') == -1);
assert.isTrue(footer.classList.contains('hide'));
}
assertSaveState('disabled');
// The add date button shouldn't be disabled
assertAddDateState(false);
});
test('with tel params', function() {
var params = {
tel: '123'
};
subject.render(params);
var toCheck = ['phone', 'address', 'email', 'note'];
var cont = document.body.innerHTML;
for (var i = 0; i < toCheck.length; i++) {
var element = 'add-' + toCheck[i];
assert.isTrue(cont.indexOf(element + '-0') > -1);
assert.isTrue(cont.indexOf(element + '-1') == -1);
}
var value = document.querySelector('#number_0').value;
assert.equal(value, params.tel);
var valueEmail = document.querySelector('#email_0').value;
assert.isFalse(valueEmail === params.tel);
assert.equal(valueEmail, '');
assert.isTrue(footer.classList.contains('hide'));
assertSaveState(null);
});
test('with email params', function() {
var params = {
email: '123'
};
subject.render(params);
var toCheck = ['phone', 'address', 'email', 'note'];
var cont = document.body.innerHTML;
for (var i = 0; i < toCheck.length; i++) {
var element = 'add-' + toCheck[i];
assert.isTrue(cont.indexOf(element + '-0') > -1);
assert.isTrue(cont.indexOf(element + '-1') == -1);
}
var value = document.querySelector('#number_0').value;
var valueEmail = document.querySelector('#email_0').value;
assert.isTrue(valueEmail === params.email);
assert.equal(value, '');
assert.isTrue(footer.classList.contains('hide'));
assertSaveState(null);
});
test('with email and tel params', function() {
var params = {
tel: '234',
email: '123'
};
subject.render(params);
var toCheck = ['phone', 'address', 'email', 'note'];
var cont = document.body.innerHTML;
for (var i = 0; i < toCheck.length; i++) {
var element = 'add-' + toCheck[i];
assert.isTrue(cont.indexOf(element + '-0') > -1);
assert.isTrue(cont.indexOf(element + '-1') == -1);
}
var value = document.querySelector('#number_0').value;
assert.isTrue(value === params.tel);
var valueEmail = document.querySelector('#email_0').value;
assert.isTrue(valueEmail === params.email);
assert.isTrue(footer.classList.contains('hide'));
assertSaveState(null);
});
test('Initially the carrier field must be in disabled state', function() {
subject.render();
var element = document.body.querySelector('#add-phone-0');
assertCarrierState(element, 'disabled');
});
test('If email is a blank string then done button must be disabled',
function() {
var params = {
email: ' '
};
subject.render(params);
assertSaveState('disabled');
}
);
test('If tel is filled and carrier empty done button must be enabled',
function() {
var params = {
tel: [{
carrier: '',
value: '123456'
}]
};
subject.render(params);
assertSaveState(null);
}
);
test('Empty form and label changes keeps the done button disabled',
function() {
subject.render();
var formView = document.getElementById('view-contact-form');
// Sending this custom event is equivalent to changing the select value
// at UI level (see contacts.js#handleSelectTagDone)
var valueModifiedEvent = new CustomEvent('ValueModified', {
bubbles: true,
detail: {
prevValue: 'work',
newValue: 'home'
}
});
formView.dispatchEvent(valueModifiedEvent);
assertSaveState('disabled');
});
test('Date tags are filtered properly', function() {
subject.render();
document.getElementById('add-new-date').click();
var date1 = document.getElementById('date_type_0');
assert.equal(date1.dataset.value, 'birthday');
var date2 = document.getElementById('date_type_1');
assert.equal(date2.dataset.value, 'anniversary');
});
});
suite('Render edit form', function() {
function assertDateContent(selector, date) {
var inputDate = document.body.querySelector(selector).
querySelector('input[type="date"]');
var contentDate = inputDate.valueAsDate;
assert.equal(contentDate.toUTCString(), mockContact.bday.toUTCString());
assert.isTrue(inputDate.previousElementSibling.
textContent.trim().length > 0);
assert.isFalse(inputDate.previousElementSibling.
classList.contains('placeholder'));
}
test('no scroll on first load', function() {
subject.render(mockContact);
var container = document.getElementById('contact-form').parentNode;
assert.equal(container.scrollTop, 0);
});
test('no scroll memorised from previous renders', function() {
subject.render(mockContact);
var container = document.getElementById('contact-form').parentNode;
// scroll container
container.scrollTop = 100;
subject.render(mockContact);
assert.equal(container.scrollTop, 0);
});
test('with no name', function() {
mockContact.givenName.pop();
subject.render(mockContact);
var nameField = document.querySelector('#givenName');
assert.equal(nameField.value, '');
});
test('with no last name', function() {
mockContact.familyName.pop();
subject.render(mockContact);
var nameField = document.querySelector('#familyName');
assert.equal(nameField.value, '');
});
test('with all fields', function() {
// For this test we need a contact with the same number of items
// on the used fields (phone, address, email, note)
mockContact.tel.pop();
mockContact.email.pop();
subject.render(mockContact);
var cont = document.body.innerHTML;
var toCheck = ['phone', 'address', 'email', 'note', 'date'];
for (var i = 0; i < toCheck.length; i++) {
var element = 'add-' + toCheck[i];
assert.isTrue(cont.indexOf(element + '-0') > -1);
assert.isTrue(cont.indexOf(element + '-1') == -1);
}
assertPhoneData(0);
assertEmailData(0);
assert.isFalse(footer.classList.contains('hide'));
// Remove Field icon on photo is present
var thumbnail = document.querySelector('#thumbnail-action');
assert.isTrue(thumbnail.querySelector('.icon-delete') !== null);
});
test('with only birthday', function() {
subject.render(mockContact);
var cont = document.body.innerHTML;
var element = 'add-date';
assert.isTrue(cont.indexOf(element + '-0') > -1);
assert.isTrue(cont.indexOf(element + '-1') === -1);
assertDateContent('#' + element + '-0', mockContact.bday);
// The add date button shouldn't be disabled
assertAddDateState(false);
});
test('with birthday and anniversary', function() {
mockContact.anniversary = new Date(0);
subject.render(mockContact);
var cont = document.body.innerHTML;
var element = 'add-date';
assert.isTrue(cont.indexOf(element + '-0') > -1);
assert.isTrue(cont.indexOf(element + '-1') > -1);
assert.isTrue(cont.indexOf(element + '-2') === -1);
assertDateContent('#' + element + '-0', mockContact.bday);
assertDateContent('#' + element + '-1', mockContact.anniversary);
// The add date button should be disabled
assertAddDateState(true);
});
test('Birthday first day of the year is rendered properly', function() {
mockContact.bday = new Date(Date.UTC(2014, 0, 1));
subject.render(mockContact);
var element = 'add-date';
assertDateContent('#' + element + '-0', mockContact.bday);
});
test('Dates are saved preserving their timestasmp referred to UTC',
function() {
var deviceContact = new MockContactAllFields();
subject.render(deviceContact);
subject.saveContact();
assert.equal(deviceContact.bday.getTime(), 0);
});
test('if the tel field is null, is ignored',
function() {
var deviceContact = new MockContactAllFields();
deviceContact.tel[0].value = null;
subject.render(deviceContact);
assert.equal(deviceContact.tel.length, 2);
subject.saveContact();
assert.equal(deviceContact.tel.length, 1);
});
test('if the email field is null, is ignored',
function() {
var deviceContact = new MockContactAllFields();
deviceContact.email[0].value = null;
subject.render(deviceContact);
assert.equal(deviceContact.email.length, 2);
subject.saveContact();
assert.equal(deviceContact.email.length, 1);
});
test('if the address field is null, is ignored',
function() {
var deviceContact = new MockContactAllFields();
deviceContact.adr.unshift({'type': ['personal']});
subject.render(deviceContact);
assert.equal(deviceContact.adr.length, 2);
subject.saveContact();
assert.equal(deviceContact.adr.length, 1);
});
test('if tel field has a value, carrier input must be in regular state',
function() {
subject.render(mockContact);
var element = document.body.querySelector('#add-phone-0');
assertCarrierState(element, null);
});
test('if tel field has no value, carrier input must be in disabled state',
function() {
mockContact.tel = [];
subject.render(mockContact);
var element = document.body.querySelector('#add-phone-0');
assertCarrierState(element, 'disabled');
});
test('FB Contact. e-mail, phone and photo from Facebook', function() {
window.fb.setIsFbContact(true);
var deviceContact = new MockContactAllFields();
var fbContact = new Mockfb.Contact(deviceContact);
fbContact.getDataAndValues().onsuccess = function() {
deviceContact.photo = null;
subject.render(deviceContact, null, this.result);
var cont = document.body.innerHTML;
var toCheck = ['phone', 'email'];
for (var i = 0; i < toCheck.length; i++) {
var element = 'add-' + toCheck[i];
assert.isTrue(cont.indexOf(element + '-0') > -1);
assert.isTrue(cont.indexOf(element + '-1') > -1);
assert.isTrue(cont.indexOf(element + '-2') == -1);
var domElement0 = document.querySelector('#' + element + '-' + '0');
assert.isTrue(domElement0.classList.contains('removed') &&
domElement0.classList.contains('facebook'));
assert.isTrue(domElement0.querySelector('.icon-delete') === null);
}
assertPhoneData(0);
assertEmailData(0);
assert.isFalse(footer.classList.contains('hide'));
// Remove Field icon photo should not be present
var thumbnail = document.querySelector('#thumbnail-action');
assert.isTrue(thumbnail.querySelector('.icon-delete').
parentNode.classList.contains('hide'));
assert.isTrue(thumbnail.classList.contains('facebook'));
assert.isTrue(thumbnail.classList.contains('removed'));
};
});
test('FB Contact. Address from Facebook', function() {
window.fb.setIsFbContact(true);
var fbContact = new Mockfb.Contact(mockContact);
fbContact.getDataAndValues().onsuccess = function() {
subject.render(mockContact, null, this.result);
var content = document.body.innerHTML;
var toCheck = ['address'];
for (var i = 0; i < toCheck.length; i++) {
var element = 'add-' + toCheck[i];
assert.isTrue(content.indexOf(element + '-0') > -1);
assert.isTrue(content.indexOf(element + '-1') > -1);
assert.isTrue(content.indexOf(element + '-2') === -1);
var domElement0 = document.querySelector('#' + element + '-' + '0');
assert.isTrue(domElement0.classList.contains('removed') &&
domElement0.classList.contains('facebook'),
'Class Removed and Facebook present');
assert.isTrue(domElement0.querySelector('.icon-delete') === null,
'Icon delete not present');
}
assertAddressData(0, this.result[0]);
assert.isFalse(footer.classList.contains('hide'));
};
});
test('FB Contact. organization from Facebook', function() {
window.fb.setIsFbContact(true);
var deviceContact = new MockContactAllFields();
var fbContact = new Mockfb.Contact(deviceContact);
fbContact.getDataAndValues().onsuccess = function() {
subject.render(deviceContact, null, this.result);
var org = document.querySelector('input[name="org"]');
assert.isTrue(org.parentNode.classList.contains('facebook'));
assert.isTrue(org.parentNode.classList.contains('removed'));
};
});
test('FB Contact. Birthday from Facebook', function() {
window.fb.setIsFbContact(true);
var fbContact = new Mockfb.Contact(mockContact);
fbContact.getDataAndValues().onsuccess = function() {
subject.render(mockContact, null, this.result);
var content = document.body.innerHTML;
var element = 'add-date';
assert.isTrue(content.indexOf(element + '-0') > -1);
assert.isTrue(content.indexOf(element + '-1') === -1);
assertDateContent('#' + element + '-0', mockContact.bday);
var domElement0 = document.querySelector('#' + element + '-' + '0');
assert.isTrue(domElement0.classList.contains('removed') &&
domElement0.classList.contains('facebook'),
'Class Removed and Facebook present');
assert.isTrue(domElement0.querySelector('.icon-delete') === null,
'Icon delete not present');
// The add date button shouldn't be disabled
assertAddDateState(false);
assert.isFalse(footer.classList.contains('hide'));
};
});
test('FB Contact. Linking and promoting given name', function() {
window.fb.setIsFbContact(true);
window.fb.setIsFbLinked(false);
var promoteToLinkedSpy = sinon.spy(Mockfb, 'promoteToLinked');
var setPropagatedFlagSpy = sinon.spy(window.fb, 'setPropagatedFlag');
mockContact.givenName.pop();
var fbContact = new Mockfb.Contact(mockContact);
fbContact.getDataAndValues().onsuccess = function() {
subject.render(mockContact, null, this.result);
document.querySelector('#givenName').value = '';
subject.saveContact();
assert.isTrue(promoteToLinkedSpy.called);
assert.isTrue(setPropagatedFlagSpy.calledWithMatch('givenName'));
promoteToLinkedSpy.restore();
setPropagatedFlagSpy.restore();
};
});
test('FB Contact. Linking and promoting family name', function() {
window.fb.setIsFbContact(true);
window.fb.setIsFbLinked(false);
var promoteToLinkedSpy = sinon.spy(Mockfb, 'promoteToLinked');
var setPropagatedFlagSpy = sinon.spy(window.fb, 'setPropagatedFlag');
mockContact.familyName.pop();
var fbContact = new Mockfb.Contact(mockContact);
fbContact.getDataAndValues().onsuccess = function() {
subject.render(mockContact, null, this.result);
document.querySelector('#familyName').value = '';
subject.saveContact();
assert.isTrue(promoteToLinkedSpy.called);
assert.isTrue(setPropagatedFlagSpy.calledWithMatch('familyName'));
promoteToLinkedSpy.restore();
setPropagatedFlagSpy.restore();
};
});
test('FB Linked. e-mail and phone both from FB and device', function() {
window.fb.setIsFbContact(true);
window.fb.setIsFbLinked(true);
var fbContact = new Mockfb.Contact(mockContact);
fbContact.getDataAndValues().onsuccess = function() {
this.result[0].tel[1] = {
'value': '+34616885989',
'type': ['Mobile'],
'carrier': 'NTT'
};
this.result[0].email[1] = {
'type': ['work'],
'value': 'workwithme@tid.es'
};
subject.render(mockContact, null, this.result);
var cont = document.body.innerHTML;
var toCheck = ['phone', 'email'];
for (var i = 0; i < toCheck.length; i++) {
var element = 'add-' + toCheck[i];
assert.isTrue(cont.indexOf(element + '-0') > -1);
assert.isTrue(cont.indexOf(element + '-1') > -1);
var domElement0 = document.querySelector('#' + element + '-' + '0');
assert.isTrue(domElement0.classList.contains('removed') &&
domElement0.classList.contains('facebook'));
assert.isTrue(domElement0.querySelector('.icon-delete') === null);
var domElement1 = document.querySelector('#' + element + '-' + '1');
assert.isFalse(domElement1.classList.contains('removed') ||
domElement1.classList.contains('facebook'));
assert.isTrue(domElement1.querySelector('.icon-delete') !== null);
}
for (var c = 0; c < 2; c++) {
assertPhoneData(c, this.result[0]);
assertEmailData(c, this.result[0]);
}
};
});
test('FB Linked. Photo local to the device', function() {
window.fb.setIsFbContact(true);
window.fb.setIsFbLinked(true);
var fbContact = new Mockfb.Contact(mockContact);
fbContact.getDataAndValues().onsuccess = function() {
subject.render(mockContact, null, this.result);
var thumbnail = document.querySelector('#thumbnail-action');
assert.isFalse(thumbnail.querySelector('.icon-delete').
parentNode.classList.contains('hide'));
assert.isFalse(thumbnail.classList.contains('facebook'));
assert.isFalse(thumbnail.classList.contains('removed'));
};
});
test('FB Linked. Birthday local to the device', function() {
window.fb.setIsFbContact(true);
window.fb.setIsFbLinked(true);
var fbContact = new Mockfb.Contact(mockContact);
fbContact.getDataAndValues().onsuccess = function() {
delete this.result[1][new Date(0).toString()];
subject.render(mockContact, null, this.result);
assertDateContent('#add-date-0', mockContact.bday);
var domElement0 = document.querySelector('#add-date-0');
assert.isFalse(domElement0.classList.contains('removed') ||
domElement0.classList.contains('facebook'),
'Class Removed or Facebook present');
assert.isFalse(domElement0.querySelector('.icon-delete') === null,
'Icon delete not present');
// The add date button shouldn't be disabled
assertAddDateState(false);
};
});
});
suite('Generate full contact name', function() {
setup(function() {
// Bypass the contacts matcher when saving contact
LazyLoader.load(['/shared/js/simple_phone_matcher.js',
'/shared/js/contacts/contacts_matcher.js'], function() {
contacts.Matcher.match = function() {};
});
});
test('given name is empty', function() {
var deviceContact = new MockContactAllFields();
deviceContact.givenName = null;
deviceContact.name = null;
subject.render(deviceContact);
subject.saveContact();
assert.equal(deviceContact.name[0], deviceContact.familyName);
});
test('family name is empty', function() {
var deviceContact = new MockContactAllFields();
deviceContact.familyName = null;
deviceContact.name = null;
subject.render(deviceContact);
subject.saveContact();
assert.equal(deviceContact.name[0], deviceContact.givenName);
});
test('both name fields are empty', function() {
var deviceContact = new MockContactAllFields();
deviceContact.givenName = null;
deviceContact.familyName = null;
deviceContact.name = null;
subject.render(deviceContact);
subject.saveContact();
assert.equal(deviceContact.name[0], undefined);
});
test('both name fields are present', function() {
var deviceContact = new MockContactAllFields();
deviceContact.name = null;
subject.render(deviceContact);
subject.saveContact();
assert.equal(deviceContact.name[0],
deviceContact.givenName + ' ' + deviceContact.familyName);
});
});
suite('Delete Contact', function() {
var deleteButton;
var realSearch;
var realMozContacts;
setup(function() {
subject.render(mockContact);
});
suiteSetup(function() {
deleteButton = document.querySelector('#delete-contact');
realSearch = contacts.Search;
contacts.Search = MockContactsSearch;
realMozContacts = navigator.mozContacts;
navigator.mozContacts = new MockMozContactsObj([]);
});
suiteTeardown(function() {
contacts.Search = realSearch;
navigator.mozContacts = realMozContacts;
});
test('show confirm', function() {
deleteButton.click();
assert.isTrue(ConfirmDialog.showing);
assert.equal(ConfirmDialog.text, 'deleteConfirmMsg');
ConfirmDialog.hide();
});
test('cancel delete', function() {
deleteButton.click();
ConfirmDialog.executeNo();
assert.isFalse(ConfirmDialog.showing);
});
test('delete contact while in search mode', function(done) {
deleteButton.click();
sinon.stub(contacts.Search,
'isInSearchMode', function() {
return true;
});
var exitSearchModeStub = sinon.stub(contacts.Search,
'exitSearchMode', function() {
assert.isTrue(true);
contactsStub.restore();
exitSearchModeStub.restore();
done();
});
var contactsStub = sinon.stub(window.navigator.mozContacts,
'remove', function() {
return {
set onsuccess(cb) {
cb();
}
};
});
ConfirmDialog.executeYes();
});
});
function assertEmpty(id) {
var fields = document.querySelectorAll('#' + id + ' input');
for (var i = 0; i < fields.length; i++) {
var currField = fields[i];
if (currField.dataset.field && currField.dataset.field != 'type') {
assert.isTrue(fields[i].value === '');
}
}
}
function assertPhoneData(c, phoneData) {
var data = phoneData || mockContact;
var valuePhone = document.querySelector('#number_' + c).value;
var typePhone = document.querySelector('#tel_type_' + c).textContent;
var carrierPhone = document.querySelector('#carrier_' + c).value;
assert.isTrue(valuePhone === data.tel[c].value);
assert.isTrue(typePhone === data.tel[c].type[0]);
assert.isTrue(carrierPhone === data.tel[c].carrier);
}
function assertEmailData(c, emailData) {
var data = emailData || mockContact;
var valueEmail = document.querySelector('#email_' + c).value;
var typeEmail = document.querySelector('#email_type_' + c).textContent;
assert.isTrue(valueEmail === data.email[c].value);
assert.isTrue(typeEmail === data.email[c].type[0]);
}
function assertAddressData(c, addrData) {
var data = addrData || mockContact;
var valueType = document.querySelector('#address_type_' + c).textContent;
assert.isTrue(valueType === data.adr[c].type[0],
'Type Value as Expected');
valueType = document.querySelector('#locality_' + c).value;
assert.isTrue(valueType === data.adr[c].locality,
'Type Value as Expected');
valueType = document.querySelector('#countryName_' + c).value;
assert.isTrue(valueType === data.adr[c].countryName,
'Type Value as Expected');
}
});