import { Parser } from '../../src/parser'; import { expect } from 'chai'; import * as moment from 'moment'; import * as faker from 'faker'; import loadFakeContentFile from '../helpers/loadFakeContentFile'; import { FlightsNotFound } from '../../src/lib/errors/FlightsNotFound'; const testParseDateTime = () => { describe('.parseDateTime()', () => { it('Should return an unchanged but formatted date', () => { const dateTime = moment(faker.date.future()); const dateTimeContent = moment(dateTime).format(Parser.FORMAT_CONTENT); const result = Parser.parseDateTime(dateTimeContent); const expected = moment.utc(dateTimeContent, Parser.FORMAT_CONTENT).format(); expect(result).to.be.equal(expected); }); }); }; const testGetFlightNumber = () => { describe('.getFlightNumber()', () => { it('Should return a formatted flight number', () => { const result = Parser.getFlightNumber('O6', '6105'); expect(result).to.be.equal('O66105'); }); }); }; const testCalculateValuesTotal = () => { describe('.calculateValuesTotal()', () => { it('Should return total values (adults=1; children=0; infants=0)', () => { const result = Parser.calculateValuesTotal( { price: 1000, miles: 30000 }, { price: 900, miles: 25000 }, { price: 800, miles: 20000 }, 1, 0, 0, ); expect(result.price).to.be.equal(1000); expect(result.miles).to.be.equal(30000); }); it('Should return total values (adults=3; children=0; infants=0)', () => { const result = Parser.calculateValuesTotal( { price: 1000, miles: 30000 }, { price: 900, miles: 25000 }, { price: 800, miles: 20000 }, 3, 0, 0, ); expect(result.price).to.be.equal(3000); expect(result.miles).to.be.equal(90000); }); it('Should return total values (adults=3; children=2; infants=0)', () => { const result = Parser.calculateValuesTotal( { price: 1000, miles: 30000 }, { price: 900, miles: 25000 }, { price: 800, miles: 20000 }, 3, 2, 0, ); expect(result.price).to.be.equal(4800); expect(result.miles).to.be.equal(140000); }); it('Should return total values (adults=3; children=2; infants=1)', () => { const result = Parser.calculateValuesTotal( { price: 1000, miles: 30000 }, { price: 900, miles: 25000 }, { price: 800, miles: 20000 }, 3, 2, 1, ); expect(result.price).to.be.equal(5600); expect(result.miles).to.be.equal(160000); }); }); }; const testGetFlightId = () => { describe('.getFlightId()', () => { it('Should return a valid hash MD5', () => { const result = Parser.getFlightId( Parser.AIRLINE, 'O66151', [ { from: 'CNF', to: 'GRU', departureDate: '2019-03-04T20:05:00Z', arrivalDate: '2019-03-04T21:25:00Z', carrier: 'Avianca Brasil', flightNumber: 'O66151', aircraft: 'Airbus Industrie A320', duration: 80, layover: 0, stops: 0, cabin: 'EC', }, ], ); expect(result).to.be.equal('7660e406d3c1744485c253b8808369cc'); }); }); }; const testGetFees = () => { describe('.getFees()', () => { it('Should return a empty array when no have fee', () => { const result = Parser.getFees(3, 2, 1, 0, 0, 0); expect(result).to.be.empty; }); it('Should return a array with 1 item for Boarding Tax (adults=3; children=0; infants=0)', () => { const result = Parser.getFees(3, 0, 0, 100, 0, 0); expect(result).to.be.length(1).and.eql( [ { group: 'price', type: 'BOARDING_TAX', value: 300, }, ], ); }); it('Should return a array with 1 item for Boarding Tax (adults=3; children=2; infants=0)', () => { const result = Parser.getFees(3, 2, 0, 100, 0, 0); expect(result).to.be.length(1).and.eql( [ { group: 'price', type: 'BOARDING_TAX', value: 500, }, ], ); }); it('Should return a array with 1 item for Convenience Fee (adults=3; children=0; infants=0)', () => { const result = Parser.getFees(3, 0, 0, 0, 0, 100); expect(result).to.be.length(1).and.eql( [ { group: 'price', type: 'CONVENIENCE_FEE', value: 300, }, ], ); }); it('Should return a array with 1 item for Convenience Fee (adults=3; children=2; infants=0)', () => { const result = Parser.getFees(3, 2, 0, 0, 0, 100); expect(result).to.be.length(1).and.eql( [ { group: 'price', type: 'CONVENIENCE_FEE', value: 500, }, ], ); }); it('Should return a array with 2 itens for Boarding Tax and Convenience Fee (adults=3; children=0; infants=0)', () => { const result = Parser.getFees(3, 0, 0, 100, 0, 200); expect(result).to.be.length(2).and.eql( [ { group: 'price', type: 'BOARDING_TAX', value: 300, }, { group: 'price', type: 'CONVENIENCE_FEE', value: 600, }, ], ); }); it('Should return a array with 2 itens for Boarding Tax and Convenience Fee (adults=3; children=2; infants=0)', () => { const result = Parser.getFees(3, 2, 0, 100, 0, 200); expect(result).to.be.length(2).and.eql( [ { group: 'price', type: 'BOARDING_TAX', value: 500, }, { group: 'price', type: 'CONVENIENCE_FEE', value: 1000, }, ], ); }); }); }; const testParser = () => { const action = 'flights'; describe('.parser() => successfully', () => { const testSuccessfully = (type: string) => { const name = type.split('-').filter((c: any, k: number) => k >= 1).join(' '); it(name[0].toUpperCase() + name.slice(1), async () => { const expected = JSON.parse(loadFakeContentFile({ action, type, file: 'response.json' })); const request = JSON.parse(loadFakeContentFile({ action, type, file: 'request.json' })); const content = loadFakeContentFile({ action, type, file: 'content.html' }); const parser = new Parser(); const { tripType, cabin, adults, children, infants, toCountry } = request; const response = await parser .parse(content, tripType, cabin, adults, children, infants, toCountry); expect(response).to.be.eql(expected); }); }; [ '01-avianca-nacional-idaVolta-ec-adultos', '02-amigo-nacional-idaVolta-ec-adultos', ].map(async (t: string) => await testSuccessfully(t)); }); describe('.parser() => not found', () => { const testNotFound = (type: string, message: string, contentFile: string) => { it(message, async () => { const request = JSON.parse(loadFakeContentFile({ action, type, file: 'request.json' })); const content = contentFile ? loadFakeContentFile({ action, type, file: contentFile }) : ''; const parser = new Parser(); const { tripType, cabin, adults, children, infants, toCountry } = request; const badFn = () => parser.parse( content, tripType, cabin, adults, children, infants, toCountry, ) expect(badFn).to.throw(Error) .and.to.have.property('statusCode') .and.to.be.equal(404); }); }; [ ['13-not-found-with-message-from-cia-treated', 'Empty content', null], [ '13-not-found-with-message-from-cia-treated', 'With message from Cia (treated)', 'content.html', ], ['14-not-found-with-message-from-cia-raw', 'With message from Cia (raw)', 'content.html'], ['16-not-found-without-availability', 'Without availability', 'content.html'], ].map(async (t: string[]) => await testNotFound(t[0], t[1], t[2])); }); }; describe('Unit test: Parser', () => { testParseDateTime(); testGetFlightNumber(); testCalculateValuesTotal(); testGetFlightId(); testGetFees(); testParser(); });