Skip to content

Commit

Permalink
feat: implement tests on text reply with additions
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Aug 14, 2018
1 parent 03d3f19 commit 791619c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/api/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface IApiResponseBody {
tts?: string;
card?: IApiResponseCard;
buttons?: IApiResponseBodyButton[];
end_session?: boolean;
end_session: boolean;
}

export interface IApiResponseSession {
Expand Down
5 changes: 4 additions & 1 deletion src/reply/extraParamsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { IApiResponseBodyButton } from '../api/response';
import { BodyButtonDeclaration } from './bodyButtonBuilder';

export interface IExtraParams {
tts?: string;
buttons?: IApiResponseBodyButton[];
end_session?: boolean;
}

export interface IExtraParamsReply {
tts?: string;
buttons?: BodyButtonDeclaration[];
end_session?: boolean;
}
Expand All @@ -19,7 +21,8 @@ export class ExtraParamsBuilder {
): IExtraParams {
if (typeof declaration === 'object') {
return {
end_session: declaration.end_session,
end_session: Boolean(declaration.end_session),
tts: declaration.tts,
};
}

Expand Down
10 changes: 8 additions & 2 deletions src/reply/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ export class Reply {
const result: IApiResponseBody = {
text: textReply.text,
tts: textReply.tts,
end_session: false,
};
if (extraParamsDeclaration) {
const extraParams = ExtraParamsBuilder.createExtraParams(
extraParamsDeclaration,
);
if (extraParams.tts) {
result.tts = extraParams.tts;
}
if (extraParams.buttons) {
result.buttons = extraParams.buttons;
}
if (extraParams.end_session) {
if (typeof extraParams.end_session !== 'undefined') {
result.end_session = extraParams.end_session;
}
}
Expand All @@ -48,6 +52,7 @@ export class Reply {
text: textReply.text,
tts: textReply.tts,
card: card,
end_session: false,
};
if (extraParamsDeclaration) {
const extraParams = ExtraParamsBuilder.createExtraParams(
Expand All @@ -56,7 +61,7 @@ export class Reply {
if (extraParams.buttons) {
result.buttons = extraParams.buttons;
}
if (extraParams.end_session) {
if (typeof extraParams.end_session !== 'undefined') {
result.end_session = extraParams.end_session;
}
}
Expand All @@ -74,6 +79,7 @@ export class Reply {
text: textReply.text,
tts: textReply.tts,
card: card,
end_session: false,
};
if (extraParamsDeclaration) {
const extraParams = ExtraParamsBuilder.createExtraParams(
Expand Down
5 changes: 4 additions & 1 deletion src/reply/textReplyBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export class TextReplyBuilder {
}

if (typeof declaration === 'string') {
return { text: declaration };
return {
text: declaration,
tts: declaration,
};
}

throw new Error(
Expand Down
30 changes: 30 additions & 0 deletions tests/reply.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Reply } from '../dist';
import { getRandomText } from './testUtils';

describe('Alice Reply (static method) suite', () => {
let text = '';
beforeEach(() => {
text = getRandomText();
});
test('Text reply', () => {
expect(Reply.text(text)).toEqual({
text: text,
tts: text,
end_session: false,
});
});
test('Text reply with extra params', () => {
const expected = {
text: text,
tts: text + '+',
end_session: true,
};
expect(
Reply.text(expected.text, { tts: expected.tts, end_session: true }),
).toEqual({
text: text,
tts: expected.tts,
end_session: true,
});
});
});

0 comments on commit 791619c

Please sign in to comment.