import { PlanPaymController } from './PlanPaym'; import { QuoteService } from '../../common/services/QuoteService'; import { UserDataState } from '../../common/state/UserDataState'; import { HostExtractor } from '../../common/util/HostExtractor'; import { TrustpilotClient } from '../../common/clients/TrustpilotClient'; import { trustpilotSummaryResponse } from '../../common/entities/Trustpilot.fixture'; import { TrustpilotSummary } from '../../common/entities/Trustpilot'; import { AnalyticsService } from '../../common/services/AnalyticsService'; import { ModalService } from '../../common/services/ModalService'; import {quote, quoteWithMeters} from '../../common/entities/Quote.fixture'; import {Quote, QuoteWithMeters, SaveQuoteResponse} from '../../common/entities/Quote'; import { $q } from '../../common/util/PromiseUtils'; import { Mock, IMock, It, Times } from 'typemoq'; import { expect } from 'chai'; describe('Paym plan page controller', () => { let $state: IMock; let $location: IMock; let quoteService: IMock; let host: IMock; let trustpilotClient: IMock; let analyticsService: IMock; let modal: IMock; let userDataState: UserDataState; function newController() { return new PlanPaymController( $state.object, $location.object, $q, quoteService.object, userDataState, host.object, trustpilotClient.object, analyticsService.object, modal.object ); } beforeEach(() => { $state = Mock.ofType(); $location = Mock.ofType(); quoteService = Mock.ofType(); host = Mock.ofType(); trustpilotClient = Mock.ofType(); analyticsService = Mock.ofType(); modal = Mock.ofType(); userDataState = {} as UserDataState; }); it('should be able to save a quote', () => { userDataState.tariffId = 'Fixed'; userDataState.emailAddress = 'test@test.com'; const ctrl = newController(); quoteService.setup(x => x.saveQuote(It.isAny())).returns(() => Promise.resolve({data: { id: '1' }})); return expect(ctrl.saveQuote().then(() => ctrl.emailFormCurrentStatus)).to.eventually.equal('completed'); }); it('should handle errors when saving a quote', () => { quoteService.setup(x => x.saveQuote(It.isAny())).returns(() => Promise.reject>('error')); userDataState.tariffId = 'Fixed'; userDataState.emailAddress = 'test@test.com'; const ctrl = newController(); return expect(ctrl.saveQuote().then(() => ctrl.error)).to.eventually.equal('Sorry, we could not save your quote. Please try again later.'); }); it('should navigate to the get-quote page if there is an error loading the quote', () => { quoteService.setup(x => x.getQuote()).returns(() => Promise.reject('error')); trustpilotClient.setup(x => x.getSummary()).returns(() => Promise.resolve(trustpilotSummaryResponse)); const ctrl = newController(); return ctrl.$onInit().then(() => $state.verify(x => x.go(It.isValue('^.get-quote')), Times.once()) ); }); it('should navigate to the quote page if we are unable to find the selected tariff in the quote', () => { quoteService.setup(x => x.getQuote()).returns(() => Promise.resolve(quoteWithMeters)); trustpilotClient.setup(x => x.getSummary()).returns(() => Promise.resolve(trustpilotSummaryResponse)); userDataState.tariffId = undefined; const ctrl = newController(); return ctrl.$onInit().then(() => $state.verify(x => x.go(It.isValue('^.quote')), Times.once()) ); }); it('should set the correct state when the showEmail() function is invoked', () => { const ctrl = newController(); ctrl.showEmail(); expect(ctrl.emailFormCurrentStatus).to.equal('visible'); }); it('should be able to transition to the switch form', () => { const ctrl = newController(); ctrl.switch(); $state.verify(x => x.go(It.isValue('^.switch')), Times.once()); }); });