tests fixed

This commit is contained in:
Vladimir Enchev
2015-07-30 09:56:24 +03:00
parent 23a8c6aba1
commit 8b32ed0176
2 changed files with 45 additions and 61 deletions

View File

@ -2,14 +2,6 @@
import TKUnit = require("./TKUnit"); import TKUnit = require("./TKUnit");
import types = require("utils/types"); import types = require("utils/types");
// <snippet module="fetch" title="fetch">
// # Fetch module
// Using fetch methods requires to load "fetch" module.
// ``` JavaScript
// var fetch = require("fetch");
// ```
// </snippet>
export var test_fetch_defined = function () { export var test_fetch_defined = function () {
TKUnit.assert(types.isDefined((fetch)), "Method fetch() should be defined!"); TKUnit.assert(types.isDefined((fetch)), "Method fetch() should be defined!");
}; };
@ -78,51 +70,6 @@ export var test_fetch_json = function (done: (err: Error, res?: string) => void)
// ``` // ```
// </snippet> // </snippet>
}; };
/*
export var test_fetch_blob = function (done: (err: Error, res?: string) => void) {
var result;
// <snippet module="fetch" title="fetch">
// ### Get Blob from URL
// ``` JavaScript
fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) {
//// Argument (r) is Blob object!
// <hide>
TKUnit.assert(r instanceof Blob, "Result from blob() should be Blob object! Actual result is: " + r);
done(null);
// </hide>
}, function (e) {
//// Argument (e) is Error!
// <hide>
done(e);
// </hide>
});
// ```
// </snippet>
};
export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) => void) {
var result;
// <snippet module="fetch" title="fetch">
// ### Get ArrayBuffer from URL
// ``` JavaScript
fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) {
//// Argument (r) is ArrayBuffer object!
// <hide>
TKUnit.assert(r instanceof ArrayBuffer, "Result from arrayBuffer() should be ArrayBuffer object! Actual result is: " + r);
done(null);
// </hide>
}, function (e) {
//// Argument (e) is Error!
// <hide>
done(e);
// </hide>
});
// ```
// </snippet>
};
*/
export var test_fetch_formData = function (done: (err: Error, res?: string) => void) { export var test_fetch_formData = function (done: (err: Error, res?: string) => void) {
var result; var result;

View File

@ -46,11 +46,16 @@ export var test_XMLHttpRequest_responseType_isDefined = function () {
export var test_XMLHttpRequest_readyStateShouldChange = function (done) { export var test_XMLHttpRequest_readyStateShouldChange = function (done) {
var count = 0; var count = 0;
xhr = new XMLHttpRequest(); // <snippet module="xhr" title="xhr">
// ### Check readyState
// ``` JavaScript
let xhr = new XMLHttpRequest();
TKUnit.assert(xhr.readyState === 0, "xhr.readyState should be UNSENT!"); TKUnit.assert(xhr.readyState === 0, "xhr.readyState should be UNSENT!");
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
// var state = xhr.readyState;
// <hide>
try { try {
if (count === 0) { if (count === 0) {
@ -70,18 +75,26 @@ export var test_XMLHttpRequest_readyStateShouldChange = function (done) {
catch (err) { catch (err) {
done(err); done(err);
} }
// </hide>
}; };
xhr.open("GET", "https://httpbin.org/get"); xhr.open("GET", "https://httpbin.org/get");
xhr.send(); xhr.send();
// ```
// </snippet>
}; };
export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done) { export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done) {
xhr = new XMLHttpRequest(); // <snippet module="xhr" title="xhr">
// ### Send/receive headers
// ``` JavaScript
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://httpbin.org/get"); xhr.open("GET", "https://httpbin.org/get");
xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState > 1) { if (xhr.readyState > 1) {
// var contentTypeHeader = xhr.getResponseHeader("Content-Type");
// <hide>
try { try {
TKUnit.assert(xhr.getResponseHeader("Content-Type") === "application/json", "Headers not sent/received properly!"); TKUnit.assert(xhr.getResponseHeader("Content-Type") === "application/json", "Headers not sent/received properly!");
done(null); done(null);
@ -89,18 +102,26 @@ export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done)
catch (err) { catch (err) {
done(err); done(err);
} }
// </hide>
} }
}; };
xhr.send(); xhr.send();
// ```
// </snippet>
}; };
export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) { export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) {
xhr = new XMLHttpRequest(); // <snippet module="xhr" title="xhr">
// ### Send/receive JSON
// ``` JavaScript
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://httpbin.org/post"); xhr.open("POST", "https://httpbin.org/post");
xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState > 3) { if (xhr.readyState > 3) {
var result = JSON.parse(xhr.responseText); var result = JSON.parse(xhr.responseText);
// var valueOne = result["json"]["MyVariableOne"];
// <hide>
try { try {
TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!"); TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
done(null); done(null);
@ -108,18 +129,26 @@ export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done)
catch (err) { catch (err) {
done(err); done(err);
} }
// </hide>
} }
}; };
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
// ```
// </snippet>
}; };
export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function (done) { export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function (done) {
xhr = new XMLHttpRequest(); // <snippet module="xhr" title="xhr">
// ### Send/receive FormData
// ``` JavaScript
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://httpbin.org/post"); xhr.open("POST", "https://httpbin.org/post");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState > 3) { if (xhr.readyState > 3) {
var result = JSON.parse(xhr.responseText); var result = JSON.parse(xhr.responseText);
// var valueOne = result["form"]["MyVariableOne"];
// <hide>
try { try {
TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Result is: " + xhr.responseText); TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Result is: " + xhr.responseText);
done(null); done(null);
@ -127,6 +156,7 @@ export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function
catch (err) { catch (err) {
done(err); done(err);
} }
// </hide>
} }
}; };
@ -135,20 +165,27 @@ export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function
data.append("MyVariableTwo", "ValueTwo"); data.append("MyVariableTwo", "ValueTwo");
xhr.send(data); xhr.send(data);
// ```
// </snippet>
}; };
export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) { export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) {
var flag = false; var flag = false;
// <snippet module="xhr" title="xhr">
xhr = new XMLHttpRequest(); // ### Abort request
// ``` JavaScript
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://httpbin.org/post"); xhr.open("POST", "https://httpbin.org/post");
xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("Content-Type", "application/json");
// <hide>
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
flag = true; flag = true;
}; };
// </hide>
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
xhr.abort(); xhr.abort();
// ```
// </snippet>
TKUnit.assert(flag === false, "Content not sent/received properly!"); TKUnit.assert(flag === false, "Content not sent/received properly!");
done(null); done(null);
}; };
@ -203,6 +240,6 @@ export function test_responseType(done) {
() => xhr.responseType = "arraybuffer", () => xhr.responseType = "arraybuffer",
"Didn't raise on unsupported type.", "Didn't raise on unsupported type.",
"Response type of 'arraybuffer' not supported." "Response type of 'arraybuffer' not supported."
); );
done(null); done(null);
} }