mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
tests fixed
This commit is contained in:
@ -2,14 +2,6 @@
|
||||
import TKUnit = require("./TKUnit");
|
||||
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 () {
|
||||
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>
|
||||
};
|
||||
/*
|
||||
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) {
|
||||
var result;
|
||||
|
@ -46,11 +46,16 @@ export var test_XMLHttpRequest_responseType_isDefined = function () {
|
||||
|
||||
export var test_XMLHttpRequest_readyStateShouldChange = function (done) {
|
||||
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!");
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
// var state = xhr.readyState;
|
||||
// <hide>
|
||||
try {
|
||||
|
||||
if (count === 0) {
|
||||
@ -70,18 +75,26 @@ export var test_XMLHttpRequest_readyStateShouldChange = function (done) {
|
||||
catch (err) {
|
||||
done(err);
|
||||
}
|
||||
// </hide>
|
||||
};
|
||||
|
||||
xhr.open("GET", "https://httpbin.org/get");
|
||||
xhr.send();
|
||||
// ```
|
||||
// </snippet>
|
||||
};
|
||||
|
||||
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.setRequestHeader("Content-Type", "application/json");
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState > 1) {
|
||||
// var contentTypeHeader = xhr.getResponseHeader("Content-Type");
|
||||
// <hide>
|
||||
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);
|
||||
}
|
||||
// </hide>
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
// ```
|
||||
// </snippet>
|
||||
};
|
||||
|
||||
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.setRequestHeader("Content-Type", "application/json");
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState > 3) {
|
||||
var result = JSON.parse(xhr.responseText);
|
||||
// var valueOne = result["json"]["MyVariableOne"];
|
||||
// <hide>
|
||||
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);
|
||||
}
|
||||
// </hide>
|
||||
}
|
||||
};
|
||||
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
|
||||
// ```
|
||||
// </snippet>
|
||||
};
|
||||
|
||||
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.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"];
|
||||
// <hide>
|
||||
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);
|
||||
}
|
||||
// </hide>
|
||||
}
|
||||
};
|
||||
|
||||
@ -135,20 +165,27 @@ export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function
|
||||
data.append("MyVariableTwo", "ValueTwo");
|
||||
|
||||
xhr.send(data);
|
||||
// ```
|
||||
// </snippet>
|
||||
};
|
||||
|
||||
export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) {
|
||||
var flag = false;
|
||||
|
||||
xhr = new XMLHttpRequest();
|
||||
// <snippet module="xhr" title="xhr">
|
||||
// ### Abort request
|
||||
// ``` JavaScript
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "https://httpbin.org/post");
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
// <hide>
|
||||
xhr.onreadystatechange = function () {
|
||||
flag = true;
|
||||
};
|
||||
// </hide>
|
||||
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
|
||||
xhr.abort();
|
||||
|
||||
// ```
|
||||
// </snippet>
|
||||
TKUnit.assert(flag === false, "Content not sent/received properly!");
|
||||
done(null);
|
||||
};
|
||||
|
Reference in New Issue
Block a user