import { Injectable } from '@angular/core'; import { CURRENCY_SERVICE_MOCK } from '@core/mocks/currency.service.mock'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { CurrencySetting } from '@core/typings/api/admin-client.typing'; import { CurrencyRadioOptions } from '@yourcause/common/masking'; import { BeforeEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect } from 'chai'; import { CurrencyService } from './currency.service'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(CurrencyService, { imports: [ GCMockModule ], providers: [ CURRENCY_SERVICE_MOCK ] }) export class CurrencyServiceSpec implements Spec { selectedCurrencies: CurrencySetting[] = [{ code: 'EUR', displayName: 'EUR', default: false }, { code: 'USD', displayName: 'USD', default: true }]; @BeforeEach() async mock (service: CurrencyService) { service['clientSettingsService'].setAllCurrencies(); service['clientSettingsService']['clientSettingsResources'].getAvailableApplicantCurrencies = async () => { return this.selectedCurrencies; }; service['clientSettingsService']['clientSettingsResources'].getSelectedCurrencies = async () => { return this.selectedCurrencies; }; service['clientSettingsService']['userService'].setLastSelectedCurrency('INR'); await service['clientSettingsService'].setCurrencySettings(); await service['clientSettingsService'].setAvailableApplicantCurrencies(1); } @TestCase('should be able to get currency options from component - as manager') getCurrencyOptionsForComponentAsManager (service: CurrencyService) { const options = service.getCurrencyOptionsForComponent('EUR', CurrencyRadioOptions.USE_ANY_CURRENCY, ''); const hasCorrectOptions = options.every((opt) => { return opt.value === 'USD' || opt.value === 'EUR'; }); expect(hasCorrectOptions).to.be.true; const optionsAreSorted = options[0].label === 'EUR'; expect(optionsAreSorted).to.be.true; } @TestCase('should be able to get currency options from component - as applicant') getCurrencyOptionsForComponentAsApplicant (service: CurrencyService) { const options = service.getCurrencyOptionsForComponent('CAD', CurrencyRadioOptions.USE_ANY_CURRENCY, ''); const addedMyOption = options.some((opt) => { return opt.value === 'CAD'; }); expect(addedMyOption).to.be.true; const optionsAreSorted = options[0].label === 'CAD'; expect(optionsAreSorted).to.be.true; } @TestCase('should be able to get currency options from component - no currencies') async getCurrencyOptionsForComponentNoCurrencies (service: CurrencyService) { service['clientSettingsService']['clientSettingsResources'].getSelectedCurrencies = async () => { return []; }; await service['clientSettingsService'].setCurrencySettings(); const options = service.getCurrencyOptionsForComponent('', CurrencyRadioOptions.USE_ANY_CURRENCY, ''); const hasOnlyUsd = options.length === 1 && options[0].value === 'USD'; expect(hasOnlyUsd).to.be.true; } @TestCase('should be able to get currency options from component - custom currency') async getCurrencyOptionsForComponentCustomCurrency (service: CurrencyService) { const options = service.getCurrencyOptionsForComponent('CAD', CurrencyRadioOptions.USE_ONE_CURRENCY, ''); const addedMyOption = options.some((opt) => { return opt.value === 'CAD'; }); expect(addedMyOption).to.be.true; const optionsAreSorted = options[0].label === 'CAD'; expect(optionsAreSorted).to.be.true; } }