Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract expectations #11

Merged
merged 1 commit into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@
"chai": "^4.0.2",
"chai-samsam": "^0.0.2",
"colors": "^1.1.2",
"global": "^4.3.2",
"mocha": "^3.4.2"
"global": "^4.3.2"
},
"devDependencies": {
"@types/bluebird": "^3.5.8",
"@types/chai": "^4.0.1",
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.25",
"@types/node": "^8.0.26",
"ts-node": "^3.1.0",
"tslint": "^5.4.3",
"typescript": "^2.4.1"
"typescript": "^2.4.1",
"mocha": "^3.4.2"
},
"files": [
"dist",
Expand Down
43 changes: 43 additions & 0 deletions src/ChaiExpectation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as chai from 'chai';
import * as chaiSamSam from 'chai-samsam';
import { Expectation } from './Expectation';

// chai.use(chaiSamSam);/
type DeepMatchReturn = { to: {
deep: {
match(arg: {}): {}
}
}};

// type hack to get the ts to be happy
type DeepMatch = (args: {}, errMsg?: string) => DeepMatchReturn;

export class ChaiExpectation implements Expectation {
private chaiModule: typeof chai;

constructor(private subject: {}, private message?: string) {
this.chaiModule = require('chai');
}

public notToBeEmpty() {
this.chaiModule.expect(this.subject, this.message).not.to.be.empty;
}

public toEqual(value: {}) {
this.chaiModule.expect(this.subject, this.message).to.equal(value);
}

public toInclude(value: {}) {
this.chaiModule.expect(this.subject, this.message).to.include(value);
}

public toBeTrue() {
this.chaiModule.expect(this.subject, this.message).to.be.true;
}

public toDeeplyInclude(value: {}) {
const expectWithDeepMatch: DeepMatch = this.chaiModule.expect as any;

expectWithDeepMatch(this.subject).to.deep.match(value);
}
}
24 changes: 24 additions & 0 deletions src/Expectation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ChaiExpectation } from './ChaiExpectation';

type SUPPORTED_FRAMEWORKS = 'chai';

export interface Expectation {
notToBeEmpty();
toEqual(value: {});
toInclude(value: {});
toBeTrue();
toDeeplyInclude(value: {});
}

export function expect(subject: {}, message?: string): Expectation {
return createExpectation(subject, message);
}

function createExpectation(subject: {}, message?: string): Expectation {
const name = 'chai';

switch (name) {
case 'chai':
return new ChaiExpectation(subject);
}
}
29 changes: 6 additions & 23 deletions src/ExpectedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import { IMessage } from 'botbuilder';
import * as chai from 'chai';
import * as chaiSamSam from 'chai-samsam';
import { expect } from './Expectation';

// chai.use(chaiSamSam);/
type DeepMatchReturn = { to: {
deep: {
match(arg: {}): {}
}
}};

// type hack to get the ts to be happy
type DeepMatch = (args: {}, errMsg?: string) => DeepMatchReturn;

const expect = chai.expect;

//tslint:disable
const expectWithDeepMatch: DeepMatch = expect as any;
//tslint:enable
export enum ExpectedMessageType {
String,
IMessage,
Expand Down Expand Up @@ -63,7 +47,7 @@ export class ExpectedMessage {
this.expectedResponseCollection = expectedResponseCollection as PossibleExpectedMessageCollections;
}

expect(this.expectedResponseCollection, 'expected response collections cannot be empty').not.to.be.empty;
expect(this.expectedResponseCollection, 'expected response collections cannot be empty').notToBeEmpty();
}

/**
Expand All @@ -85,8 +69,7 @@ export class ExpectedMessage {
this.deepMessageMatchCheck(outgoingMessage);
break;
default:
expect(outgoingMessage.type).to.equal('save');

expect(outgoingMessage.type).toEqual('save');
}
}

Expand All @@ -105,7 +88,7 @@ export class ExpectedMessage {
const errorString =
`Bot should have responded with '${errorStringExpectedResponseText}', but was '${outgoingText}`;

expect(expectedResponseStrings, errorString).to.include(outgoingText);
expect(expectedResponseStrings, errorString).toInclude(outgoingText);
}

/**
Expand All @@ -118,7 +101,7 @@ export class ExpectedMessage {
const text = outgoingMessage.text;
const regexCollection: RegExp[] = this.expectedResponseCollection as RegExp[];
expect(regexCollection.some((regex: RegExp) => regex.test(text)),
`${text} did not match any regex in ${regexCollection}`).to.be.true;
`${text} did not match any regex in ${regexCollection}`).toBeTrue();
}

/**
Expand Down Expand Up @@ -160,7 +143,7 @@ export class ExpectedMessage {
delete expectedResponse.agent;

try {
expectWithDeepMatch(clone).to.deep.match(expectedResponse);
expect(clone).toDeeplyInclude(expectedResponse);
matchExists = true;
} catch (e) {
// continue
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"files": [
"./src/BotTester.ts"
]
}
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
version "2.2.41"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.41.tgz#e27cf0817153eb9f2713b2d3f6c68f1e1c3ca608"

"@types/node@^8.0.25":
version "8.0.25"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.25.tgz#66ecaf4df93f5281b48427ee96fbcdfc4f0cdce1"
"@types/node@^8.0.26":
version "8.0.26"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.26.tgz#4d58be925306fd22b1141085535a0268b8beb189"

ajv@^4.9.1:
version "4.11.8"
Expand Down