import {Mod as CoreModule} from '../../core/core.mod'; import {Mod as FlightListModule} from '../flight-list.mod'; import FlightDetailModule from '../../flight-detail/flight-detail.mod'; import {Directive, Controller, FlightListParams} from './flight-list'; import {IFlightDetail} from './../../core/service/flightService'; import {ComponentTest} from'../../infrastructure/ComponentHelper'; class FlightListTest extends ComponentTest { public api: any; public $stateParams: FlightListParams; public $state: ng.ui.IStateService; public flights: Array; public ctrl: Controller; constructor() { super(Directive.$componentName, 'app/flight-list/list/flight-list.tpl.html'); } public buildFlights(count: number): void { this.flights = []; for (let i = 0; i < count; i++) { this.flights.push({ Id: '1', Type: 'A', Operator: 'SK', Number: '0214' + i, AircraftType: '333' + i, Registration: 'PK-GPE' + i, Location: 'MAN', Scheduled: '09:00', Estimated: '09:00', Actual: '09:00', Terminal: 'T1', Stand: 'A' + i, FlightConcat: 'SK' + '0214' + i }); } } public setUpForSuccessfulGet(): void { this.$stateParams.filter = "default"; this.buildFlights(5); let expectedUrl = this.api.flightList + "?window=" + this.$stateParams.filter; this.$httpBackend.expectGET(expectedUrl) .respond(201, { Flights: this.flights, RequestId: 'ste1234567' }); this.ctrl = this.compile(); } public setUpForNoFlightsGet(): void { this.$stateParams.filter = "default"; let expectedUrl = this.api.flightList + "?window=" + this.$stateParams.filter; this.$httpBackend.expectGET(expectedUrl) .respond(201, { Flights: new Array(), RequestId: 'ste1234567' }); this.ctrl = this.compile(); } } describe('Framework: fieldListFilter', () => { let $filter: any; let instance: FlightListTest, flightList: FlightListModule, core: CoreModule, flightDetail: FlightDetailModule; beforeEach(() => { if (!instance) { core = new CoreModule(angular); flightList = new FlightListModule(angular); flightDetail = new FlightDetailModule(angular); angular.mock.module('ui.router'); angular.mock.module('ionic'); angular.mock.module('chroma.configuration'); angular.mock.module(CoreModule.$componentName); angular.mock.module(FlightDetailModule.$componentName); angular.mock.module(FlightListModule.$componentName); angular.mock.inject((_$filter_, $state, $stateParams, api) => { instance = new FlightListTest(); $filter = _$filter_; instance.$state = $state; instance.$stateParams = $stateParams; instance.api = api; }); } }); it('Flight Filter: should return flight SK02141 when I search with SK02141', () => { instance.setUpForSuccessfulGet(); instance.buildFlights(5); let result = $filter('fieldListFilter')(instance.ctrl.flights, 'SK02141', ['Number', 'Operator', 'AircraftType', 'Location', 'Terminal', 'Registration', 'Stand', 'FlightConcat']); expect(result.length).toBe(1); expect(result[0].FlightConcat).toBe('SK02141'); }); it('fieldListFilter: filter should be registered', () => { let result = $filter('fieldListFilter'); expect(result).not.toBeUndefined(); }); it('fieldListFilter: empty query string should return all flight results', () => { instance.buildFlights(5); let result = $filter('fieldListFilter')(instance.ctrl.flights, '', ['Number', 'Operator', 'AircraftType', 'Location', 'Terminal', 'Registration', 'Stand', 'FlightConcat']); expect(instance.ctrl.flights.length).toBe(5); }); it('fieldListFilter: should return flight with stand A1 when I search for A1', () => { instance.setUpForSuccessfulGet(); instance.buildFlights(5); let result = $filter('fieldListFilter')(instance.ctrl.flights, 'A1', ['Number', 'Operator', 'AircraftType', 'Location', 'Terminal', 'Registration', 'Stand', 'FlightConcat']); expect(result.length).toBe(1); expect(result[0].Stand).toBe('A1'); }); it('fieldListFilter: should return flight with aircraft type 331 when I search for 331', () => { instance.setUpForSuccessfulGet(); instance.buildFlights(5); let result = $filter('fieldListFilter')(instance.ctrl.flights, '3331', ['Number', 'Operator', 'AircraftType', 'Location', 'Terminal', 'Registration', 'Stand', 'FlightConcat']); expect(result.length).toBe(1); expect(result[0].AircraftType).toBe('3331'); }); it('fieldListFilter: should return flight with registration PK-GPE2 when I search for PK-GPE2', () => { instance.setUpForSuccessfulGet(); instance.buildFlights(5); let result = $filter('fieldListFilter')(instance.ctrl.flights, 'PK-GPE2', ['Number', 'Operator', 'AircraftType', 'Location', 'Terminal', 'Registration', 'Stand', 'FlightConcat']); expect(result.length).toBe(1); expect(result[0].Registration).toBe('PK-GPE2'); }); it('fieldListFilter: should return all flights when I search for PK-GPE as they all start with that', () => { instance.setUpForSuccessfulGet(); instance.buildFlights(5); let result = $filter('fieldListFilter')(instance.ctrl.flights, 'PK-GPE', ['Number', 'Operator', 'AircraftType', 'Location', 'Terminal', 'Registration', 'Stand', 'FlightConcat']); expect(result.length).toBe(5); }); it('fieldListFilter: should return all flights when I search T1 as they are all assigned that terminal', () => { instance.setUpForSuccessfulGet(); instance.buildFlights(5); let result = $filter('fieldListFilter')(instance.ctrl.flights, 'T1', ['Number', 'Operator', 'AircraftType', 'Location', 'Terminal', 'Registration', 'Stand', 'FlightConcat']); expect(result.length).toBe(5); }); }); describe('Flight List View Tests', () => { let instance: FlightListTest, core: CoreModule, flightDetail: FlightDetailModule, flightList: FlightListModule; beforeEach(() => { if (!instance) { core = new CoreModule(angular); flightDetail = new FlightDetailModule(angular); flightList = new FlightListModule(angular); angular.mock.module('ui.router'); angular.mock.module('ionic'); angular.mock.module('chroma.configuration'); angular.mock.module(CoreModule.$componentName); angular.mock.module(FlightDetailModule.$componentName); angular.mock.module(FlightListModule.$componentName); angular.mock.inject((api, $state, $stateParams) => { instance = new FlightListTest(); instance.api = api; instance.$state = $state; instance.$stateParams = $stateParams; }); } }); it('Filter has 5 flights and 5 flights displayed', () => { instance.setUpForSuccessfulGet(); expect(instance.ctrl).toBeDefined(); expect(instance.ctrl.flights).toBeDefined(); expect(instance.ctrl.flights.length).toEqual(5); expect(instance.ctrl.flights).toEqual(instance.flights); }); it('New flight is added when refreshed', () => { instance.setUpForSuccessfulGet(); var currentCount = instance.ctrl.flights.length; instance.buildFlights(6); let expectedUrl = instance.api.flightList + "?window=" + instance.$stateParams.filter; instance.$httpBackend.expectGET(expectedUrl) .respond(201, { Flights: instance.flights, RequestId: 'ste1234567' }); instance.ctrl.load(false); instance.$httpBackend.flush(); expect(instance.ctrl.flights.length).toBe(6); expect(instance.ctrl.flights.length).toBeGreaterThan(currentCount); }); it('Detail shown when flight pressed', () => { let firstFlight = instance.flights[0], requestId = 'ste1234567'; spyOn(instance.$state, "go"); instance.ctrl.showDetail(firstFlight); expect(instance.$state.go).toHaveBeenCalledWith('chroma.flight-detail', { id: firstFlight.Id, flight: firstFlight, requestId: requestId }); }); it('No flights error message displayed', () => { instance.setUpForNoFlightsGet(); expect(instance.ctrl.flights.length).toBe(0); expect(instance.ctrl.message).toBe(Controller.NO_FLIGHTS_MESSAGE); }); });