This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
Bug 1039643: Write unit tests for contacts.js #22377
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
/*global MocksHelper, Contacts, MockMozContactsObj, | ||
MockasyncStorage, Mockfb | ||
*/ | ||
|
||
'use strict'; | ||
|
||
require('/apps/communications/contacts/test/unit/mock_mozContacts.js'); | ||
require('/shared/test/unit/mocks/mock_contact_all_fields.js'); | ||
require('/shared/test/unit/mocks/mock_lazy_loader.js'); | ||
require('/shared/test/unit/mocks/mock_async_storage.js'); | ||
require('/shared//test/unit/mocks/mock_simple_phone_matcher.js'); | ||
require('/apps/communications/contacts/test/unit/mock_fb.js'); | ||
|
||
var mocksHelperForContacts = new MocksHelper([ | ||
'asyncStorage', | ||
'LazyLoader', | ||
'SimplePhoneMatcher', | ||
'fb' | ||
]); | ||
|
||
mocksHelperForContacts.init(); | ||
|
||
require('/shared/js/dialer/contacts.js'); | ||
|
||
suite('dialer/contacts', function() { | ||
var realMozContacts; | ||
var searchNumber = '3334445555'; | ||
var mozContactNum = '638883'; | ||
var aMozTestContact = { | ||
id: 'abcdef', | ||
givenName: ['Carlos'], | ||
familyName: ['Fernández'], | ||
tel: [ | ||
{ | ||
type: ['home'], | ||
value: mozContactNum | ||
} | ||
] | ||
}; | ||
|
||
var aFacebookContact = { | ||
id:'567', | ||
updated: 'date', | ||
additionalName:['Green'], | ||
adr:[{type:['home'],locality:'Palencia', region:'Castilla y León', | ||
countryName:'España'},{type:['current'], locality:'Greater London', | ||
region:'London', countryName:'United Kingdom'}], | ||
bday:'Thu, 01 Jan 1970 00:00:00 GMT', | ||
email:[{type:['personal'],value:'test@test.com'}, | ||
{type:['work'],value:'test@work.com',pref:true}], | ||
honorificPrefix:['Mr.'],familyName:['Taylor'],givenName:['Bret'], | ||
nickname:['PG'],jobTitle:['Sr. Software Architect'],name:['Bret Taylor'], | ||
org:['FB'],tel:[{value:'+346578888888',type:['mobile'],carrier:'TEF', | ||
pref:true},{value:'+3120777777',type:['Home'],carrier:'KPN'}], | ||
url:[{type:['fb_profile_photo'],value:'https://abcd1.jpg'}], | ||
category:['favorite','facebook','not_linked',220439],note:['Note 1'], | ||
photo:[{}] | ||
}; | ||
aFacebookContact.bday = new Date(0); | ||
|
||
mocksHelperForContacts.attachTestHelpers(); | ||
|
||
suiteSetup(function() { | ||
realMozContacts = navigator.mozContacts; | ||
navigator.mozContacts = new MockMozContactsObj([aMozTestContact]); | ||
}); | ||
|
||
suiteTeardown(function() { | ||
navigator.mozContacts = realMozContacts; | ||
}); | ||
|
||
suite('> Contacts Revision', function() { | ||
test('> Should return revision', function() { | ||
Contacts.getRevision(function(status) { | ||
assert.deepEqual(status, 'fakeRevision'); | ||
}); | ||
}); | ||
}); | ||
|
||
suite('> FindByNumber', function() { | ||
var expectedMatchingTel = {type: ['home'], value: mozContactNum }; | ||
var expectedMatchingContact = {id: 'abcdef', givenName: ['Carlos'], | ||
familyName: ['Fernández'], | ||
tel: [expectedMatchingTel]}; | ||
|
||
setup(function() { | ||
this.sinon.spy(navigator.mozContacts, 'find'); | ||
this.sinon.spy(Mockfb, 'getContactByNumber'); | ||
this.sinon.spy(Mockfb, 'getData'); | ||
}); | ||
|
||
test('should find the exact number for < 7 digits', function() { | ||
var options = { | ||
filterBy: ['tel'], | ||
filterOp: 'equals', | ||
filterValue: mozContactNum | ||
}; | ||
Contacts.findByNumber(mozContactNum, function(contact, matchingTel) { | ||
sinon.assert.calledWith(navigator.mozContacts.find, options); | ||
assert.deepEqual(matchingTel, expectedMatchingTel); | ||
assert.deepEqual(contact, expectedMatchingContact); | ||
}); | ||
}); | ||
|
||
test('should find numbers matching for >= 7 digits', function() { | ||
var options = { | ||
filterBy: ['tel'], | ||
filterOp: 'match', | ||
filterValue: '6388830123' | ||
}; | ||
|
||
Contacts.findByNumber('6388830123', function(contact, matchingTel) { | ||
sinon.assert.calledWith(navigator.mozContacts.find, options); | ||
assert.deepEqual(matchingTel, expectedMatchingTel); | ||
assert.deepEqual(contact, expectedMatchingContact); | ||
}); | ||
}); | ||
|
||
test('should search FB if no MozContact found', function() { | ||
navigator.mozContacts.setContacts([]); | ||
Contacts.findByNumber('6388830123', function(contact, matchingTel) { | ||
sinon.assert.calledWith(Mockfb.getContactByNumber, '6388830123'); | ||
}); | ||
}); | ||
|
||
test('should NOT search FB if MozContact found', function() { | ||
navigator.mozContacts.setContacts([aMozTestContact]); | ||
|
||
Contacts.findByNumber(mozContactNum, function(contact, matchingTel) { | ||
sinon.assert.notCalled(Mockfb.getContactByNumber); | ||
assert.deepEqual(matchingTel, expectedMatchingTel); | ||
assert.deepEqual(contact, expectedMatchingContact); | ||
}); | ||
}); | ||
|
||
test('should return null if no FB or MozContact found', function() { | ||
navigator.mozContacts.setContacts([]); | ||
Mockfb.setEmptyContacts(); | ||
Contacts.findByNumber(searchNumber, function(contact, matchingTel) { | ||
assert.isNull(contact); | ||
}); | ||
}); | ||
|
||
test('should merge w/FB if contact is facebook linked', function() { | ||
navigator.mozContacts.setContacts([aMozTestContact]); | ||
Mockfb.setIsFbLinked(true); | ||
Contacts.findByNumber(mozContactNum, function(contact, matchingTel) { | ||
sinon.assert.calledOnce(Mockfb.getData); | ||
assert.deepEqual(matchingTel, expectedMatchingTel); | ||
//Check that the facebook data was added. Fake out the date (but | ||
//validate it's a date format first) to avoid having the test fail | ||
//due to date being mismatched. | ||
assert.ok(contact.updated instanceof Date && | ||
!isNaN(contact.updated.valueOf())); | ||
contact.updated = 'date'; | ||
assert.deepEqual(contact, aFacebookContact); | ||
}); | ||
}); | ||
|
||
test('should NOT merge w/FB if not linked', function() { | ||
navigator.mozContacts.setContacts([aMozTestContact]); | ||
Mockfb.setIsFbLinked(false); | ||
Contacts.findByNumber(searchNumber, function(contact, matchingTel) { | ||
sinon.assert.notCalled(Mockfb.getData); | ||
assert.notEqual(contact.id, '567'); | ||
assert.isUndefined(contact.jobTitle); | ||
}); | ||
}); | ||
}); | ||
|
||
suite('> FindListByNumber ', function() { | ||
var limit = 20; | ||
|
||
setup(function() { | ||
this.sinon.spy(navigator.mozContacts, 'find'); | ||
this.sinon.spy(Mockfb, 'isFbContact'); | ||
this.sinon.spy(Mockfb, 'getData'); | ||
|
||
this.sinon.stub(MockasyncStorage, 'getItem'); | ||
}); | ||
|
||
teardown(function() { | ||
MockasyncStorage.getItem.restore(); | ||
}); | ||
|
||
test('should call find with correct filter option', function() { | ||
//From contacts.js | ||
var options = { | ||
filterBy: ['tel'], | ||
filterOp: 'contains', | ||
filterValue: searchNumber, | ||
sortBy: 'familyName', | ||
sortOrder: 'ascending', | ||
filterLimit: limit | ||
}; | ||
|
||
Contacts.findListByNumber(searchNumber, limit, function() { | ||
sinon.assert.calledWith(navigator.mozContacts.find, options); | ||
}); | ||
}); | ||
|
||
test('should merge w/FB contacts if mozContacts are also FB contacts', | ||
function() { | ||
Mockfb.setIsFbContact(true); | ||
navigator.mozContacts.setContacts([aMozTestContact]); | ||
MockasyncStorage.getItem.yields(false); | ||
|
||
Contacts.findListByNumber(limit, searchNumber, function(contacts) { | ||
sinon.assert.called(Mockfb.isFbContact); | ||
sinon.assert.called(Mockfb.getData); | ||
//Check that the facebook data was added. Fake out the date (but | ||
//validate it's a date format first) to avoid having the test fail | ||
//due to date being mismatched. | ||
assert.ok(contacts[0].updated instanceof Date && | ||
!isNaN(contacts[0].updated.valueOf())); | ||
contacts[0].updated = 'date'; | ||
assert.deepEqual(contacts[0], aFacebookContact); | ||
}); | ||
}); | ||
|
||
test('should not merge w/FB contacts if mozContacts is not FB contacts', | ||
function() { | ||
Mockfb.setIsFbContact(false); | ||
navigator.mozContacts.setContacts([aMozTestContact]); | ||
MockasyncStorage.getItem.yields(true); | ||
|
||
Contacts.findListByNumber(limit, searchNumber, function(contacts) { | ||
sinon.assert.called(Mockfb.isFbContact); | ||
//Check that facebook data was not added | ||
assert.isUndefined(contacts[0].honorificPrefix); | ||
assert.isUndefined(contacts[0].jobTitle); | ||
assert.isUndefined(contacts[0].org); | ||
assert.isUndefined(contacts[0].note); | ||
//Check that existing MozContact data is unchanged | ||
assert.deepEqual(contacts[0], aMozTestContact); | ||
}); | ||
}); | ||
|
||
test('should NOT get FB contacts if no MozContacts found', function() { | ||
navigator.mozContacts.setContacts([]); | ||
Contacts.findListByNumber(searchNumber, limit, function(contacts) { | ||
sinon.assert.notCalled(Mockfb.isFbContact); | ||
assert.isNull(contacts); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
var MockSimplePhoneMatcher; | ||
|
||
function MockSimplePhoneMatcherObj() { | ||
} | ||
|
||
MockSimplePhoneMatcherObj.prototype = { | ||
generateVariants: function(number) { | ||
return number; | ||
}, | ||
sanitizedNumber: function spm_sanitizedNumber(number) { | ||
return number; | ||
}, | ||
bestMatch: function (variants, matches) { | ||
var bestMatchIndex = 0; | ||
var bestLocalIndex = 0; | ||
var allMatches = []; | ||
var matchNum = 0; | ||
return { | ||
totalMatchNum: matchNum, | ||
allMatches: allMatches, | ||
bestMatchIndex: bestMatchIndex, | ||
localIndex: bestLocalIndex | ||
}; | ||
} | ||
}; | ||
|
||
MockSimplePhoneMatcher = new MockSimplePhoneMatcherObj(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Audio and Notification are not needed.