import { createResponse } from 'node-mocks-http'; import { joinFilePath } from '../../../../src'; import type { HttpResponse } from '../../../../src'; import { RenderEjsHandler } from '../../../../src/server/util/RenderEjsHandler'; describe('RenderEjsHandler', (): void => { let response: HttpResponse; let templatePath: string; let templateFile: string; beforeEach((): void => { response = createResponse() as HttpResponse; templatePath = joinFilePath(__dirname, '../../../assets/idp'); templateFile = 'testHtml.ejs'; }); it('throws an error if the path is not valid.', async(): Promise => { const handler = new RenderEjsHandler<{ message: string }>('/bad/path', 'badFile.thing'); await expect(handler.handle({ response, props: { message: 'cool', }, })).rejects.toThrow(`ENOENT: no such file or directory, open '/bad/path/badFile.thing'`); }); it('throws an error if valid parameters were not provided.', async(): Promise => { const handler = new RenderEjsHandler(templatePath, templateFile); await expect(handler.handle({ response, props: 'This is an invalid prop.', })).rejects.toThrow(); }); it('successfully renders a page.', async(): Promise => { const handler = new RenderEjsHandler<{ message: string }>(templatePath, templateFile); await handler.handle({ response, props: { message: 'cool', }, }); // Cast to any because mock-response depends on express, which this project doesn't have const testResponse = response as any; expect(testResponse._isEndCalled()).toBe(true); expect(testResponse._getData()).toBe('

cool

'); expect(testResponse._getStatusCode()).toBe(200); }); it('successfully escapes html input.', async(): Promise => { const handler = new RenderEjsHandler<{ message: string }>(templatePath, templateFile); await handler.handle({ response, props: { message: '', }, }); // Cast to any because mock-response depends on express, which this project doesn't have const testResponse = response as any; expect(testResponse._isEndCalled()).toBe(true); expect(testResponse._getData()).toBe('

<script>alert(1)</script>

'); expect(testResponse._getStatusCode()).toBe(200); }); it('successfully renders when no props are needed.', async(): Promise => { const handler = new RenderEjsHandler(templatePath, 'noPropsTestHtml.ejs'); await handler.handle({ response, props: undefined, }); // Cast to any because mock-response depends on express, which this project doesn't have const testResponse = response as any; expect(testResponse._isEndCalled()).toBe(true); expect(testResponse._getData()).toBe('

secret message

'); expect(testResponse._getStatusCode()).toBe(200); }); });