import { Injectable } from '@angular/core'; import { ComponentFixture } from '@angular/core/testing'; import { FormGroup, FormGroupDirective } from '@angular/forms'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { BaseApplication } from '@core/typings/application.typing'; import { FormDefinitionComponent } from '@features/configure-forms/form.typing'; import { GcFormRendererComponent } from '@features/formio/form-renderer/gc-form-renderer/gc-form-renderer.component'; import { CurrencyRadioOptions, CurrencyValue } from '@yourcause/common/masking'; import { AfterEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularComponent } from '@yourcause/test-decorators/angular'; import { expect, spy } from 'chai'; import { GcAmountRequestedComponent } from './gc-amount-requested.component'; const formGroupDirective = new FormGroupDirective([], []); formGroupDirective.form = new FormGroup({ }); @Injectable({ providedIn: 'root' }) @DescribeAngularComponent(GcAmountRequestedComponent, { imports: [ GCMockModule ], providers: [{ provide: FormGroupDirective, useValue: formGroupDirective }, { provide: GcFormRendererComponent, useValue: { onFormGroupReady () {} } }] }) export class GcAmountRequestedComponentSpec implements Spec< GcAmountRequestedComponentSpec, ComponentFixture > { component = { type: 'amountRequested', key: 'amount', customCurrency: 'CAD', useCustomCurrency: CurrencyRadioOptions.USE_ANY_CURRENCY, validate: { custom: '' } } as FormDefinitionComponent; parentFields: Partial = { amountRequested: 500, currencyRequested: 'CAD', currencyRequestedAmountEquivalent: 400 }; data: CurrencyValue = { amountEquivalent: 400, amountInDefaultCurrency: 500, amountForControl: 400, currency: 'CAD' }; private initiate ( instance: GcAmountRequestedComponent, component = this.component, parentFields = this.parentFields, data = this.data, forceDefaultCurrency = false, useCustomCurrency = this.component.useCustomCurrency ) { instance['clientSettingsService']['set']('defaultCurrency', 'USD'); instance.formioComponent = component; instance.parentFields = parentFields; instance.data = data; instance.currencyOptions = [{ label: 'USD', value: 'USD' }, { label: 'CAD', value: 'CAD' }]; instance.forceDefaultCurrency = forceDefaultCurrency; instance.label = 'Amount label'; instance.useCustomCurrency = useCustomCurrency; spy.on(instance['validatorService'], 'getValidatorsForSimpleComponent', () => { return []; }); spy.on(instance, 'setFormGroup'); instance.ngOnInit(); } @AfterEach() reset () { spy.restore(); } @TestCase('should render') testShouldRender ({ componentInstance: instance }: ComponentFixture) { expect(instance).to.exist; } @TestCase('should set validators on init') setValidatorsAndFormGroupOnInit ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); expect(instance['validatorService']['getValidatorsForSimpleComponent']).to.have.been.called.once; expect(instance['setFormGroup']).to.have.been.called.once; } @TestCase('should be able to patch value') dataChangeShouldPatchValue ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); spy.on(instance, 'patchValue'); spy.on(instance, 'amountChanged'); // update control amount to 650 instance.data = { amountEquivalent: 400, amountInDefaultCurrency: 500, currency: 'CAD', amountForControl: 650 }; // "data" is handled through a getter/setter. The setter should call "patchValue" expect(instance['patchValue']).to.have.been.called.once; expect(instance['amountChanged']).to.have.been.called.once; } @TestCase('amount changed should emit') amountChanged ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); spy.on(instance['onValueChange'], 'emit'); instance.amountChanged(); expect(instance['onValueChange']['emit']).to.have.been.called.once; } @TestCase('currency changed should emit') currencyChanged ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); spy.on(instance['onValueChange'], 'emit'); instance.onCurrencyChange('USD'); expect(instance['onValueChange']['emit']).to.have.been.called.once; } @TestCase('can access default currency') defaultCurrency ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); expect(instance.defaultCurrency).to.be.equal('USD'); } @TestCase('can access currency - dont force default') currency ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); // force default currency is false, so use currency from this.data expect(instance.currency).to.be.equal(this.data.currency); } @TestCase('can access currency - force default') currencyForceDefault ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); instance.forceDefaultCurrency = true; // force default currency is true, so use currency client settings expect(instance.currency).to.be.equal('USD'); } @TestCase('can access currency - dont force default') equivalentCurrency ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); expect(instance.equivalentCurrency).to.be.equal('USD'); } @TestCase('can access currency - force default') equivalentCurrencyForceDefault ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); instance.forceDefaultCurrency = true; expect(instance.equivalentCurrency).to.be.equal(this.data.currency); } @TestCase('can access currency - dont force default') equivalentAmount ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); expect(instance.equivalentAmount).to.be.equal(this.data.amountInDefaultCurrency); } @TestCase('can access currency - force default') equivalentAmountForceDefault ({ componentInstance: instance }: ComponentFixture) { this.initiate(instance); instance.forceDefaultCurrency = true; expect(instance.equivalentAmount).to.be.equal(this.data.amountEquivalent); } }