declare const global: any import { JSDOM } from 'jsdom' import { expect } from 'chai' import { stub } from 'sinon' import { getTrackedEmail, addIdentityTrackingToLink, extractLinksToReplace, replaceLinksWithWrappedLinks, generateOrgIdRegex, domainIncludes, getGmailIdFromResponse, shouldParseLink, isHttpProtocol, getApplicableLinksFromDoc, } from '../../js/utils/email' import { GMAILRESPONSES } from './gmailReponses' describe('utils/email', () => { before(() => { global.window = new JSDOM(`

Hello world

`).window }) after(() => { delete global.window }) describe('domainIncludes', () => { it('should check the list correctly', () => { expect(domainIncludes(['google.com'], 'www.google.com')).to.be.true expect(domainIncludes(['google.com'], 'www.bing.com')).to.be.false }) }) describe('getTrackedEmail', () => { it('should parse plain email', () => { expect(getTrackedEmail('trevor@drift.com')).to.equal('trevor@drift.com') }) it('should parse email + name', () => { expect(getTrackedEmail('Trevor Rundell ')).to.equal('trevor@drift.com') }) }) describe('addTrackingToLink', () => { it('should add the utk correctly', () => { expect(addIdentityTrackingToLink('www.drift.com', 'token')).to.equal('www.drift.com?d_utk=token') }) }) describe('extractLinksToReplace', () => { const mockCompose = { body: stub(), recipients: stub().returns({ to: ['Tommy Hu '] }), subject: stub().returns('subject'), } const defaultData = { endUserEmail: 'thu@drift.com', emailSubject: 'subject', emailUUID: 'testUUID', } it('should extract the url for one link', () => { mockCompose.body.returns('www.drift.com') expect(extractLinksToReplace(mockCompose, 'testUUID')) .to.deep.equal([ { ...defaultData, linkText: 'www.drift.com', url: 'http://www.drift.com' }, ]) }) it('should extract the url for multiple link', () => { mockCompose.body.returns('www.drift.comteam.driftqa.com') expect(extractLinksToReplace(mockCompose, 'testUUID')) .to.deep.equal([ { ...defaultData, linkText: 'www.drift.com', url: 'http://www.drift.com' }, { ...defaultData, linkText: 'team.driftqa.com', url: 'http://team.driftqa.com' }, ]) }) }) describe('replaceLinksWithWrappedLinks', () => { it('should replace links with wrapped links', () => { const testBody = 'www.drift.comwww.driftqa.com' const testWrappedLinks = [ { url: 'http://www.drift.com', clickId: 'id1', }, { url: 'http://www.driftqa.com', clickId: 'id2', }, ] expect(replaceLinksWithWrappedLinks(testBody, testWrappedLinks)) .to.equal('www.drift.comwww.driftqa.com') }) }) describe('generateOrgIdRegex', () => { it('generates correct regex', () => { const orgIdRegex = generateOrgIdRegex(1) expect(!!'www.googleurl.com#www.trackingurl.com/pixel.gif?orgId=1'.match(orgIdRegex)).to.equal(true) expect(!!'www.googleurl.com#www.trackingurl.com/pixel.gif?orgId=2'.match(orgIdRegex)).to.equal(false) // has to match the entire end expect(!!'www.googleurl.com#www.trackingurl.com/pixel.gif?orgId=12'.match(orgIdRegex)).to.equal(false) }) }) describe('getGmailIdFromResponse', () => { it('should extract the id from all responses', () => { GMAILRESPONSES.map((response, index) => { expect(getGmailIdFromResponse(response)).to.equal('TESTGMAILID', `failed at index: ${index}`) }) }) }) describe('shouldParseLink', () => { it('should return true for valid values of orgUrl', () => { const variationsOfLinks = [ 'marathonmuse.com', 'www.marathonmuse.com', 'www.more.marathonmuse.com', 'http://www.marathonmuse.com', 'https://www.marathonmuse.com', 'https://www.marathonmuse.com/this/also/works', ] variationsOfLinks.forEach(variationOfSrc => variationsOfLinks.forEach(variationOfOrgUrl => expect(shouldParseLink({ url: variationOfSrc }, { orgUrl: variationOfOrgUrl }), `${variationOfSrc} didn't work with ${variationOfOrgUrl}`).to.be.true)) expect(shouldParseLink({ url: 'www.more.marathonmuse.co.uk' }, { orgUrl: 'marathonmuse.co.uk' })).to.be.true }) it('should return false for non valid values of orgUrl', () => { expect(shouldParseLink({ url: 'www.drift.com' }, { orgUrl: 'marathonmuse.com' })).to.be.false expect(shouldParseLink({ url: 'marathonmuse.com' }, { orgUrl: 'www.drift.com' })).to.be.false }) it('should work for drift.me links that match the globalHandle', () => { expect(shouldParseLink({ url: 'www.drift.me/globalTommy' }, { globalHandle: 'globalTommy' })).to.be.true expect(shouldParseLink({ url: 'www.drift.me/tommy' }, { globalHandle: 'globalTommy' })).to.be.false }) it('should work for drift profile pages that match the handle', () => { expect(shouldParseLink({ url: 'www.orghandle.drift.com/tommy' }, { orgHandle: 'orghandle', handle: 'tommy' })).to.be.true expect(shouldParseLink({ url: 'www.orghandle.drift.com/notTommy' }, { orgHandle: 'orghandle', handle: 'tommy' })).to.be.false }) }) describe('isHttpProtocol', () => { it('should return true for http/s addresses', () => { expect(isHttpProtocol('http://www.drift.com')).to.be.true expect(isHttpProtocol('https://www.drift.com')).to.be.true }) it('should return false for non http/s addresses', () => { expect(isHttpProtocol('ftp://www.drift.com')).to.be.false expect(isHttpProtocol('ssh://www.drift.com')).to.be.false expect(isHttpProtocol('mailto:tommy@drift.com')).to.be.false }) }) describe('getApplicableLinksFromDoc', () => { const testBody = `
www.drift.com
www.drift.com
www.drift.com
ssh://drift.com
ftp://drift.com
tommy@drift.com
` const dom = new JSDOM(testBody) const doc = dom.window.document it('should extract three links to parse', () => { expect(getApplicableLinksFromDoc(doc).length).to.equal(3) }) }) })