From 3f6b5b60dc1c163a7dbbeb951b3549079362fd5b Mon Sep 17 00:00:00 2001 From: fletcherist Date: Sun, 12 Aug 2018 20:50:17 +0300 Subject: [PATCH] fix: remove redundant tests & fix type error --- src/api/request.ts | 11 ++--- tests/button.spec.ts | 32 -------------- tests/buttonBuilder.spec.ts | 29 ------------- tests/commands.spec.ts | 12 ------ tests/index.spec.ts | 37 ---------------- tests/reply.spec.ts | 86 ------------------------------------- tests/scene.spec.ts | 72 ------------------------------- tests/testUtils.ts | 4 +- 8 files changed, 5 insertions(+), 278 deletions(-) delete mode 100644 tests/button.spec.ts delete mode 100644 tests/buttonBuilder.spec.ts delete mode 100644 tests/commands.spec.ts delete mode 100644 tests/index.spec.ts delete mode 100644 tests/reply.spec.ts delete mode 100644 tests/scene.spec.ts diff --git a/src/api/request.ts b/src/api/request.ts index b073c1d..8ef9740 100644 --- a/src/api/request.ts +++ b/src/api/request.ts @@ -4,15 +4,10 @@ export interface IApiRequestMeta { client_id: string; } -enum ApiRequestBodyType { - SimpleUtterance = 'SimpleUtterance', - ButtonPressed = 'ButtonPressed', -} - -export interface IApiRequesBody { +export interface IApiRequestBody { command: string; original_utterance: string; - type: ApiRequestBodyType; + type: 'SimpleUtterance' | 'ButtonPressed'; markup?: { dangerous_context?: true; }; @@ -29,7 +24,7 @@ export interface IApiRequesSession { export interface IApiRequest { meta: IApiRequestMeta; - request: IApiRequesBody; + request: IApiRequestBody; session: IApiRequesSession; version: string; } diff --git a/tests/button.spec.ts b/tests/button.spec.ts deleted file mode 100644 index cf7c470..0000000 --- a/tests/button.spec.ts +++ /dev/null @@ -1,32 +0,0 @@ -import button from '../button'; -import ButtonBuilder from '../buttonBuilder'; - -test('create button with string constructor', () => { - const expected = { - title: 'send message', - }; - const btn = button(expected.title); - expect(btn).toEqual(expected); -}); - -test('create button with object constructor', () => { - const expected = { - title: 'show messages', - payload: { test: 'test' }, - hide: true, - }; - const btn = button({ - title: expected.title, - payload: expected.payload, - hide: expected.hide, - }); - - const btn2 = new ButtonBuilder() - .title(expected.title) - .payload(expected.payload) - .shouldHide(expected.hide) - .get(); - - expect(btn).toEqual(expected); - expect(btn2).toEqual(expected); -}); diff --git a/tests/buttonBuilder.spec.ts b/tests/buttonBuilder.spec.ts deleted file mode 100644 index 309bbce..0000000 --- a/tests/buttonBuilder.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -// const ButtonBuilder = require('../buttonBuilder') -import ButtonBuilder from '../buttonBuilder'; - -test('common test for buttonBuilder', () => { - const expected = { - title: 'Some button text', - url: 'https://example.com', - hide: true, - payload: { - some: 'data', - }, - }; - - const button = new ButtonBuilder(); - - // test using button builder factory - button - .title(expected.title) - .url(expected.url) - .shouldHide(expected.hide) - .payload(expected.payload); - - expect(button.get()).toEqual(expected); - - // test using button builder constructor - - const button2 = new ButtonBuilder(expected); - expect(button2.get()).toEqual(expected); -}); diff --git a/tests/commands.spec.ts b/tests/commands.spec.ts deleted file mode 100644 index 97b39c6..0000000 --- a/tests/commands.spec.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Module test for commands class -import Commands from '../commands'; - -test('adding a command', () => { - const commands = new Commands(); - - // Testing a string command - const cb = ctx => 'test'; - - commands.add('Привет, алиса!', cb); - expect(commands.length).toBe(1); -}); diff --git a/tests/index.spec.ts b/tests/index.spec.ts deleted file mode 100644 index 12136d6..0000000 --- a/tests/index.spec.ts +++ /dev/null @@ -1,37 +0,0 @@ -import Alice, { - Scene, - reply, - button, - ReplyBuilder, - ButtonBuilder, - loggerMiddleware, -} from '../../dist/index'; - -test('typescript import modules', () => { - expect(Alice).toBeDefined(); - expect(Scene).toBeDefined(); - expect(reply).toBeDefined(); - expect(button).toBeDefined(); - expect(ReplyBuilder).toBeDefined(); - expect(ButtonBuilder).toBeDefined(); - expect(loggerMiddleware).toBeDefined(); -}); - -test('commonjs require modules', () => { - const AliceCommon = require('../../dist/index'); - const { - Scene, - reply, - button, - ReplyBuilder, - ButtonBuilder, - loggerMiddleware, - } = AliceCommon; - expect(AliceCommon).toBeDefined(); - expect(Scene).toBeDefined(); - expect(reply).toBeDefined(); - expect(button).toBeDefined(); - expect(ReplyBuilder).toBeDefined(); - expect(ButtonBuilder).toBeDefined(); - expect(loggerMiddleware).toBeDefined(); -}); diff --git a/tests/reply.spec.ts b/tests/reply.spec.ts deleted file mode 100644 index 2141272..0000000 --- a/tests/reply.spec.ts +++ /dev/null @@ -1,86 +0,0 @@ -import reply from '../reply' -import { bigImageCard, image, footer } from '../card' -import button from '../button' -import Context from '../context' -import { ALICE_PROTOCOL_VERSION, DEFAULT_END_SESSION } from '../constants' -import { generateRequest } from './testUtils' - -const MOCKED_IMAGE_ID = '213044/2a175da14f91b71df60c' - -test('create reply with string constructor', () => { - const expectedData = { - response: { - text: 'send message', - buttons: [], - end_session: DEFAULT_END_SESSION, - }, - session: null, - version: ALICE_PROTOCOL_VERSION, - } - - const msg = reply(expectedData.response.text) - expect(msg).toEqual(expectedData) -}) - -test('create reply with object constructor', () => { - const expectedData = { - response: { - text: 'send message', - tts: 'send m+essage', - buttons: [], - end_session: true, - }, - session: null, - version: ALICE_PROTOCOL_VERSION, - } - - const msg = reply({ - text: expectedData.response.text, - tts: expectedData.response.tts, - buttons: expectedData.response.buttons, - endSession: expectedData.response.end_session, - }) - expect(msg).toEqual(expectedData) -}) - -test('creating new image', () => { - expect(image(MOCKED_IMAGE_ID)).toEqual({ - image_id: MOCKED_IMAGE_ID, - }) -}) - -test('ctx.replyWithImage string', () => { - const ctx = new Context({ - req: generateRequest('test'), - }) - const mockedImageId = '213044/2a175da14f91b71df60c' - - const res = ctx.replyWithImage(mockedImageId) - expect(res.response.card).toEqual({ - type: 'BigImage', - image_id: mockedImageId, - }) -}) - -test('ctx.replyWithImage object', () => { - const ctx = new Context({ - req: generateRequest('test'), - }) - const mockedBigImage = { - image_id: MOCKED_IMAGE_ID, - title: '1', - description: '1', - button: button('123'), - footer: footer('1', button('123')), - } - - const res = ctx.replyWithImage(mockedBigImage) - expect(res.response.card).toEqual({ - type: 'BigImage', - image_id: MOCKED_IMAGE_ID, - button: button('123'), - title: '1', - description: '1', - footer: footer('1', button('123')), - }) -}) diff --git a/tests/scene.spec.ts b/tests/scene.spec.ts deleted file mode 100644 index abe5c47..0000000 --- a/tests/scene.spec.ts +++ /dev/null @@ -1,72 +0,0 @@ -import Alice from '../alice'; -import Scene from '../scene'; -import { generateRequest } from './testUtils'; - -test('creating scene with name', () => { - const scene = new Scene('testName'); - expect(scene.name).toBe('testName'); -}); - -test('registering an array of scenes', () => { - const alice = new Alice(); - const scene1 = new Scene('scene1'); - const scene2 = new Scene('scene2'); - - alice.registerScene([scene1, scene2]); - - // yup it's a private method but who cares whatsoever?.. - expect(alice.scenes.length).toBe(2); -}); - -test('register scene and enter in', async () => { - const alice = new Alice(); - const scene = new Scene('123'); - scene.enter('1', ctx => ctx.reply('enter')); - scene.any(ctx => ctx.reply('scene-any')); - scene.command('3', ctx => ctx.reply('command')); - scene.leave('2', ctx => ctx.reply('leave')); - - alice.registerScene(scene); - alice.any(ctx => ctx.reply('hi')); - - let res; - res = await alice.handleRequest(generateRequest('hello')); - expect(res.response.text).toBe('hi'); - - res = await alice.handleRequest(generateRequest('1')); - expect(res.response.text).toBe('enter'); - res = await alice.handleRequest(generateRequest('blablabla')); - expect(res.response.text).toBe('scene-any'); - - res = await alice.handleRequest(generateRequest('2')); - expect(res.response.text).toBe('leave'); -}); - -test('changing scene', async () => { - const alice = new Alice(); - const scene1 = new Scene('scene1'); - const scene2 = new Scene('scene2'); - scene1.enter('keyword', ctx => { - ctx.enterScene(scene2); - return ctx.reply('scene1'); - }); - scene2.any(ctx => { - ctx.leaveScene(); - return ctx.reply('scene2'); - }); - - alice.registerScene([scene1, scene2]); - alice.any(ctx => ctx.reply('main')); - - let data; - data = await alice.handleRequest(generateRequest('hello')); - expect(data.response.text).toBe('main'); - // Test scene1 change scene method (ctx.enterScene) - data = await alice.handleRequest(generateRequest('keyword')); - expect(data.response.text).toBe('scene1'); - // Test scene2 leave method - data = await alice.handleRequest(generateRequest('hello')); - expect(data.response.text).toBe('scene2'); - data = await alice.handleRequest(generateRequest('hello')); - expect(data.response.text).toBe('main'); -}); diff --git a/tests/testUtils.ts b/tests/testUtils.ts index f95136d..6882099 100644 --- a/tests/testUtils.ts +++ b/tests/testUtils.ts @@ -1,5 +1,5 @@ -// import { WebhookRequest } from '../yandex/response'; -export const request = (commandText, utteranceText?): WebhookRequest => ({ +import { IApiRequest } from '../src/api/request'; +export const request = (commandText, utteranceText?): IApiRequest => ({ meta: { client_id: 'Developer Console', locale: 'ru-RU',