import { SimplySmartService } from "./SimplySmartService"; import { appConfig, appConfigCommunity } from "../common/entities/AppConfig.fixture"; import { Mock, It, IMock, Times } from 'typemoq'; import { expect } from 'chai'; describe('SimplySmart service', () => { let service: SimplySmartService; let localStorageService: IMock<{ get(prop: string): string }>; let experimentService: IMock<{ activateAndGetStatus(exp: any): boolean }>; let eligibilityService: IMock<{ isAreaEligibleForTariff(): boolean }>; let RUNNING_EXPERIMENTS: any; let APP_CONFIG: any; beforeEach(() => { localStorageService = Mock.ofType(); experimentService = Mock.ofType(); eligibilityService = Mock.ofType(); RUNNING_EXPERIMENTS = { 'SIMPLY_SMART': 'QS - Simply smart' }; APP_CONFIG = { SIMPLY_SMART_POSTCODE_CHECK: true }; buildService(APP_CONFIG, appConfig); }); function buildService(appConfModule, appConfWindow?) { appConfModule = appConfModule ? appConfModule : APP_CONFIG; appConfWindow = appConfWindow ? appConfWindow : appConfig; service = new SimplySmartService(appConfModule, appConfWindow, localStorageService.object, experimentService.object, RUNNING_EXPERIMENTS, eligibilityService.object); } it("should force users to see Simply Smart if their postcode is in the fixed list of postcodes " + "and the simplySmartPostcodeCheck feature flag is on", () => { localStorageService.setup(x => x.get(It.isAnyString())).returns(() => "B1 1AA"); expect(service.shouldForceShowSimplySmart()).to.be.true; }); it("should force users to see Simply Smart if area eligibility check is required " + "and the postcode is eligible", () => { localStorageService.setup(x => x.get(It.isAnyString())).returns(() => "A1 1AA"); eligibilityService.setup(x => x.isAreaEligibleForTariff()).returns(() => true); expect(service.shouldForceShowSimplySmart(true)).to.be.true; }); it("should not force users to see Simply Smart if area eligibility check is required " + "and the postcode is not eligible", () => { localStorageService.setup(x => x.get(It.isAnyString())).returns(() => "A1 1AA"); eligibilityService.setup(x => x.isAreaEligibleForTariff()).returns(() => false); expect(service.shouldForceShowSimplySmart(true)).to.be.false; }); it("shouldn't force users to see Simply Smart if the simplySmartPostcodeCheck feature flag is off", () => { buildService({ SIMPLY_SMART_POSTCODE_CHECK: false }); localStorageService.setup(x => x.get(It.isAnyString())).returns(() => "B1 1AA"); expect(service.shouldForceShowSimplySmart()).to.be.false; }); it("shouldn't force users to see Simply Smart for communities", () => { buildService(APP_CONFIG, appConfigCommunity); localStorageService.setup(x => x.get(It.isAnyString())).returns(() => "B1 1AA"); expect(service.shouldForceShowSimplySmart()).to.be.false; }); it("shouldn't force users to see Simply Smart if their postcode isn't in the fixed list of postcodes", () => { localStorageService.setup(x => x.get(It.isAnyString())).returns(() => "A1 1AA"); expect(service.shouldForceShowSimplySmart()).to.be.false; }); it("shouldn't force users to see Simply Smart if the postcode is invalid/empty", () => { localStorageService.setup(x => x.get(It.isAnyString())).returns(() => undefined); expect(service.shouldForceShowSimplySmart()).to.be.false; localStorageService.setup(x => x.get(It.isAnyString())).returns(() => ""); expect(service.shouldForceShowSimplySmart()).to.be.false; localStorageService.setup(x => x.get(It.isAnyString())).returns(() => "A1 1AA"); expect(service.shouldForceShowSimplySmart()).to.be.false; }); it("should allow users to see Simply Smart if they are in eligible area and if the A/B test is running", () => { eligibilityService.setup(x => x.isAreaEligibleForTariff()).returns(() => true); experimentService.setup(x => x.activateAndGetStatus(It.isAny())).returns(() => true); expect(service.shouldForceShowSimplySmart()).to.be.false; expect(service.shouldShowSimplySmart()).to.be.true; }); it("shouldn't allow users to see Simply Smart if they are in non-eligible area or if the A/B test isn't running", () => { eligibilityService.setup(x => x.isAreaEligibleForTariff()).returns(() => false); experimentService.setup(x => x.activateAndGetStatus(It.isAny())).returns(() => true); expect(service.shouldForceShowSimplySmart()).to.be.false; expect(service.shouldShowSimplySmart()).to.be.false; }); it("shouldn't allow users to see Simply Smart if they are in eligible area and the A/B test isn't running", () => { eligibilityService.setup(x => x.isAreaEligibleForTariff()).returns(() => true); experimentService.setup(x => x.activateAndGetStatus(It.isAny())).returns(() => false); expect(service.shouldForceShowSimplySmart()).to.be.false; expect(service.shouldShowSimplySmart()).to.be.false; }); });