I'm new to Angular and attempting to test an Angular 6 service which utilizes handlebars.js to build client-side html templates, based on input JSON / data.
The service produces the desired result in development, but I'm unable to build a test confirming the service compiles html correctly. I have base templates (as JavaScript strings) imported directly into the service, but when I attempt to run the compilation method in the .spec file, I receive Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed undefined
, meaning the base template string variables are not being instantiated in the service within the .spec file.
PdfReportService.service.ts
file
import { Injectable } from '@angular/core';
import * as Handlebars from 'handlebars/dist/handlebars.min.js';
// base template
import base_html from './root_template/html';
// form templates
import form_html from './form_template/form.html';
import form_css from './form_template/form.css';
@Injectable({
providedIn: 'root'
})
export class PdfReportService {
...
...
public compileform(json: object, context?: any) {
Handlebars.registerPartial({ hbs_css_template: form_css });
Handlebars.registerPartial({ hbs_form_body: form_html });
return Handlebars.compile(base_html)({ json, context });
}
}
PdfReportService.service.spec.ts
file
import { TestBed, inject } from '@angular/core/testing';
import { PdfReportService } from './pdf-reports.service';
// import test data
import {
formInputJson,
formInputContext,
formCompiledHTML
} from './pdf-reports.service.spec.data';
fdescribe('PdfReportService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [PdfReportService]
});
});
it('should compile the correct html from form input data', inject(
[PdfReportService],
(service: PdfReportService) => {
const result = service.compileform(formInputJson, formInputContext);
expect(result).toEqual(formCompiledHTML);
}
));
});
I've tried altering the service and multiple different methods of passing the vars through the test, but they always remain undefined. Any help would be appreciated!