Permalink
Browse files

Unit tests for core

  • Loading branch information...
justinwilaby committed Oct 1, 2018
1 parent 41bafda commit 39c056e8545efc051d9c1d6a3bdc8a8c7af5baa9
View
@@ -16,6 +16,7 @@
"sourceMaps": "inline",
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-react-jsx"
"@babel/plugin-transform-react-jsx",
"@babel/plugin-transform-runtime"
]
}
View
@@ -1,11 +1,14 @@
const fs = require('fs-extra');
const path = require('path');
const babelOptions = fs.readJsonSync('.babelrc');
const babelOptions = fs.readJsonSync(path.join(__dirname, '.babelrc'));
const transformer = require('babel-jest');
const { createTransformer } = transformer;
const thisTransformer = createTransformer(babelOptions);
Object.assign(transformer, thisTransformer);
transformer.createTransformer = () => thisTransformer;
transformer.createTransformer = () => {
return thisTransformer
};
module.exports = transformer;
@@ -12,7 +12,6 @@
"@babel/preset-typescript"
],
"ignore": [
"**/*.spec.ts",
"**/*.d.ts"
],
"sourceMaps": "inline",
@@ -40,7 +40,10 @@ const defaultConfig = {
test: /\.(tsx?)|(jsx)$/,
exclude: [/node_modules/],
use: {
loader: 'babel-loader'
loader: 'babel-loader',
options: {
ignore: ['**/*.spec.tsx?']
}
}
},
{
@@ -11,8 +11,7 @@
"@babel/preset-typescript"
],
"ignore": [
"**/*.d.ts",
"**/*.spec.ts"
"**/*.d.ts"
],
"sourceMaps": "inline",
"plugins": [
@@ -8,7 +8,7 @@
"homepage": "https://github.com/Microsoft/BotFramework-Emulator",
"scripts": {
"build": "run-s lint build:electron",
"build:electron": "babel ./src --out-dir app/server --extensions \".ts,.tsx\" && gulp copy-extension-stubs",
"build:electron": "babel ./src --out-dir app/server --extensions \".ts,.tsx\" --ignore \"**/*.spec.ts\" && gulp copy-extension-stubs",
"lint": "tslint --project tsconfig.json",
"start": "concurrently --kill-others --names \"electron,react-app\" --success first \"npm run start:electron:dev\" \"npm run start:react-app\"",
"start:electron": "./node_modules/.bin/electron --inspect=7777 .",
@@ -11,8 +11,7 @@
"@babel/preset-typescript"
],
"ignore": [
"**/*.d.ts",
"**/*.spec.ts"
"**/*.d.ts"
],
"sourceMaps": "inline",
"plugins": [
@@ -11,7 +11,7 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "babel ./src --out-dir lib --extensions \".ts,.tsx\"",
"build": "babel ./src --out-dir lib --extensions \".ts,.tsx\" --ignore \"**/*.spec.ts\"",
"clean": "rimraf lib",
"start": "node-dev .",
"test": "jest",
@@ -11,7 +11,6 @@
"@babel/preset-typescript"
],
"ignore": [
"**/*.spec.ts",
"**/*.d.ts"
],
"plugins": [
@@ -8,7 +8,7 @@
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "babel ./src --out-dir lib --extensions \".ts,.tsx\"",
"build": "babel ./src --out-dir lib --extensions \".ts,.tsx\" --ignore \"**/*.spec.ts\"",
"build:watch": "npm run build:prod --outdir lib --watch",
"clean": "rimraf lib",
"lint": "tslint --project tslint.json",
@@ -35,9 +35,12 @@
"homepage": "https://github.com/microsoft/botframework-localmode#readme",
"jest": {
"transform": {
"^.+\\.(tsx?|jsx?)$": "babel-jest"
"^.+\\.(tsx?)$": "babel-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"testURL": "http://localhost",
"testMatch": [
"**/?(*.)(spec|test).(ts)?(x)"
],
"moduleFileExtensions": [
"ts",
"tsx",
@@ -0,0 +1,153 @@
import getAttachment from './getAttachment';
import Attachments from '../../facility/attachments';
import * as HttpStatus from 'http-status-codes';
import getAttachmentInfo from './getAttachmentInfo';
describe('The getAttachment middleware', () => {
let facilities;
let attachments;
let attachmentId;
beforeEach(() => {
attachments = new Attachments();
attachmentId = attachments.uploadAttachment({
name: 'an attachment',
originalBase64: 'aGk=',
type: 'application/text',
thumbnailBase64: 'aGk='
});
facilities = {
attachments
};
});
it('should get the specified attachment', () => {
const getAttachmentMiddleware = getAttachment({ facilities } as any);
const req = {
params: {
viewId: 'thumbnail',
attachmentId
}
};
const res = {
send: () => null,
contentType: ''
};
const sendSpy = jest.spyOn(res, 'send');
getAttachmentMiddleware(req as any, res as any, function () {
return null;
} as any);
expect(sendSpy).toHaveBeenCalledWith(HttpStatus.OK, Buffer.from('aGk=', 'base64'));
expect(res.contentType).toBe('application/text');
});
it('should send an error response when the "originalBase64" and "thumbnailBase64" are falsy', () => {
const getAttachmentMiddleware = getAttachment({ facilities } as any);
(attachments as any).attachments[attachmentId].originalBase64 = undefined;
(attachments as any).attachments[attachmentId].thumbnailBase64 = undefined;
const req = {
params: {
viewId: 'thumbnail',
attachmentId
}
};
const res = {
send: () => null,
end: () => null,
contentType: ''
};
const sendSpy = jest.spyOn(res, 'send');
getAttachmentMiddleware(req as any, res as any, function () {
return null;
} as any);
expect(sendSpy).toHaveBeenCalledWith(HttpStatus.NOT_FOUND, {
'error': {
'code': 'BadArgument',
'message': 'There is no thumbnail view'
}
});
expect(res.contentType).toBe('');
});
});
describe('the getAttachmentInfo middleware', () => {
let facilities;
let attachments;
let attachmentId;
beforeEach(() => {
attachments = new Attachments();
attachmentId = attachments.uploadAttachment({
name: 'an attachment',
originalBase64: 'aGk=',
type: 'application/text',
thumbnailBase64: 'aGk='
});
facilities = {
attachments
};
});
it('should get the attachment info when a valid request is made', () => {
const getAttachmentInfoMiddleware = getAttachmentInfo({ facilities } as any);
const req = {
params: {
viewId: 'thumbnail',
attachmentId
}
};
const res = {
send: () => null,
end: () => null,
contentType: ''
};
const sendSpy = jest.spyOn(res, 'send');
getAttachmentInfoMiddleware(req as any, res as any, function () {
return null;
} as any);
expect(sendSpy).toHaveBeenCalledWith(HttpStatus.OK, {
'name': 'an attachment',
'type': 'application/text',
'views': [{ 'size': 2, 'viewId': 'original' }, { 'size': 2, 'viewId': 'thumbnail' }]
});
});
it('should send an error response when the attachment is not found', () => {
const getAttachmentInfoMiddleware = getAttachmentInfo({ facilities } as any);
const req = {
params: {
viewId: 'thumbnail',
attachmentId: 'not there'
}
};
const res = {
send: () => null,
end: () => null,
contentType: ''
};
const sendSpy = jest.spyOn(res, 'send');
getAttachmentInfoMiddleware(req as any, res as any, function () {
return null;
} as any);
expect(sendSpy).toHaveBeenCalledWith(HttpStatus.NOT_FOUND, {
'error': {
'code': 'BadArgument',
'message': 'attachment[not there] not found'
}
});
});
});
@@ -32,17 +32,17 @@
//
import * as HttpStatus from 'http-status-codes';
import * as Restify from 'restify';
import BotEmulator from '../../botEmulator';
import createAPIException from '../../utils/createResponse/apiException';
import ErrorCodes from '../../types/errorCodes';
import AttachmentData from '../../types/attachment/data';
import AttachmentParams from '../attachmentParams';
import sendErrorResponse from '../../utils/sendErrorResponse';
import { Next, Request, Response } from 'restify';
export default function getAttachment(bot: BotEmulator) {
return (req: Restify.Request, res: Restify.Response, next: Restify.Next): any => {
return (req: Request, res: Response, next: Next): any => {
try {
const parms: AttachmentParams = req.params;
const attachment: AttachmentData = bot.facilities.attachments.getAttachmentData(parms.attachmentId);
@@ -52,7 +52,7 @@ export default function getAttachment(bot: BotEmulator) {
const attachmentBase64 = parms.viewId === 'original' ? attachment.originalBase64 : attachment.thumbnailBase64;
if (attachmentBase64) {
const buffer = new Buffer(attachmentBase64, 'base64');
const buffer = Buffer.from(attachmentBase64, 'base64');
res.contentType = attachment.type;
res.send(HttpStatus.OK, buffer);
@@ -0,0 +1,51 @@
import BotState from '../../facility/botState';
import ConversationSet from '../../facility/conversationSet';
import BotEndpoint from '../../facility/botEndpoint';
import deleteStateForUser from './deleteStateForUser';
import * as HttpStatus from 'http-status-codes';
import Conversation from '../../facility/conversation';
import BotEmulator from '../../botEmulator';
import User from '../../types/user';
describe('The botStateMiddleware', () => {
let botState: BotState;
let emulator: BotEmulator;
let conversation: Conversation;
let user: User;
beforeEach(() => {
emulator = { facilities: { logger: { logMessage: () => true } } } as any;
emulator.facilities.conversations = new ConversationSet();
user = { id: '321', name: 'a user' };
const endpoint = new BotEndpoint('12', '123', 'http://localhost:12345', '', '', false, {});
conversation = emulator.facilities.conversations.newConversation(emulator, endpoint, user);
botState = new BotState(emulator, 256);
botState.setBotData('3c', conversation.conversationId, user.id, { eTag: 'tag', data: {} });
emulator.facilities.botState = botState;
});
it('should delete the state for the user', () => {
const deleteStateMiddleware = deleteStateForUser(emulator);
const req = {
params: { userId: user.id },
accepts: 'application/json',
acceptsEncoding: 'utf8'
};
const res = {
send: () => null,
end: () => null,
contentType: ''
};
const sendSpy = jest.spyOn(res, 'send');
deleteStateMiddleware(req as any, res as any, (() => null) as any);
expect(sendSpy).toHaveBeenCalledWith(HttpStatus.OK);
expect(emulator.facilities.botState.getBotData('3c', conversation.conversationId, user.id)).toEqual({
'data': null,
'eTag': '*'
});
});
});
@@ -12,7 +12,6 @@
"@babel/preset-typescript"
],
"ignore": [
"**/*.spec.ts",
"**/*.d.ts"
],
"sourceMaps": "inline",
@@ -25,7 +25,10 @@ module.exports = {
test: /\.(tsx?)|(jsx)$/,
exclude: [/node_modules/],
use: {
loader: 'babel-loader'
loader: 'babel-loader',
"options": {
"ignore": "**/*.spec.tsx?"
}
}
},
],
@@ -11,6 +11,9 @@
],
"@babel/preset-typescript"
],
"ignore": [
"**/*.d.ts"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/plugin-transform-react-jsx",
@@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"build": "npm run clean && babel ./src --out-dir built --extensions \".ts,.tsx\"",
"build": "npm run clean && babel ./src --out-dir built --extensions \".ts,.tsx\" --ignore \"**/*.spec.ts\"",
"clean": "rimraf ./built",
"prepare": "npm run clean",
"test": "jest"
@@ -12,7 +12,6 @@
"@babel/preset-typescript"
],
"ignore": [
"**/*.spec.ts",
"**/*.d.ts"
],
"sourceMaps": "inline",
Oops, something went wrong.

0 comments on commit 39c056e

Please sign in to comment.