import { Injectable } from '@angular/core'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { ApplicantFromSearch } from '@core/typings/applicant.typing'; import { OfflineProgramDetail } from '@core/typings/program.typing'; import { CyclesUI } from '@core/typings/ui/cycles.typing'; import { VettingInfo } from '@features/offline-grants/offline-grants-create-edit-application/organization/organization.component'; import { SearchResult } from '@yourcause/common'; import { BeforeEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect } from 'chai'; import { OfflineGrantsService } from './offline-grants.service'; import { CreateEditApplicationModalResponse, OfflineCreateModalAction, SubmitApplicationOptions } from './offline-grants.typing'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(OfflineGrantsService, { imports: [ GCMockModule ] }) export class OfflineGrantsServiceSpec implements Spec { existingApplicantId = 1; newApplicantId = 2; existingOrgId = 3; existingApplicant: ApplicantFromSearch = { id: this.existingApplicantId, firstName: 'Tina', lastName: 'Conway', email: 'tina@mailinator.com', phoneNumber: '', address: { address: '5516 Alexis Court', address2: '', city: 'North Charleston', state: 'SC', postalCode: '29406', country: 'US' }, notifyApplicant: true }; newApplicant: ApplicantFromSearch = { id: null, firstName: 'Tina', lastName: 'Conway', email: 'tina@mailinator.com', phoneNumber: '', address: { address: '5516 Alexis Court', address2: '', city: 'North Charleston', state: 'SC', postalCode: '29406', country: 'US' }, notifyApplicant: true }; existingOrg = { text: 'Darkness to Light', document: { name: 'Darkness to Light', id: this.existingOrgId + '' } } as SearchResult; modalResponse: CreateEditApplicationModalResponse = { program: { } as OfflineProgramDetail, cycle: { isClientProcessing: true } as CyclesUI.ProgramCycle, grantProgramId: 1, cycleId: 2, selectedApplicant: this.existingApplicant, selectedOrg: this.existingOrg, applicantCanReceiveEmails: true, vettingInfo: {} as VettingInfo, modalAction: OfflineCreateModalAction.SAVE_AND_EDIT }; @BeforeEach() mock (service: OfflineGrantsService) { service['applicantService']['applicantPortalResources'].createApplicant = async () => { return { applicantId: this.newApplicantId, userId: null }; }; } @TestCase('should be able to get applicant ID when they exist') async getApplicantIdExisting (service: OfflineGrantsService) { const applicantId = await service.getApplicantId( this.existingApplicant, true ); expect(applicantId).to.be.equal(this.existingApplicantId); } @TestCase('should be able to get applicant ID when they do not exist') async getApplicantIdNew (service: OfflineGrantsService) { const applicantId = await service.getApplicantId( this.newApplicant, true ); expect(applicantId).to.be.equal(this.newApplicantId); } @TestCase('should be able to get organization ID for existing') async getOrganizationId (service: OfflineGrantsService) { const response = await service.getOrganizationId( this.modalResponse, null ); expect(response.organizationId).to.be.equal(this.existingOrgId); } @TestCase('should be able to show correct toastr on create edit app for success') getCreateEditAppToastrTextSuccess (service: OfflineGrantsService) { const success = true; const isCreate = true; let modalAction = OfflineCreateModalAction.SEND_TO_APPLICANT; let text = service.getCreateEditAppToastrText( success, isCreate, modalAction, false, 1 ); let expectedText = 'Application 1 created and sent to applicant.'; expect(text).to.be.equal(expectedText); modalAction= OfflineCreateModalAction.SAVE_AS_DRAFT; text = service.getCreateEditAppToastrText( success, isCreate, modalAction, false, 1 ); expectedText = 'Application 1 saved as draft.'; expect(text).to.be.equal(expectedText); modalAction = OfflineCreateModalAction.SAVE_AND_EDIT; text = service.getCreateEditAppToastrText( success, isCreate, modalAction, false, 1 ); expectedText = 'Application saved. Make changes to the default form and send to the applicant or submit on their behalf.'; expect(text).to.be.equal(expectedText); } @TestCase('should be able to show correct toastr on create edit app for fail') getCreateEditAppToastrTextFail (service: OfflineGrantsService) { const success = false; let isCreate = false; const modalAction = OfflineCreateModalAction.SEND_TO_APPLICANT; let text = service.getCreateEditAppToastrText( success, isCreate, modalAction, false, 1 ); let expectedText = 'There was an error updating the application.'; expect(text).to.be.equal(expectedText); isCreate = true; text = service.getCreateEditAppToastrText( success, isCreate, modalAction, false, 1 ); expectedText = 'There was an error creating the application.'; expect(text).to.be.equal(expectedText); } @TestCase('should be able to get submit options') getSubmitOptions (service: OfflineGrantsService) { const options = service.getSubmitOptions(false); expect(options.length).to.be.equal(2); const foundEnterDefault = options.some((opt) => { return opt.value === SubmitApplicationOptions.ENTER_DEFAULT_LEVEL; }); expect(foundEnterDefault).to.be.true; const foundSelectWfl = options.some((opt) => { return opt.value === SubmitApplicationOptions.SELECT_A_LEVEL; }); expect(foundSelectWfl).to.be.true; } }