import { Injectable } from '@angular/core'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { ApplicationFromApi } from '@core/typings/application.typing'; import { NonprofitDataApi, NonprofitDetail } from '@core/typings/organization.typing'; import { ApplicationStatuses } from '@core/typings/status.typing'; import { SpecialHandling } from '@features/formio/formio-components/standard-formio-components/gc-special-handling/gc-special-handling.component'; import { AddressFromApi, OrganizationEligibleForGivingStatus } from '@yourcause/common'; import { BeforeEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect } from 'chai'; import { SpecialHandlingService } from './special-handling.service'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(SpecialHandlingService, { imports: [ GCMockModule ] }) export class SpecialHandlingServiceSpec implements Spec { defaultAddress: AddressFromApi = { address1: 'Address 1', address2: '', city: 'City', country: 'US', stateProvRegCode: 'SC', stateProvRegName: 'SC', countryCode: 'US', postalCode: '23533' }; applicationFromApi: ApplicationFromApi = { reservedFunds: false, budgetId: null, fundingSourceId: null, applicationGuid: 'abc-123', applicationId: 23532, applicationStatus: ApplicationStatuses.Approved, submittedDate: '', applicantId: 12542, applicantFirstName: 'Lukie', applicantCanReceiveEmails: true, applicantLastName: 'Smith', applicantEmail: 'lukie@mailinator.com', applicantPhoneNumber: '', applicationProfileImageUrl: '', applicantAddress: '', programName: 'Program name', programId: 3456, grantProgramTimezone: 'UTC', organizationId: 56, charityId: 5, organizationName: 'Org name', orgnizationImageUrl: '', organizationIdentification: '12-3415', organizationAddress: '', isPrivateOrg: false, daysInReview: 0, isApplicationInClientUserWorkflowLevel: true, workflowLevelApplicationReview: null, reviewRuleType: null, totalWorkflowLevelReviews: 4, totalWorkflowLevelUsers: 4, majorityReviewed: false, currentWorkflowName: 'WFL name', currentWorkFlowLevelName: 'WF name', parentWorkFlowLevelName: 'Parent WFL name', parentWorkFlowLevelId: 1, canRouteApplication: true, canArchiveAndUnarchiveApplication: true, canApproveApplication: true, canAwardApplication: true, canDeclineApplication: true, canArchiveApplication: true, canExtendFormDueDate: true, canRecommendFunding: true, hasNonPaidPayments: true, workflowLevelUsersWithoutReviews: [], workflowLevelRoutes: [], managerWillComplete: true, isDraft: true, programAllowCollaboration: true, programSendEmailsToCollaborators: true, amountRequested: 500, paymentDesignation: 'payment designation', careOf: null, createdBy: null, lastUpdatedBy: null, updatedDate: '', createdDate: '', nonprofitGuid: 'guid', currentWorkFlowLevelId: 235325, currentWorkFlowId: 567, isArchived: true, nominee: null, nominationApplicationId: 6, canAccessNominationApplication: true, isMasked: true, canViewMaskedApplicantInfo: false, specialHandlingAddress1: 'Address 1', specialHandlingAddress2: 'Address 2', specialHandlingCity: 'City', specialHandlingCountry: 'US', specialHandlingName: 'Special Handling name', specialHandlingPostalCode: '32690', specialHandlingStateProvinceRegion: 'SC', specialHandlingNotes: '', specialHandlingFileUrl: null, specialHandlingReason: '', currencyRequestedAmountEquivalent: 0, currencyRequested: 'USD', inKindAmountRequested: 0, inKindItems: [], isProgramArchived: true, meetsVettingRequirement: true, grantProgramCycle: null, hasAwards: false, hasPayments: false, hasCashAward: false, organizationEligibleForGivingStatus: OrganizationEligibleForGivingStatus.ELIGIBLE, recommendedFundingAmount: 0, orgIsInternational: false, applicantIsSSO: false, currentWorkflowLevelShowBudgetSummaryInfo: true, currentWorkflowLevelAllowUserToViewAwardsAndPayments: true, currentWorkflowLevelAllowAward: true }; nonprofitDetail = { name: 'Org Name', displayAddress: this.defaultAddress, address: this.defaultAddress, remittanceInfo: { address: this.defaultAddress } } as NonprofitDetail; @BeforeEach() mock (service: SpecialHandlingService) { service['nonprofitService']['nonprofitResources'].getNonprofitAdditionalDataByGuid = async () => { return { nonprofitDetail: this.nonprofitDetail, nonprofit: {} as NonprofitDataApi }; }; } @TestCase('should be able to get is special handling valid') isSpecialHandlingValid (service: SpecialHandlingService) { const invalidHandling: SpecialHandling = { name: 'Invalid', address1: '', address2: '', city: 'City', state: 'State', country: '', postalCode: '', notes: '', fileUrl: '', reason: '' }; let valid = service.isSpecialHandlingValid(invalidHandling); expect(valid).to.be.false; const validHandling: SpecialHandling = { name: 'Valid', address1: 'Address1', address2: '', city: 'City', state: 'State', country: 'US', postalCode: '43543', notes: '', fileUrl: '', reason: '' }; valid = service.isSpecialHandlingValid(validHandling); expect(valid).to.be.true; } @TestCase('should be able to get default special handling') async getDefaultSpecialHandling (service: SpecialHandlingService) { const result = await service.getDefaultSpecialHandling( 1, 'guid', 'org name', 353, 'Josie', 'Conway', 'josie.conway@mailinator.com', '' ); expect(result.defaultAddress.address1).to.be.equal(this.defaultAddress.address1); expect(result.defaultAddress.city).to.be.equal(this.defaultAddress.city); expect(result.defaultAddress.postalCode).to.be.equal(this.defaultAddress.postalCode); expect(result.defaultAddress.country).to.be.equal(this.defaultAddress.country); } @TestCase('should be able to get special handling') getSpecialHandling (service: SpecialHandlingService) { const result = service.getSpecialHandling(this.applicationFromApi); expect(result.address1).to.be.equal(this.applicationFromApi.specialHandlingAddress1); expect(result.city).to.be.equal(this.applicationFromApi.specialHandlingCity); expect(result.postalCode).to.be.equal(this.applicationFromApi.specialHandlingPostalCode); } @TestCase('should be able to get has special handling') hasSpecialHandling (service: SpecialHandlingService) { const specialHandling: SpecialHandling = { name: 'Name', address1: 'Test', address2: '', city: 'City', state: 'State', postalCode: '23533', country: 'US', fileUrl: '', reason: '', notes: '' }; let pass = service.hasSpecialHandling(specialHandling); expect(pass).to.be.true; pass = service.hasSpecialHandling({ ...specialHandling, address1: '' }); expect(pass).to.be.false; } @TestCase('should be able to get is special handling same as default') isSameAsDefault (service: SpecialHandlingService) { const specialHandling: SpecialHandling = { name: 'Name', address1: 'Test', address2: '', city: 'City', state: 'State', postalCode: '23533', country: 'US', fileUrl: '', reason: '', notes: '' }; const defaultAddress: SpecialHandling = { name: 'Name', address1: 'Test', address2: '', city: 'City', state: 'State', postalCode: '23533', country: 'US', fileUrl: '', reason: '', notes: '' }; let isSame = service.isSameAsDefault( specialHandling, defaultAddress ); expect(isSame).to.be.true; isSame = service.isSameAsDefault( { ...specialHandling, address1: 'Different' }, defaultAddress ); expect(isSame).to.be.false; } @TestCase('should be able to get special handling for save') getSpecialHandlingForSave (service: SpecialHandlingService) { const specialHandling: SpecialHandling = { name: 'Name', address1: 'Test', address2: '', city: 'City', state: 'State', postalCode: '23533', country: 'US', fileUrl: '', reason: '', notes: '' }; const defaultAddress: SpecialHandling = { name: 'Name', address1: 'Test', address2: '', city: 'City', state: 'State', postalCode: '23533', country: 'US', fileUrl: '', reason: '', notes: '' }; let adapted = service.getSpecialHandlingForSave( specialHandling, defaultAddress ); // Same as default means we don't pass anything to API expect(adapted.specialHandlingAddress1).to.be.equal(''); expect(adapted.specialHandlingCity).to.be.equal(''); expect(adapted.specialHandlingCountry).to.be.equal(''); adapted = service.getSpecialHandlingForSave( { ...specialHandling, address2: 'different' }, defaultAddress ); // Not same as default means we do send to API expect(adapted.specialHandlingAddress1).to.be.equal(specialHandling.address1); expect(adapted.specialHandlingCity).to.be.equal(specialHandling.city); expect(adapted.specialHandlingCountry).to.be.equal(specialHandling.country); } }