Skip to content

Commit

Permalink
Preparing tests for browser support #34
Browse files Browse the repository at this point in the history
  • Loading branch information
dareid committed Feb 7, 2016
1 parent 2d05f5d commit 10d4157
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 185 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"after": true,
"afterEach": true,
"describe": true,
"it": true
"it": true,
"window": true
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"scripts": {
"pretest": "npm install",
"test": "istanbul cover _mocha -- -t 10000 test/*.js test/*/*.js examples/*.js",
"test": "istanbul cover _mocha",
"predoc": "npm install",
"doc": "jsdoc -t node_modules/jaguarjs-jsdoc -c conf.json -R README.md -r lib",
"prebuild:js": "rm -rf dist",
Expand Down
19 changes: 10 additions & 9 deletions test/assertions/compression.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
var chakram = require('./../../lib/chakram.js'),
expect = chakram.expect;
var testsRunningInNode = (typeof global !== "undefined" ? true : false),
chakram = (testsRunningInNode ? global.chakram : window.chakram),
expect = (testsRunningInNode ? global.expect : window.expect);

describe("Chakram Assertions", function() {

describe("Compression", function() {

it("should allow assertions on uncompressed responses", function () {
var noncompressed = chakram.get("http://httpbin.org/get");
expect(noncompressed).not.to.be.encoded.with.gzip;
expect(noncompressed).not.to.be.encoded.with.deflate;
return chakram.wait();
});

it("should detect gzip compression", function () {
var gzip = chakram.get("http://httpbin.org/gzip");
expect(gzip).to.be.encoded.with.gzip;
expect(gzip).not.to.be.encoded.with.deflate;
return chakram.wait();
});

it("should detect deflate compression", function () {
var deflate = chakram.get("http://httpbin.org/deflate");
expect(deflate).not.to.be.encoded.with.gzip;
expect(deflate).to.be.encoded.with.deflate;
return chakram.wait();
});

it("should support shorter language chains", function () {
var deflate = chakram.get("http://httpbin.org/deflate");
expect(deflate).not.to.be.gzip;
expect(deflate).to.be.deflate;
return chakram.wait();
});
});
});

});
});
11 changes: 6 additions & 5 deletions test/assertions/cookie.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var chakram = require('./../../lib/chakram.js'),
expect = chakram.expect,
request = require('request');
var testsRunningInNode = (typeof global !== "undefined" ? true : false),
chakram = (testsRunningInNode ? global.chakram : window.chakram),
expect = (testsRunningInNode ? global.expect : window.expect);

describe("Chakram Assertions", function() {
describe("Cookies", function() {
Expand Down Expand Up @@ -75,7 +75,8 @@ describe("Chakram Assertions", function() {
return chakram.wait();
});

it("should preserve cookies if defaults jar set to instance", function() {
testsRunningInNode && it("should preserve cookies if defaults jar set to instance", function() {
var request = require('request');
var jar = request.jar();
chakram.setRequestDefaults({jar: jar});

Expand All @@ -89,4 +90,4 @@ describe("Chakram Assertions", function() {


});
});
});
31 changes: 16 additions & 15 deletions test/assertions/header.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
var chakram = require('./../../lib/chakram.js'),
expect = chakram.expect;
var testsRunningInNode = (typeof global !== "undefined" ? true : false),
chakram = (testsRunningInNode ? global.chakram : window.chakram),
expect = (testsRunningInNode ? global.expect : window.expect);

describe("Chakram Assertions", function() {
describe("Chakram Assertions", function() {
describe("Header", function() {

var headerRequest;

before(function() {
headerRequest = chakram.get("http://httpbin.org/response-headers?testheader=true123");
});

it("should check existance of a header", function () {
expect(headerRequest).to.have.header('testheader');
expect(headerRequest).to.have.header('testHeaDer');
expect(headerRequest).not.to.have.header('notpresentheader');
return chakram.wait();
});

it("should check that header matches string", function () {
expect(headerRequest).to.have.header('testheader', "true123");
expect(headerRequest).to.have.header('TESTHEADER', "true123");

expect(headerRequest).not.to.have.header('testheader', "123");
expect(headerRequest).not.to.have.header('testheader', "TRUE");
expect(headerRequest).not.to.have.header('testheader', "true");
expect(headerRequest).not.to.have.header('testheader', "tru");

expect(headerRequest).not.to.have.header('notpresentheader', "true123");
return chakram.wait();
});

it("should check that header satisfies regex", function () {
expect(headerRequest).to.have.header('testheader', /true/);
expect(headerRequest).to.have.header('testheader', /ru/);
expect(headerRequest).to.have.header('testheader', /\d/);
expect(headerRequest).to.have.header('testheader', /[t][r]/);
expect(headerRequest).to.have.header('Testheader', /TRUE/i);

expect(headerRequest).not.to.have.header('testheader', /tree/);
expect(headerRequest).not.to.have.header('testheader', /\s/);
expect(headerRequest).not.to.have.header('testheader', /[t][w|y|j]/);
expect(headerRequest).not.to.have.header('testheader', /TRUE/);

expect(headerRequest).not.to.have.header('notpresentheader', "/true123/");
return chakram.wait();
});

it("should call provided functions with the header value", function () {
expect(headerRequest).to.have.header('testheader', function (headerValue) {
expect(headerValue).to.equal("true123");
Expand All @@ -55,5 +56,5 @@ describe("Chakram Assertions", function() {
});
return chakram.wait();
});
});
});
});
});
47 changes: 24 additions & 23 deletions test/assertions/json.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
var chakram = require('./../../lib/chakram.js'),
expect = chakram.expect;
var testsRunningInNode = (typeof global !== "undefined" ? true : false),
chakram = (testsRunningInNode ? global.chakram : window.chakram),
expect = (testsRunningInNode ? global.expect : window.expect);

describe("Chakram Assertions", function() {
describe("Chakram Assertions", function() {
describe("JSON", function() {

var postRequest;

before(function() {
postRequest = chakram.post("http://httpbin.org/post", {
stringArray: ["test1", "test2", "test3"],
number: 20,
str: "test str",
empty: null,
obj: {
test: "str"
test: "str"
}
});
});

it("should throw an error if path does not exist", function () {
return postRequest.then(function (obj) {
expect(function() {
expect(obj).to.have.json('headers.non.existant', {});
}).to.throw(Error);
});
});
});

it("should support checking that a path does not exist", function () {
return expect(postRequest).not.to.have.json('headers.non.existant');
});

describe("Equals", function () {
it("should ensure matches json exactly", function () {
return chakram.waitFor([
expect(postRequest).to.have.json('json.stringArray', ["test1", "test2", "test3"]),
expect(postRequest).to.have.json('json.number', 20),
expect(postRequest).not.to.have.json('json.number', 22),
expect(postRequest).to.have.json('json.obj', {
test: "str"
test: "str"
})
]);
});
});
it("should be able to equal nulls", function () {
return expect(postRequest).to.have.json('json.empty', null);
});
});

var testChainedCompriseProperty = function(description, buildChain) {
describe(description, function () {
it("should ensure body includes given json", function() {
Expand All @@ -60,10 +61,10 @@ describe("Chakram Assertions", function() {
}
}),
buildChain(expect(postRequest).to.not).json({
json: { number: 22 }
json: { number: 22 }
}),
buildChain(expect(postRequest).to).json({
json: {
json: {
obj: {
test: "str"
}
Expand All @@ -76,7 +77,7 @@ describe("Chakram Assertions", function() {
return postRequest.then(function (resp) {
expect(function() {
buildChain(expect(resp).to.not).json({
json: { number: 20 }
json: { number: 20 }
});
}).to.throw(Error);
});
Expand All @@ -93,29 +94,29 @@ describe("Chakram Assertions", function() {
test: "str"
}),
buildChain(expect(postRequest).to.not).json('json.obj', {
doesnt: "exist"
doesnt: "exist"
})
]);
});
});
};

testChainedCompriseProperty("Comprise", function(assertion){ return assertion.comprise.of; });
testChainedCompriseProperty("Comprised", function(assertion){ return assertion.be.comprised.of; });
describe("Callbacks", function () {

describe("Callbacks", function () {
it("should allow custom callbacks to be used to run assertions", function () {
return expect(postRequest).to.have.json('json.stringArray', function (data) {
expect(data).to.deep.equal(["test1", "test2", "test3"]);
});
});

it("should allow the whole JSON body to be checked", function () {
return expect(postRequest).to.have.json(function (data) {
expect(data.json.number).to.be.above(19).and.below(21);
expect(data.json.number).not.to.equal(211);
});
});
});
});
});
});
});
Loading

0 comments on commit 10d4157

Please sign in to comment.