jeudi 9 août 2018

Error when trying to test component with external package included

I am having an error in one of my test files where I get the following error when I try and start the Karma runner:

Error: Uncaught (in promise): TypeError: locale.toLowerCase is not a function
            at <Jasmine>
            at resolvePromise (node_modules/zone.js/dist/zone.js:814:31)
            at node_modules/zone.js/dist/zone.js:877:17
            at ZoneDelegate.invokeTask (node_modules/zone.js/dist/zone.js:421:31)
            at AsyncTestZoneSpec.onInvokeTask (node_modules/zone.js/dist/async-test.js:90:25)

I have no idea why this is happening and have been pulling my hair out over this. Here is the component under test. As you can see I am using an external package called NgxSmartModal:

import { Component, Inject, LOCALE_ID, OnDestroy, OnInit } from "@angular/core";
import { formatCurrency } from "@angular/common";

interface SecciItemInterface {
    name: string,
    content: string
}

@Component({
    selector: "secci-modal",
    templateUrl: "migration/app/pages/payment-page/sub-sections/secci-modal/secci-modal.component.html"
})

/**
 * Display the SECCI direct debit modal
 **/
export class SecciModal implements OnInit, OnDestroy {

    public totalAncillariesTax: number = 0;
    public ancillaries = [];
    public interestRate: any;

    constructor(
        @Inject("Brand") private Brand,
        @Inject("Quote") private Quote,
        @Inject("Api") private Api,
        @Inject("Global") private Global,
        @Inject("Page") private Page,
        @Inject(LOCALE_ID) private _locale: string
    ) {

    }

    public ngOnInit(): void {
        this.getInterestRate();
        this.ancillaries = this.secciAncillaries();
    }

    /**
     * Sets the ancillaries that are to be displayed in the modal
     *
     * @returns {Array<Object>}
     */
    public secciAncillaries(): Array<Object> {
        const ancDisplayList = [];

        if (this.Quote.cars) {
            if (this.Quote.ancillaries !== 'undefined' && this.Quote.ancillaries !== null) {
                const secciSelectedList = JSON.parse(JSON.stringify(this.Quote.ancillaries.selected));
                const secciAvailableList = JSON.parse(JSON.stringify(this.Quote.ancillaries.available));

                if (this.Quote.haveComprehensive) {
                    secciSelectedList.PMWindscreenUpgradeCov = true;
                    secciAvailableList.PMWindscreenUpgradeCov = {
                        "single" : {
                            "total" : 0
                        },
                        "name" : "Windscreen Cover"
                    };
                }

                let ncbProtectionTotal = 0;
                let ncbProtectionTax = 0;
                let ncbGuaranteeTotal = 0;
                let ncbGuaranteeTax = 0;
                for (let car in this.Quote.cars) {
                    if (this.Quote.cars[car].ancillaries){
                        if (this.Quote.cars[car].ancillaries.selected["PMNCBProtectionCov"]) {
                            ncbProtectionTotal += this.Quote.cars[car].ancillaries.selected["PMNCBProtectionCov"].price;
                            ncbProtectionTax += this.Quote.cars[car].ancillaries.selected["PMNCBProtectionCov"].taxAmount;
                        }
                        if (this.Quote.cars[car].ancillaries.selected["PMNCBGuaranteedCov"]) {
                            ncbGuaranteeTotal += this.Quote.cars[car].ancillaries.selected["PMNCBGuaranteedCov"].price;
                            ncbGuaranteeTax += this.Quote.cars[car].ancillaries.selected["PMNCBGuaranteedCov"].taxAmount;
                        }
                    }
                }
                const ncbProtection = {"content" : formatCurrency(ncbProtectionTotal, this._locale, "£", "GBP"), "name" : "Protected No Claims Bonus"};
                const ncbGuarantee = {"content" : formatCurrency(ncbProtectionTotal, this._locale, "£", "GBP"), "name" : "Guaranteed No Claims Bonus"};
                for (let anc in secciSelectedList) {
                    if (secciSelectedList[anc]) {
                        let content: string;
                        if (secciAvailableList[anc].single.total == 0) {
                            content = "Included";
                        }
                        else {
                            content = formatCurrency(secciAvailableList[anc].single.total, this._locale, "£", "GBP");
                            this.totalAncillariesTax += secciAvailableList[anc].single.tax;
                        }

                        const obj: SecciItemInterface = {
                            name: secciAvailableList[anc].name,
                            content
                        };

                        ancDisplayList.push(obj);
                    }
                }
                if (ncbProtectionTotal > 0){
                    this.totalAncillariesTax += ncbProtectionTax;
                    ancDisplayList.push(ncbProtection);
                }
                if (ncbGuaranteeTotal > 0){
                    this.totalAncillariesTax += ncbGuaranteeTax;
                    ancDisplayList.push(ncbGuarantee);
                }
            }
        }

        if (this.Quote.home){
            if (this.Quote.home.ancillaries !== 'undefined' && this.Quote.home.ancillaries !== null) {
                const secciSelectedList = JSON.parse(JSON.stringify(this.Quote.ancillaries.selected));
                const secciAvailableList = JSON.parse(JSON.stringify(this.Quote.ancillaries.available));

                for (let anc in secciSelectedList){
                    if (secciSelectedList[anc]){
                        const obj: SecciItemInterface = {
                            name: secciAvailableList[anc].name,
                            content: formatCurrency(secciAvailableList[anc].single.total, this._locale, "£", "GBP")
                        };

                        this.totalAncillariesTax += secciAvailableList[anc].single.tax;
                        ancDisplayList.push(obj);
                    }
                }
            }
        }
        return ancDisplayList;
    }

    public getInterestRate(): any {
        const proratedPremium = this.Quote.response.quotePolicyTerm.proratedPremium;
        this.interestRate = ((proratedPremium !== "undefined" && proratedPremium !== null) && (proratedPremium.monthlyPremium)) ? proratedPremium.monthlyPremium.interest : "ERROR";
    }

    public ngOnDestroy(): void {

    }

    private modalHeight() {

    }
}

And the spec file:

import { TestBed, ComponentFixture, async, inject } from "@angular/core/testing";
import { SecciModal } from "./secci-modal.component";
import {NgxSmartModalModule, NgxSmartModalService} from "ngx-smart-modal";
import { LOCALE_ID } from "@angular/core";

fdescribe("Secci Modal component", () => {
    let fixture: ComponentFixture<SecciModal>;
    let component: SecciModal;
    let mockBrand = {
        name: "name",
        ddRejectionFee: 0,
        preCollectionAdminFee: 0,
        postCollectionAdminFee: 0
    };
    let mockQuote = {
        policy: {
            monthly: {
                deposit: 0,
                numberOfPayments: 0,
                totalPremium: 0,
                installmentCharges: 0,
                apr: 0,

            },
            singleDirectDebitCost: 0,
            singleDirectDebitTax: 0
        },
        daysOfCover: 0,
        haveComprehensive: true,
        ancillaries: {
            selected: {
                PMWindscreenUpgradeCov: true
            },
            available: {

            }
        },
        cars: [
            {
                ancillaries: {
                    selected: {
                        PMNCBProtectionCov: {
                            price: 0,
                            taxAmount: 0
                        },
                        PMNCBGuaranteedCov: {
                            price: 0,
                            taxAmount: 0
                        }
                    }
                }
            }
        ],
        home: {
            ancillaries: {
                selected: {

                },
                available: {

                }
            }
        },
        response: {
            quotePolicyTerm: {
                proratedPremium: {
                    monthlyPremium: {
                        interest: 0
                    }
                }
            }
        }
    };
    let mockApi = {

    };
    let mockGlobal = {

    };
    let mockPage = {

    };

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                SecciModal
            ],
            imports: [
                NgxSmartModalModule.forRoot()
            ],
            providers: [
                {
                    provide: "Brand",
                    useValue: mockBrand
                },
                {
                    provide: "Quote",
                    useValue: mockQuote
                },
                {
                    provide: "Api",
                    useValue: mockApi
                },
                {
                    provide: "Global",
                    useValue: mockGlobal
                },
                {
                    provide: "Page",
                    useValue: mockPage
                },
                {
                    provide: LOCALE_ID,
                    useValue: {}
                }
            ]
        })
            .compileComponents()
            .then(() => {
                // create component and test fixture
                fixture = TestBed.createComponent(SecciModal);
                component = fixture.componentInstance;
                fixture.detectChanges();
            });
    }));

    it("secciAncillaries", (done) => {
        expect(true).toBeTruthy();
        done();
    });
});

As I said, I can't really provide any further information as I don't have any idea why this is happening and have never received this error before! Any help would be a plus. Thanks

Aucun commentaire:

Enregistrer un commentaire