Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/meteor/.mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ module.exports = {
'app/api/server/lib/**/*.spec.ts',
'app/file-upload/server/**/*.spec.ts',
'app/statistics/server/**/*.spec.ts',
'app/livechat/server/lib/**/*.spec.ts',
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const dataMap: [Partial<ILivechatVisitor>, IOmnichannelSource, CreateContactPara
contactManager: {
username: 'user1',
},
lastChat: { _id: 'afdsfdasf', ts: testDate },
},
{
type: OmnichannelSourceType.WIDGET,
Expand All @@ -50,8 +51,10 @@ const dataMap: [Partial<ILivechatVisitor>, IOmnichannelSource, CreateContactPara
details: {
type: OmnichannelSourceType.WIDGET,
},
lastChat: { _id: 'afdsfdasf', ts: testDate },
},
],
lastChat: { _id: 'afdsfdasf', ts: testDate },
customFields: undefined,
shouldValidateCustomFields: false,
contactManager: 'manager1',
Expand All @@ -62,6 +65,7 @@ const dataMap: [Partial<ILivechatVisitor>, IOmnichannelSource, CreateContactPara
{
_id: 'visitor1',
username: 'Username',
lastChat: { _id: 'afdsfdasf', ts: testDate },
},
{
type: OmnichannelSourceType.SMS,
Expand All @@ -85,11 +89,13 @@ const dataMap: [Partial<ILivechatVisitor>, IOmnichannelSource, CreateContactPara
details: {
type: OmnichannelSourceType.SMS,
},
lastChat: { _id: 'afdsfdasf', ts: testDate },
},
],
customFields: undefined,
shouldValidateCustomFields: false,
contactManager: undefined,
lastChat: { _id: 'afdsfdasf', ts: testDate },
},
],

Expand All @@ -113,7 +119,7 @@ const dataMap: [Partial<ILivechatVisitor>, IOmnichannelSource, CreateContactPara
unknown: false,
channels: [
{
name: 'sms',
name: 'widget',
visitor: {
visitorId: 'visitor1',
source: {
Expand Down Expand Up @@ -150,6 +156,7 @@ const dataMap: [Partial<ILivechatVisitor>, IOmnichannelSource, CreateContactPara
invalidCustomFieldId: 'invalidCustomFieldValue',
},
activity: [],
lastChat: { _id: 'afdsfdasf', ts: testDate },
},
{
type: OmnichannelSourceType.WIDGET,
Expand All @@ -161,7 +168,7 @@ const dataMap: [Partial<ILivechatVisitor>, IOmnichannelSource, CreateContactPara
unknown: true,
channels: [
{
name: 'sms',
name: 'widget',
visitor: {
visitorId: 'visitor1',
source: {
Expand All @@ -173,13 +180,15 @@ const dataMap: [Partial<ILivechatVisitor>, IOmnichannelSource, CreateContactPara
details: {
type: OmnichannelSourceType.WIDGET,
},
lastChat: { _id: 'afdsfdasf', ts: testDate },
},
],
customFields: {
customFieldId: 'customFieldValue',
},
shouldValidateCustomFields: false,
contactManager: undefined,
lastChat: { _id: 'afdsfdasf', ts: testDate },
},
],
];
Expand All @@ -197,10 +206,9 @@ describe('mapVisitorToContact', () => {
getAllowedCustomFields.resolves([{ _id: 'customFieldId', label: 'custom-field-label' }]);
});

const index = 0;
for (const [visitor, source, contact] of dataMap) {
dataMap.forEach(([visitor, source, contact], index) => {
it(`should map an ILivechatVisitor + IOmnichannelSource to an ILivechatContact [${index}]`, async () => {
expect(await mapVisitorToContact(visitor, source)).to.be.deep.equal(contact);
});
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,14 @@ describe('resolveContactConflicts', () => {
conflictingFields: [{ field: 'customFields.customField', value: 'oldValue' }],
});
modelsMock.Settings.incrementValueById.resolves(1);
modelsMock.LivechatContacts.updateContact.resolves({
_id: 'contactId',
customField: { customField: 'newValue' },
conflictingFields: [{ field: 'customFields.customField', value: 'oldValue' }],
} as Partial<ILivechatContact>);

const result = await resolveContactConflicts({ contactId: 'contactId', customField: { customField: 'newValue' } });
await resolveContactConflicts({ contactId: 'contactId', customFields: { customField: 'newestValue' } });

expect(modelsMock.LivechatContacts.findOneEnabledById.getCall(0).args[0]).to.be.equal('contactId');

expect(modelsMock.Settings.incrementValueById.getCall(0).args[0]).to.be.equal('Livechat_conflicting_fields_counter');
expect(modelsMock.Settings.incrementValueById.getCall(0).args[1]).to.be.equal(1);

expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[0]).to.be.equal('contactId');
expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({ customFields: { customField: 'newValue' } });
expect(result).to.be.deep.equal({
_id: 'contactId',
customField: { customField: 'newValue' },
conflictingFields: [],
expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({
customFields: { customField: 'newestValue' },
});
});

Expand All @@ -66,28 +55,13 @@ describe('resolveContactConflicts', () => {
conflictingFields: [{ field: 'name', value: 'Old Name' }],
});
modelsMock.Settings.incrementValueById.resolves(1);
modelsMock.LivechatContacts.updateContact.resolves({
_id: 'contactId',
name: 'New Name',
customField: { customField: 'newValue' },
conflictingFields: [],
} as Partial<ILivechatContact>);

const result = await resolveContactConflicts({ contactId: 'contactId', name: 'New Name' });
await resolveContactConflicts({ contactId: 'contactId', name: 'New Name' });

expect(modelsMock.LivechatContacts.findOneEnabledById.getCall(0).args[0]).to.be.equal('contactId');

expect(modelsMock.Settings.incrementValueById.getCall(0).args[0]).to.be.equal('Livechat_conflicting_fields_counter');
expect(modelsMock.Settings.incrementValueById.getCall(0).args[1]).to.be.equal(1);

expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[0]).to.be.equal('contactId');
expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({ name: 'New Name' });
expect(result).to.be.deep.equal({
_id: 'contactId',
name: 'New Name',
customField: { customField: 'newValue' },
conflictingFields: [],
});
});

it('should update the contact with the resolved contact manager', async () => {
Expand All @@ -96,31 +70,16 @@ describe('resolveContactConflicts', () => {
name: 'Name',
contactManager: 'contactManagerId',
customFields: { customField: 'value' },
conflictingFields: [{ field: 'manager', value: 'newContactManagerId' }],
conflictingFields: [{ field: 'manager', value: 'oldManagerId' }],
});
modelsMock.Settings.incrementValueById.resolves(1);
modelsMock.LivechatContacts.updateContact.resolves({
_id: 'contactId',
name: 'Name',
contactManager: 'newContactManagerId',
customField: { customField: 'value' },
conflictingFields: [],
} as Partial<ILivechatContact>);

const result = await resolveContactConflicts({ contactId: 'contactId', name: 'New Name' });
await resolveContactConflicts({ contactId: 'contactId', name: 'New Name', customFields: { manager: 'newContactManagerId' } });

expect(modelsMock.LivechatContacts.findOneEnabledById.getCall(0).args[0]).to.be.equal('contactId');

expect(modelsMock.Settings.incrementValueById.getCall(0).args[0]).to.be.equal('Livechat_conflicting_fields_counter');
expect(modelsMock.Settings.incrementValueById.getCall(0).args[1]).to.be.equal(1);

expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[0]).to.be.equal('contactId');
expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({ contactManager: 'newContactManagerId' });
expect(result).to.be.deep.equal({
_id: 'contactId',
name: 'New Name',
customField: { customField: 'newValue' },
conflictingFields: [],
expect(modelsMock.LivechatContacts.updateContact.getCall(0).args[1]).to.be.deep.contain({
customFields: { customField: 'value', manager: 'newContactManagerId' },
});
});

Expand Down Expand Up @@ -219,21 +178,4 @@ describe('resolveContactConflicts', () => {
);
expect(modelsMock.LivechatContacts.updateContact.getCall(0)).to.be.null;
});

it('should throw an error if the contact manager is invalid', async () => {
modelsMock.LivechatContacts.findOneEnabledById.resolves({
_id: 'contactId',
name: 'Name',
contactManager: 'contactManagerId',
customFields: { customField: 'value' },
conflictingFields: [{ field: 'manager', value: 'newContactManagerId' }],
});
await expect(resolveContactConflicts({ contactId: 'id', contactManager: 'invalid' })).to.be.rejectedWith(
'error-contact-manager-not-found',
);

expect(validateContactManagerMock.getCall(0).args[0]).to.be.equal('invalid');

expect(modelsMock.LivechatContacts.updateContact.getCall(0)).to.be.null;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const modelsMock = {

const { updateContact } = proxyquire.noCallThru().load('./updateContact', {
'./getAllowedCustomFields': {
getAllowedCustomFields: sinon.stub(),
getAllowedCustomFields: sinon.stub().resolves([]),
},
'./validateContactManager': {
validateContactManager: sinon.stub(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('validateCustomFields', () => {
const allowedCustomFields = [{ _id: 'field1', label: 'Field 1', required: false }];
const customFields = { field2: 'value' };

expect(() => validateCustomFields(allowedCustomFields, customFields, { ignoreValidationErrors: true }))
expect(() => validateCustomFields(allowedCustomFields, customFields, { ignoreAdditionalFields: true }))
.not.to.throw()
.and.to.equal({});
});
Expand Down
Loading