From 4354131aaeba83fcfb22a576ac3a2f25e62ab5ee Mon Sep 17 00:00:00 2001 From: ssube Date: Sun, 20 Oct 2019 09:52:35 -0500 Subject: [PATCH] fix(tests): cover new transform helpers --- src/transform/TemplateTransform.ts | 10 +++---- test/transform/TestHelpers.ts | 24 ++++++++++++++++- test/transform/TestTemplateTransform.ts | 35 +++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/transform/TemplateTransform.ts b/src/transform/TemplateTransform.ts index 07c58983e..48723bb61 100644 --- a/src/transform/TemplateTransform.ts +++ b/src/transform/TemplateTransform.ts @@ -1,4 +1,5 @@ -import { Inject } from 'noicejs'; +import { isString } from 'lodash'; +import { BaseError, Inject } from 'noicejs'; import { Transform, TransformData } from '.'; import { INJECT_TEMPLATE } from '../BaseService'; @@ -39,11 +40,10 @@ export class TemplateTransform extends BaseTransform impl this.logger.debug({ key, scope }, 'rendering template with scope'); const result = template.render(scope); this.logger.debug({ key, result }, 'rendered template with scope'); - if (Array.isArray(result)) { - out.set(key, result); - } else { - out.set(key, [result]); + if (!isString(result)) { + throw new BaseError('template did not return string'); } + out.set(key, [result]); } return makeDict(out); } diff --git a/test/transform/TestHelpers.ts b/test/transform/TestHelpers.ts index 3ea5172bd..2bdbea4f0 100644 --- a/test/transform/TestHelpers.ts +++ b/test/transform/TestHelpers.ts @@ -1,10 +1,11 @@ import { expect } from 'chai'; import { ineeda } from 'ineeda'; +import { BaseError } from 'noicejs'; import { Command, CommandVerb } from '../../src/entity/Command'; import { Message } from '../../src/entity/Message'; import { InvalidArgumentError } from '../../src/error/InvalidArgumentError'; -import { applyTransforms, Transform } from '../../src/transform'; +import { applyTransforms, extractBody, Transform } from '../../src/transform'; import { entityData } from '../../src/transform/BaseTransform'; import { TYPE_TEXT } from '../../src/utils/Mime'; import { describeLeaks, itLeaks } from '../helpers/async'; @@ -111,3 +112,24 @@ describeLeaks('entity data helper', async () => { expect(() => entityData('test' as any)).to.throw(InvalidArgumentError); }); }); + +describeLeaks('extract body helper', async () => { + /* tslint:disable:no-any */ + itLeaks('should throw if body is not an array', async () => { + expect(() => extractBody({ + body: 'test' as any, + })).to.throw(BaseError); + }); + + itLeaks('should throw if body values are not strings', async () => { + expect(() => extractBody({ + body: [1, 2, 3] as any, + })).to.throw(BaseError); + }); + + itLeaks('should return the body', async () => { + expect(extractBody({ + body: ['test'], + })).to.equal('test'); + }); +}); diff --git a/test/transform/TestTemplateTransform.ts b/test/transform/TestTemplateTransform.ts index e29f8297e..6d70ca91c 100644 --- a/test/transform/TestTemplateTransform.ts +++ b/test/transform/TestTemplateTransform.ts @@ -1,5 +1,6 @@ import { expect } from 'chai'; import { ineeda } from 'ineeda'; +import { BaseError } from 'noicejs'; import { INJECT_TEMPLATE } from '../../src/BaseService'; import { Command, CommandVerb } from '../../src/entity/Command'; @@ -49,4 +50,38 @@ describeLeaks('template transform', async () => { body: [templates.body], }); }); + + itLeaks('should throw when template does not render a string', async () => { + const { container } = await createServiceContainer(); + + const data = { + test: ['1'], + }; + const templates = { + body: 'test_body', + }; + const transform = await createService(container, TemplateTransform, { + [INJECT_TEMPLATE]: ineeda({ + compile: () => ineeda