import { Injectable } from '@angular/core'; import { GCMockModule } from '@core/mocks/gc-module.mock'; import { UserTypes } from '@core/typings/client-user.typing'; import { PaginatedResponse, TemplateMarginsForUI } from '@yourcause/common'; import { BeforeEach, Spec, TestCase } from '@yourcause/test-decorators'; import { DescribeAngularService } from '@yourcause/test-decorators/angular'; import { expect } from 'chai'; import { DocumentTemplateService } from './document-template.service'; import { DocumentTemplateFromApi, TemplateMarginsFromAPI, URLSasTokenObj } from './document-template.typing'; @Injectable({ providedIn: 'root' }) @DescribeAngularService(DocumentTemplateService, { imports: [ GCMockModule ] }) export class DocumentTemplateServiceSpec implements Spec { mockTemplate = '

↵↵

Words

↵↵

↵'; mockTokenObject: URLSasTokenObj = { urlsAndSasTokens: { ['https://qagrantsconnect.blob.core.windows.net/templates/1034fe38-ac4d-4ad3-a51c-0c3d6de4e166.png']: '?token' } }; mockTemplateWithTokens = '

↵↵

Words

↵↵

↵'; @BeforeEach() prep (service: DocumentTemplateService) { service['documentTemplateResources']['getBulkTemplateFileSASTokens'] = async () => { return this.mockTokenObject; }; } @TestCase('should be able to adapt document templates') async adaptDocumentTemplates (service: DocumentTemplateService) { const paginatedResponse: PaginatedResponse = { records: [{ id: 1, name: 'Grant Agreement', createdDate: '03/03/2020', createdBy: { firstName: 'David', lastName: 'Johnson', email: 'david.johnson@blackbaud.me', userType: UserTypes.MANAGER, impersonatedBy: null } }], recordCount: 1 }; const adapted = service.adaptDocumentTemplates(paginatedResponse.records); expect(adapted[0].statusText).to.not.be.undefined; } @TestCase('should be able to adapt margins') adaptMarginsToAndFromAPI (service: DocumentTemplateService) { const marginsFromAPI: TemplateMarginsFromAPI = { top: '1', bottom: '2', left: '3', right: '4' }; const marginsForUI: TemplateMarginsForUI = { top: 1, bottom: 2, left: 3, right: 4 }; const apiAdaptedToUI = service.adaptTemplateMarginsForUI(marginsFromAPI); const uiAdaptedToAPI = service.adaptTemplateMarginsForAPI(marginsForUI); const eachUIValIsCorrect = Object.keys(marginsForUI).every((key) => { return marginsForUI[key as keyof TemplateMarginsForUI] === apiAdaptedToUI[key as keyof TemplateMarginsForUI]; }); const eachAPIValIsCorrect = Object.keys(marginsFromAPI).every((key) => { return marginsFromAPI[key as keyof TemplateMarginsFromAPI] === uiAdaptedToAPI[key as keyof TemplateMarginsFromAPI]; }); expect(eachUIValIsCorrect).to.be.true; expect(eachAPIValIsCorrect).to.be.true; } @TestCase('should be able to extract GC file urls') extractGCFileURLs (service: DocumentTemplateService) { const imgUrls = service.extractGCFileURLS(this.mockTemplate); expect(imgUrls).to.include('https://qagrantsconnect.blob.core.windows.net/templates/1034fe38-ac4d-4ad3-a51c-0c3d6de4e166.png'); // should not include external img src expect(imgUrls).to.not.include('https://encrypted-tbn0.gstatic.com/images'); // should not include entire upload url, only base url expect(imgUrls).to.not.include('https://qagrantsconnect.blob.core.windows.net/templates/1034fe38-ac4d-4ad3-a51c-0c3d6de4e166.png?sv=2018-03-28&sr=b&sig=kXqfUnIlooZlBa3uhT071A%2BNjDRaDkqGSsESc%2FZyvE4%3D&se=2021-03-17T17%3A32%3A11Z&sp=r'); } @TestCase('should be able to attach tokens to image urls') attachTokensToImageUrls (service: DocumentTemplateService) { const adaptedHtml = service.attachTokensToImages(this.mockTemplate, this.mockTokenObject); expect(adaptedHtml).to.equal(this.mockTemplateWithTokens); } @TestCase('should be able to adapt template images') async adaptTemplateImages (service: DocumentTemplateService) { const adaptedTemplate = await service.adaptTemplateImages(this.mockTemplate, 1); expect(adaptedTemplate).to.equal(this.mockTemplateWithTokens); } }