From 8b32ed0176a033904195cac0e0a155f45d9a173a Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Thu, 30 Jul 2015 09:56:24 +0300 Subject: [PATCH] tests fixed --- apps/tests/fetch-tests.ts | 53 --------------------------------------- apps/tests/xhr-tests.ts | 53 +++++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 61 deletions(-) diff --git a/apps/tests/fetch-tests.ts b/apps/tests/fetch-tests.ts index de541975a..2f9fbb064 100644 --- a/apps/tests/fetch-tests.ts +++ b/apps/tests/fetch-tests.ts @@ -2,14 +2,6 @@ import TKUnit = require("./TKUnit"); import types = require("utils/types"); -// -// # Fetch module -// Using fetch methods requires to load "fetch" module. -// ``` JavaScript -// var fetch = require("fetch"); -// ``` -// - export var test_fetch_defined = function () { 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) // ``` // }; -/* -export var test_fetch_blob = function (done: (err: Error, res?: string) => void) { - var result; - - // - // ### Get Blob from URL - // ``` JavaScript - fetch("https://httpbin.org/get").then(response => { return response.blob(); }).then(function (r) { - //// Argument (r) is Blob object! - // - TKUnit.assert(r instanceof Blob, "Result from blob() should be Blob object! Actual result is: " + r); - done(null); - // - }, function (e) { - //// Argument (e) is Error! - // - done(e); - // - }); - // ``` - // -}; - -export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) => void) { - var result; - - // - // ### Get ArrayBuffer from URL - // ``` JavaScript - fetch("https://httpbin.org/get").then(response => { return response.arrayBuffer(); }).then(function (r) { - //// Argument (r) is ArrayBuffer object! - // - TKUnit.assert(r instanceof ArrayBuffer, "Result from arrayBuffer() should be ArrayBuffer object! Actual result is: " + r); - done(null); - // - }, function (e) { - //// Argument (e) is Error! - // - done(e); - // - }); - // ``` - // -}; -*/ export var test_fetch_formData = function (done: (err: Error, res?: string) => void) { var result; diff --git a/apps/tests/xhr-tests.ts b/apps/tests/xhr-tests.ts index 912a2e0e4..a71f5566f 100644 --- a/apps/tests/xhr-tests.ts +++ b/apps/tests/xhr-tests.ts @@ -46,11 +46,16 @@ export var test_XMLHttpRequest_responseType_isDefined = function () { export var test_XMLHttpRequest_readyStateShouldChange = function (done) { var count = 0; - xhr = new XMLHttpRequest(); + // + // ### Check readyState + // ``` JavaScript + let xhr = new XMLHttpRequest(); TKUnit.assert(xhr.readyState === 0, "xhr.readyState should be UNSENT!"); xhr.onreadystatechange = function () { + // var state = xhr.readyState; + // try { if (count === 0) { @@ -70,18 +75,26 @@ export var test_XMLHttpRequest_readyStateShouldChange = function (done) { catch (err) { done(err); } + // }; xhr.open("GET", "https://httpbin.org/get"); xhr.send(); + // ``` + // }; export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done) { - xhr = new XMLHttpRequest(); + // + // ### Send/receive headers + // ``` JavaScript + let xhr = new XMLHttpRequest(); xhr.open("GET", "https://httpbin.org/get"); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onreadystatechange = function () { if (xhr.readyState > 1) { + // var contentTypeHeader = xhr.getResponseHeader("Content-Type"); + // try { TKUnit.assert(xhr.getResponseHeader("Content-Type") === "application/json", "Headers not sent/received properly!"); done(null); @@ -89,18 +102,26 @@ export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done) catch (err) { done(err); } + // } }; xhr.send(); + // ``` + // }; export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) { - xhr = new XMLHttpRequest(); + // + // ### Send/receive JSON + // ``` JavaScript + let xhr = new XMLHttpRequest(); xhr.open("POST", "https://httpbin.org/post"); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onreadystatechange = function () { if (xhr.readyState > 3) { var result = JSON.parse(xhr.responseText); + // var valueOne = result["json"]["MyVariableOne"]; + // try { TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!"); done(null); @@ -108,18 +129,26 @@ export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) catch (err) { done(err); } + // } }; xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); + // ``` + // }; export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function (done) { - xhr = new XMLHttpRequest(); + // + // ### Send/receive FormData + // ``` JavaScript + let xhr = new XMLHttpRequest(); xhr.open("POST", "https://httpbin.org/post"); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { if (xhr.readyState > 3) { var result = JSON.parse(xhr.responseText); + // var valueOne = result["form"]["MyVariableOne"]; + // try { TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Result is: " + xhr.responseText); done(null); @@ -127,6 +156,7 @@ export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function catch (err) { done(err); } + // } }; @@ -135,20 +165,27 @@ export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function data.append("MyVariableTwo", "ValueTwo"); xhr.send(data); + // ``` + // }; export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) { var flag = false; - - xhr = new XMLHttpRequest(); + // + // ### Abort request + // ``` JavaScript + let xhr = new XMLHttpRequest(); xhr.open("POST", "https://httpbin.org/post"); xhr.setRequestHeader("Content-Type", "application/json"); + // xhr.onreadystatechange = function () { flag = true; }; + // xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); xhr.abort(); - + // ``` + // TKUnit.assert(flag === false, "Content not sent/received properly!"); done(null); }; @@ -203,6 +240,6 @@ export function test_responseType(done) { () => xhr.responseType = "arraybuffer", "Didn't raise on unsupported type.", "Response type of 'arraybuffer' not supported." - ); + ); done(null); }