import { leads } from '../seeds'; import { assignLeadId, cleanPhone, cleanRow, cleanString, columnHeaders, dobValues, extraColumns, initLeadId, isHiddenFile, isTuesday, isWithinAgeRange, report, setBranchCodes, validateBranch, validateEmail, validateLoadDate, validatePhone, validData, zipcode, } from '../src'; import { leadWithLeadId } from './../seeds'; describe('Init lead Id', () => { test('should return a valid leadId', () => { const date: string = '2017-12-09'; expect(initLeadId(date)).toBe(2017120900001); }); }); describe('isTuesday', () => { test('return false if not Tuesday', () => { expect(isTuesday('2017-12-07')).toBe(false); }); test('return true if its Tuesday', () => { expect(isTuesday('2017-12-05')).toBe(true); }); }); describe('dateTimeRange', () => { test('return true if between range', () => { expect( validateLoadDate({ dateLoaded: '2017-12-05', datePosted: '12/01/2017' }), ).toBe(true); }); test('return false if between range', () => { expect( validateLoadDate({ dateLoaded: '2017-12-05', datePosted: '2017-01-12' }), ).toBe(false); }); }); describe('validateEmail', () => { test('should return true if valid email', () => { expect(validateEmail('john.doe@gmail.com')).toBe(true); }); test('should return false if not valid email', () => { expect(validateEmail('invalid.com')).toBe(false); }); test('should return false if empty', () => { expect(validateEmail('')).toBe(false); }); }); describe('zipcode', () => { test('should return the save zip code if it has five digits', () => { expect(zipcode('20768')).toBe('20768'); }); test('should add a leading zero if the zip is not 5 digits', () => { expect(zipcode('6704')).toBe('06704'); }); test('should return empty strint if empty', () => { expect(zipcode('')).toBe(''); }); test('should return 5 digit from zip + 4', () => { expect(zipcode('06708-1234')).toBe('06708'); }); test('should return 5 digits from zip + 4 missing starting 0', () => { expect(zipcode('6708-1234')).toBe('06708'); }); }); describe('dobValues', () => { test('should return the correct values', () => { expect(dobValues('11/11/1988')).toEqual({ BIRTH_MONTH: 11, BIRTH_DAY: 11, BIRTH_YEAR: 1988, }); }); test('should return zeros if invalid', () => { expect(dobValues('99/99/1988')).toEqual({ BIRTH_MONTH: 0, BIRTH_DAY: 0, BIRTH_YEAR: 0, }); }); }); describe('age-range', () => { test('return false if unable to validate age-range', () => { expect( isWithinAgeRange({ loadDate: '2016-10-19', dob: '1951-11-11', fileName: 'DK_pre', }), ).toBe(false); }); describe('Health Leads', () => { test('return true if in range', () => { expect( isWithinAgeRange({ loadDate: '2016-10-19', dob: '1952-10-19', fileName: 'DK_hlt_pre', }), ).toBe(true); }); test('return false if out of range', () => { expect( isWithinAgeRange({ loadDate: '2016-10-19', dob: '2016-10-19', fileName: 'DK_hlt_pre', }), ).toBe(false); }); }); describe('LIFE Leads', () => { test('return true if in range', () => { expect( isWithinAgeRange({ loadDate: '2017-06-01', dob: '1955-06-01', fileName: 'DK_life_pre', }), ).toBe(true); }); test('return false if out of range', () => { expect( isWithinAgeRange({ loadDate: '2016-10-19', dob: '2016-10-19', fileName: 'DK_life_pre', }), ).toBe(false); }); }); }); describe('cleanPhone', () => { test('should remove non-digital characters', () => { expect(cleanPhone('(800) @ ¢ 500-2222')).toBe('8005002222'); expect(cleanPhone('8005002222')).toBe('8005002222'); }); }); describe('extraColumns', () => { let extraColumnData; beforeEach(() => { const data = { PRODUCT_OF_INTEREST: 'HEALTH', SOURCE_URL: 'www.usacoverage.com', SOURCEID: '11285-DK', }; const POI_LIFE = { PRODUCT_OF_INTEREST: 'LIFE' }; const POI_HEALTH = { PRODUCT_OF_INTEREST: 'HEALTH' }; extraColumnData = { DK_HEALTH: data, DK_LIFE: { ...data, ...{ PRODUCT_OF_INTEREST: 'LIFE' } }, IR: { ...data, ...{ SOURCEID: '55902-IR' } }, LP_AI: { ...data, ...{ SOURCEID: '00007AI' } }, LP_LS: { ...data, ...{ SOURCEID: '00007LS' } }, LP_OA: { ...data, ...{ SOURCEID: '00007OA' } }, STEVE: { ...data, ...POI_LIFE, ...{ SOURCEID: '13204' } }, STEVE_DS: { ...data, ...POI_LIFE, ...{ SOURCEID: '13204-DS' } }, STEVE_IFA: { ...data, ...POI_HEALTH, ...{ SOURCEID: '13204-IFA' } }, STEVE_IFA_DD: { ...data, ...POI_HEALTH, ...{ SOURCEID: '13204-IFA-DD' } }, DK_ERROR: { ...data, ...{ PRODUCT_OF_INTEREST: '', SOURCEID: '' } }, REV_FILE: { ...data, ...{ SOURCEID: '49124' } }, }; }); test('should return correct extra columns', () => { expect(extraColumns('11285-DK-hlt')).toEqual(extraColumnData.DK_HEALTH); expect(extraColumns('11285-DK-LIFE')).toEqual(extraColumnData.DK_LIFE); expect(extraColumns('55902-IR-hlt.xlsx')).toEqual(extraColumnData.IR); expect(extraColumns('00007AI-hlt.xlsx')).toEqual(extraColumnData.LP_AI); expect(extraColumns('00007LS-hlt.xlsx')).toEqual(extraColumnData.LP_LS); expect(extraColumns('00007OA-hlt.xlsx')).toEqual(extraColumnData.LP_OA); expect(extraColumns('13204-IFA-DD-HEALTH.xlsx')).toEqual( extraColumnData.STEVE_IFA_DD, ); expect(extraColumns('13204-DS-life.xlsx')).toEqual( extraColumnData.STEVE_DS, ); expect(extraColumns('13204-IFA-health.xlsx')).toEqual( extraColumnData.STEVE_IFA, ); }); test('should return the correct vendor id without sourceid', () => { expect(extraColumns('49124-HEALTH')).toEqual(extraColumnData.REV_FILE); }); test('should return an empty string when invalid', () => { expect(extraColumns('DK_pre')).toEqual(extraColumnData.DK_ERROR); }); }); describe('isHiddenFile', () => { test('should return false if the file is not hidden', () => { expect(isHiddenFile('11285-DK-hlt')).toBe(false); }); test('should return true if its a hidden file', () => { expect(isHiddenFile('.DS_Store')).toBe(true); }); }); describe('setBranchCodes', () => { test('should append correct branches', () => { const zipFile = [ { Branch: '1014', ZipCode: '01450', State: 'MA' }, { Branch: '1015', ZipCode: '01463', State: 'MA' }, { Branch: '1016', ZipCode: '01469', State: 'MA' }, ]; expect(setBranchCodes(zipFile)).toEqual({ '01450': '1014', '01463': '1015', '01469': '1016', }); }); }); describe('validateBranch', () => { const branch = { '01450': '1014', '01463': '1015', '01469': '1016', }; test('should return the correct branch value', () => { expect(validateBranch('01463', branch)).toBe(true); }); test('should return false if not true', () => { expect(validateBranch('1111', branch)).toBe(false); }); }); describe('columnHeaders', () => { const values = { row: { FIRSTNAME: 'trial', LASTNAME: 'trial', DOB: '11/11/1999', IP_ADDRESS: '123456', GENDER: 'Male', }, wrongRow: { FIRSTNAME: 'trial', LASTNAME: 'trial', OB: '11/11/1999' }, }; const headers = Object.keys(values.row); const wrongHeaders = Object.keys(values.wrongRow); test('ensure correct column headers', () => { expect(columnHeaders(headers)).toBe(true); }); test('return false if wrong column headers', () => { expect(columnHeaders(wrongHeaders)).toBe(false); }); }); describe('validatePhone', () => { test('should return true when phone is valid', () => { expect(validatePhone('(102) 598-1234')).toBe(true); }); test('should return false when phone is invalid', () => { expect(validatePhone('11111111')).toBe(false); }); }); describe('clean strings', () => { test('should return a clean string', () => { expect(cleanString('ñæme')).toBe('name'); expect(cleanString('test12.mail@gmail.com')).toBe('test12.mail@gmail.com'); }); }); describe('cleanRow', () => { const initRow = { DATE_POSTED: '2017-12-04', DOB: '1/2/45', FIRSTNAME: 'test', LASTNAME: 'tëstæ', EMAIL: 'test@test.com', PHONE: '1111111111', ADDRESS: '18 Ave Sw', ZIP: '91146', CITY: 'SOMEWHERE', STATE: 'WA', }; const finalRow = { DATE_POSTED: '2017-12-04', DOB: '1/2/45', FIRSTNAME: 'test', LASTNAME: 'testa', EMAIL: 'test@test.com', PHONE: '1111111111', ADDRESS: '18 Ave Sw', ZIP: '91146', CITY: 'SOMEWHERE', STATE: 'WA', }; test('should return a clean row', () => { expect(cleanRow(initRow)).toEqual(finalRow); }); }); describe('reports', () => { test('should return invalid data count', () => { expect(report(leads, 'VALID_PHONE')).toBe(1); expect(report(leads, 'VALID_AGE')).toBe(0); expect(report(leads, 'VALID_LOAD_DATE')).toBe(2); }); }); describe('validData', () => { test('return only valid data per the criteria', () => { expect(validData(leads, 'VALID_PHONE')).toHaveLength(1); expect(validData(leads, 'VALID_AGE')).toHaveLength(2); }); }); describe('assignLeadId', () => { test('should return file with leadid assined', () => { expect(assignLeadId(leads, 2018011200000)).toEqual(leadWithLeadId); }); });