Skip to content

Commit

Permalink
fix(tests): cover new transform helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Oct 20, 2019
1 parent 1cf1d3b commit 4354131
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/transform/TemplateTransform.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -39,11 +40,10 @@ export class TemplateTransform extends BaseTransform<TemplateTransformData> 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);
}
Expand Down
24 changes: 23 additions & 1 deletion test/transform/TestHelpers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
});
});
35 changes: 35 additions & 0 deletions test/transform/TestTemplateTransform.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<TemplateCompiler>({
compile: () => ineeda<Template>({
render: () => [],
}),
}),
data: {
filters: [],
strict: true,
templates,
},
metadata: {
kind: 'template-transform',
name: 'test_transform',
},
});
return expect(transform.transform(new Command({
context: ineeda<Context>(),
data: {},
labels: {},
noun: 'test',
verb: CommandVerb.Get,
}), TYPE_JSON, data)).to.eventually.be.rejectedWith(BaseError);
});
});

0 comments on commit 4354131

Please sign in to comment.