mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-26 11:17:04 +08:00
chore: update tests app short imports
This commit is contained in:
@ -1,7 +1,4 @@
|
||||
// >> application-settings-require
|
||||
var appSettings = require("application-settings");
|
||||
// << application-settings-require
|
||||
|
||||
var appSettings = require("tns-core-modules/application-settings");
|
||||
import * as TKUnit from "../TKUnit";
|
||||
|
||||
var stringKey: string = "stringKey";
|
||||
@ -16,61 +13,51 @@ export var testBoolean = function () {
|
||||
var boolValueBefore = appSettings.getBoolean(boolKey);
|
||||
TKUnit.assert(false === boolValueBefore, "Cannot set boolean to false, currently it is: " + appSettings.getBoolean(boolKey));
|
||||
|
||||
// >> application-settings-boolean
|
||||
appSettings.setBoolean("boolKey", true);
|
||||
var boolValue = appSettings.getBoolean("boolKey", false);
|
||||
// << application-settings-boolean
|
||||
|
||||
TKUnit.assert(true === boolValue, "Cannot set boolean to true");
|
||||
|
||||
TKUnit.assert(true === appSettings.getBoolean(boolKey), "Cannot set boolean to true (no default)");
|
||||
};
|
||||
|
||||
export var testString = function () {
|
||||
// >> application-settings-string
|
||||
appSettings.setString("stringKey", "String value");
|
||||
var stringValue = appSettings.getString("stringKey");
|
||||
// << application-settings-string
|
||||
|
||||
TKUnit.assert("String value" === stringValue, "Cannot set string value");
|
||||
};
|
||||
|
||||
export var testNumber = function () {
|
||||
// >> application-settings-number
|
||||
appSettings.setNumber("numberKey", 54.321);
|
||||
var value = parseFloat(appSettings.getNumber("numberKey").toFixed(3));
|
||||
// << application-settings-number
|
||||
|
||||
TKUnit.assert(54.321 === value, "Cannot set number value 54.321 != " + value);
|
||||
};
|
||||
|
||||
export var testDefaults = function () {
|
||||
// >> application-settings-notset
|
||||
var defaultValue = appSettings.getString("noStringKey", "No string value");
|
||||
// will return "No string value" if there is no value for "noStringKey"
|
||||
// << application-settings-notset
|
||||
|
||||
TKUnit.assert("No string value" === defaultValue, "Bad default string value");
|
||||
TKUnit.assert(true === appSettings.getBoolean(noBoolKey, true), "Bad default boolean value");
|
||||
TKUnit.assert(123.45 === appSettings.getNumber(noNumberKey, 123.45), "Bad default number value");
|
||||
}
|
||||
|
||||
export var testDefaultsWithNoDefaultValueProvided = function () {
|
||||
// >> application-settings-nodefault
|
||||
var defaultValue = appSettings.getString("noStringKey");
|
||||
// will return undefined if there is no value for "noStringKey"
|
||||
// << application-settings-nodefault
|
||||
|
||||
TKUnit.assertEqual(defaultValue, undefined, "Default string value is not undefined");
|
||||
|
||||
TKUnit.assertEqual(appSettings.getBoolean(noBoolKey), undefined, "Default boolean value is not undefined");
|
||||
TKUnit.assertEqual(appSettings.getNumber(noNumberKey), undefined, "Default number value is not undefined");
|
||||
};
|
||||
|
||||
// <snippet module="application-settings" title="application-settings">
|
||||
// ## Other functions
|
||||
// </snippet>
|
||||
|
||||
export var testHasKey = function () {
|
||||
// >> application-settings-haskey
|
||||
var hasKey = appSettings.hasKey("noBoolKey");
|
||||
// will return false if there is no value for "noBoolKey"
|
||||
// << application-settings-haskey
|
||||
|
||||
TKUnit.assert(!hasKey, "There is a key: " + noBoolKey);
|
||||
TKUnit.assert(!appSettings.hasKey(noStringKey), "There is a key: " + noStringKey);
|
||||
TKUnit.assert(!appSettings.hasKey(noNumberKey), "There is a key: " + noNumberKey);
|
||||
@ -81,9 +68,8 @@ export var testHasKey = function () {
|
||||
};
|
||||
|
||||
export var testRemove = function () {
|
||||
// >> application-settings-removekey
|
||||
appSettings.remove("boolKey");
|
||||
// << application-settings-removekey
|
||||
|
||||
TKUnit.assert(!appSettings.hasKey(boolKey), "Failed to remove key: " + boolKey);
|
||||
|
||||
appSettings.remove(stringKey);
|
||||
@ -94,9 +80,8 @@ export var testRemove = function () {
|
||||
};
|
||||
|
||||
export var testClear = function () {
|
||||
// >> application-settings-clear
|
||||
appSettings.clear();
|
||||
// << application-settings-clear
|
||||
|
||||
TKUnit.assert(!appSettings.hasKey(boolKey), "Failed to remove key: " + boolKey);
|
||||
TKUnit.assert(!appSettings.hasKey(stringKey), "Failed to remove key: " + stringKey);
|
||||
TKUnit.assert(!appSettings.hasKey(numberKey), "Failed to remove key: " + numberKey);
|
||||
@ -104,10 +89,10 @@ export var testClear = function () {
|
||||
|
||||
export var testFlush = function () {
|
||||
appSettings.setString(stringKey, "String value");
|
||||
// >> application-settings-flush
|
||||
|
||||
var flushed = appSettings.flush();
|
||||
// will return boolean indicating whether flush to disk was successful
|
||||
// << application-settings-flush
|
||||
|
||||
TKUnit.assert(flushed, "Flush failed: " + flushed);
|
||||
TKUnit.assert(appSettings.hasKey(stringKey), "There is no key: " + stringKey);
|
||||
};
|
||||
|
@ -1,11 +1,6 @@
|
||||
import * as TKUnit from "../TKUnit";
|
||||
import * as types from "tns-core-modules/utils/types";
|
||||
|
||||
// >> virtual-array-require
|
||||
import * as virtualArrayModule from "tns-core-modules/data/virtual-array";
|
||||
// << virtual-array-require
|
||||
|
||||
require("globals");
|
||||
|
||||
export var test_VirtualArray_shouldCreateArrayFromSpecifiedLength = function () {
|
||||
var array = new virtualArrayModule.VirtualArray<number>(100);
|
||||
@ -20,11 +15,9 @@ export var test_VirtualArray_setItemShouldSetCorrectItem = function () {
|
||||
};
|
||||
|
||||
export var test_VirtualArray_setItemShouldRaiseChangeEventWhenYouSetDifferentItem = function () {
|
||||
// >> virtual-array-itemsloading
|
||||
var array = new virtualArrayModule.VirtualArray<number>(100);
|
||||
array.loadSize = 15;
|
||||
|
||||
// >> (hide)
|
||||
var result: virtualArrayModule.ChangedData<number>;
|
||||
var index = 0;
|
||||
|
||||
@ -43,7 +36,6 @@ export var test_VirtualArray_setItemShouldRaiseChangeEventWhenYouSetDifferentIte
|
||||
|
||||
TKUnit.assert(result && result.eventName === "change" && result.action === virtualArrayModule.ChangeType.Update &&
|
||||
result.removed.length === 1 && result.index === index && result.addedCount === 1, "VirtualArray<T> setItem() should raise 'change' event with correct args!");
|
||||
// << (hide)
|
||||
|
||||
array.on(virtualArrayModule.VirtualArray.itemsLoadingEvent, (args: virtualArrayModule.ItemsLoading) => {
|
||||
// Argument (args) is ItemsLoading.
|
||||
@ -64,18 +56,14 @@ export var test_VirtualArray_setItemShouldRaiseChangeEventWhenYouSetDifferentIte
|
||||
|
||||
array.load(args.index, itemsToLoad);
|
||||
});
|
||||
// << virtual-array-itemsloading
|
||||
};
|
||||
|
||||
export var test_VirtualArray_loadShouldRaiseChangeEventWithCorrectArgs = function () {
|
||||
// >> virtual-array-change
|
||||
var array = new virtualArrayModule.VirtualArray<number>(100);
|
||||
array.loadSize = 15;
|
||||
|
||||
// >> (hide)
|
||||
var result: virtualArrayModule.ChangedData<number>;
|
||||
var index = 0;
|
||||
// << (hide)
|
||||
|
||||
array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
|
||||
// Argument (args) is ChangedData<T>.
|
||||
@ -83,15 +71,12 @@ export var test_VirtualArray_loadShouldRaiseChangeEventWithCorrectArgs = functio
|
||||
// args.action is "update".
|
||||
// args.removed.length and result.addedCount are equal to number of loaded items with load() method.
|
||||
|
||||
// >> (hide)
|
||||
result = args;
|
||||
// << (hide)
|
||||
});
|
||||
|
||||
var itemsToLoad = [0, 1, 2];
|
||||
|
||||
array.load(index, itemsToLoad);
|
||||
// << virtual-array-change
|
||||
|
||||
TKUnit.assert(result && result.eventName === "change" && result.action === virtualArrayModule.ChangeType.Update &&
|
||||
result.removed.length === itemsToLoad.length && result.index === index && result.addedCount === itemsToLoad.length,
|
||||
@ -99,14 +84,11 @@ export var test_VirtualArray_loadShouldRaiseChangeEventWithCorrectArgs = functio
|
||||
};
|
||||
|
||||
export var test_VirtualArray_lengthIncreaseShouldRaiseChangeEventWithCorrectArgs = function () {
|
||||
// >> virtual-array-lenght
|
||||
var array = new virtualArrayModule.VirtualArray<number>(100);
|
||||
array.loadSize = 15;
|
||||
|
||||
// >> (hide)
|
||||
var result: virtualArrayModule.ChangedData<number>;
|
||||
var index = array.length;
|
||||
// << (hide)
|
||||
|
||||
array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
|
||||
// Argument (args) is ChangedData<T>.
|
||||
@ -114,13 +96,10 @@ export var test_VirtualArray_lengthIncreaseShouldRaiseChangeEventWithCorrectArgs
|
||||
// args.action is "add".
|
||||
// args.removed.length is 0, result.addedCount is equal to the delta between new and old "length" property values.
|
||||
|
||||
// >> (hide)
|
||||
result = args;
|
||||
// << (hide)
|
||||
});
|
||||
|
||||
array.length += array.loadSize;
|
||||
// << virtual-array-lenght
|
||||
|
||||
TKUnit.assert(result && result.eventName === "change" && result.action === virtualArrayModule.ChangeType.Add
|
||||
&& result.index === index && result.addedCount === array.loadSize && result.removed.length === 0,
|
||||
@ -128,16 +107,11 @@ export var test_VirtualArray_lengthIncreaseShouldRaiseChangeEventWithCorrectArgs
|
||||
};
|
||||
|
||||
export var test_VirtualArray_lengthDecreaseShouldRaiseChangeEventWithCorrectArgs = function () {
|
||||
// <snippet module="data/virtual-array" title="virtual-array">
|
||||
// ### Handle "change" event when you increase "length" property.
|
||||
// ``` JavaScript
|
||||
var array = new virtualArrayModule.VirtualArray<number>(100);
|
||||
array.loadSize = 15;
|
||||
|
||||
// <hide>
|
||||
var result: virtualArrayModule.ChangedData<number>;
|
||||
var index = array.length;
|
||||
// </hide>
|
||||
|
||||
array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
|
||||
// Argument (args) is ChangedData<T>.
|
||||
@ -145,9 +119,7 @@ export var test_VirtualArray_lengthDecreaseShouldRaiseChangeEventWithCorrectArgs
|
||||
// args.action is "remove".
|
||||
// result.addedCount is 0, args.removed.length is equal to the delta between new and old "length" property values.
|
||||
|
||||
// <hide>
|
||||
result = args;
|
||||
// </hide>
|
||||
});
|
||||
|
||||
array.length -= array.loadSize;
|
||||
|
@ -1,4 +1,3 @@
|
||||
require("globals");
|
||||
import * as http from "tns-core-modules/http";
|
||||
|
||||
declare var postMessage: any;
|
||||
|
@ -1,15 +1,9 @@
|
||||
/* tslint:disable:no-unused-variable */
|
||||
import { ImageSource } from "tns-core-modules/image-source";
|
||||
import * as TKUnit from "../TKUnit";
|
||||
import * as http from "tns-core-modules/http";
|
||||
import * as fs from "tns-core-modules/file-system";
|
||||
import { addHeader } from "tns-core-modules/http/http-request";
|
||||
|
||||
require("globals");
|
||||
|
||||
// >> http-require
|
||||
// var http = require("http");
|
||||
// << http-require
|
||||
|
||||
export var test_getString_isDefined = function () {
|
||||
TKUnit.assert(typeof (http.getString) !== "undefined", "Method http.getString() should be defined!");
|
||||
};
|
||||
@ -17,20 +11,14 @@ export var test_getString_isDefined = function () {
|
||||
export var test_getString = function (done: (err: Error, res?: string) => void) {
|
||||
var result;
|
||||
|
||||
// >> http-get-string
|
||||
http.getString("https://httpbin.org/get").then(function (r) {
|
||||
//// Argument (r) is string!
|
||||
// >> (hide)
|
||||
result = r;
|
||||
done(null);
|
||||
// << (hide)
|
||||
}, function (e) {
|
||||
//// Argument (e) is Error!
|
||||
// >> (hide)
|
||||
done(e);
|
||||
// << (hide)
|
||||
});
|
||||
// << http-get-string
|
||||
};
|
||||
|
||||
export var test_getString_fail = function (done) {
|
||||
@ -67,10 +55,8 @@ export var test_getJSON_isDefined = function () {
|
||||
export var test_getJSON = function (done) {
|
||||
var result;
|
||||
|
||||
// >> http-get-json
|
||||
http.getJSON("https://httpbin.org/get").then(function (r) {
|
||||
//// Argument (r) is JSON!
|
||||
// >> (hide)
|
||||
//completed = true;
|
||||
result = r;
|
||||
try {
|
||||
@ -81,15 +67,11 @@ export var test_getJSON = function (done) {
|
||||
done(e);
|
||||
}
|
||||
done(null);
|
||||
// << (hide)
|
||||
}, function (e) {
|
||||
//// Argument (e) is Error!
|
||||
//console.log(e);
|
||||
// >> (hide)
|
||||
done(e);
|
||||
// << (hide)
|
||||
});
|
||||
// << http-get-json
|
||||
};
|
||||
|
||||
export var test_getJSON_fail = function (done) {
|
||||
@ -203,26 +185,20 @@ export var test_getImage_isDefined = function () {
|
||||
export var test_getImage = function (done) {
|
||||
var result;
|
||||
|
||||
// >> http-get-image
|
||||
http.getImage("https://httpbin.org/image/png").then((r) => {
|
||||
// Argument (r) is ImageSource!
|
||||
// >> (hide)
|
||||
result = r;
|
||||
try {
|
||||
TKUnit.assert(result instanceof require("image-source").ImageSource, "Result from getImage() should be valid ImageSource object!");
|
||||
TKUnit.assert(result instanceof ImageSource, "Result from getImage() should be valid ImageSource object!");
|
||||
done(null);
|
||||
}
|
||||
catch (err) {
|
||||
done(err);
|
||||
}
|
||||
// << (hide)
|
||||
}, (err) => {
|
||||
// Argument (e) is Error!
|
||||
// >> (hide)
|
||||
done(err);
|
||||
// << (hide)
|
||||
});
|
||||
// << http-get-image
|
||||
};
|
||||
|
||||
export var test_getImage_fail = function (done) {
|
||||
@ -262,10 +238,8 @@ export var test_getFile_isDefined = function () {
|
||||
export var test_getFile = function (done) {
|
||||
var result;
|
||||
|
||||
// >> http-get-urlfile
|
||||
http.getFile("https://raw.githubusercontent.com/NativeScript/NativeScript/master/tests/app/logo.png").then(function (r) {
|
||||
//// Argument (r) is File!
|
||||
// >> (hide)
|
||||
result = r;
|
||||
try {
|
||||
TKUnit.assert(result instanceof fs.File, "Result from getFile() should be valid File object!");
|
||||
@ -274,24 +248,18 @@ export var test_getFile = function (done) {
|
||||
catch (err) {
|
||||
done(err);
|
||||
}
|
||||
// << (hide)
|
||||
}, function (e) {
|
||||
//// Argument (e) is Error!
|
||||
// >> (hide)
|
||||
done(e);
|
||||
// << (hide)
|
||||
});
|
||||
// << http-get-urlfile
|
||||
};
|
||||
|
||||
export var test_getContentAsFile = function (done) {
|
||||
var result;
|
||||
|
||||
// >> http-get-urlfile-content
|
||||
var filePath = fs.path.join(fs.knownFolders.documents().path, "test.png");
|
||||
http.getFile("https://httpbin.org/image/png?testQuery=query&anotherParam=param", filePath).then(function (r) {
|
||||
//// Argument (r) is File!
|
||||
// >> (hide)
|
||||
result = r;
|
||||
try {
|
||||
TKUnit.assert(result instanceof fs.File, "Result from getFile() should be valid File object!");
|
||||
@ -300,14 +268,10 @@ export var test_getContentAsFile = function (done) {
|
||||
catch (err) {
|
||||
done(err);
|
||||
}
|
||||
// << (hide)
|
||||
}, function (e) {
|
||||
//// Argument (e) is Error!
|
||||
// >> (hide)
|
||||
done(e);
|
||||
// << (hide)
|
||||
});
|
||||
// << http-get-urlfile-content
|
||||
};
|
||||
|
||||
export var test_getFile_fail = function (done) {
|
||||
@ -361,11 +325,9 @@ export var test_request_requestShouldTimeout = function (done) {
|
||||
export var test_request_responseStatusCodeShouldBeDefined = function (done) {
|
||||
var result: http.HttpResponse;
|
||||
|
||||
// >> http-get-response
|
||||
http.request({ url: "https://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
//// Argument (response) is HttpResponse!
|
||||
var statusCode = response.statusCode;
|
||||
// >> (hide)
|
||||
result = response;
|
||||
try {
|
||||
TKUnit.assert(typeof (result.statusCode) !== "undefined", "response.statusCode should be defined!");
|
||||
@ -374,14 +336,10 @@ export var test_request_responseStatusCodeShouldBeDefined = function (done) {
|
||||
catch (err) {
|
||||
done(err);
|
||||
}
|
||||
// << (hide)
|
||||
}, function (e) {
|
||||
//// Argument (e) is Error!
|
||||
// >> (hide)
|
||||
done(e);
|
||||
// << (hide)
|
||||
});
|
||||
// << http-get-response
|
||||
};
|
||||
|
||||
export var test_headRequest_responseStatusCodeShouldBeDefined = function (done) {
|
||||
@ -401,13 +359,11 @@ export var test_headRequest_responseStatusCodeShouldBeDefined = function (done)
|
||||
export var test_request_responseHeadersShouldBeDefined = function (done) {
|
||||
var result: http.HttpResponse;
|
||||
|
||||
// >> http-get-response-headers
|
||||
http.request({ url: "https://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
//// Argument (response) is HttpResponse!
|
||||
//for (var header in response.headers) {
|
||||
// console.log(header + ":" + response.headers[header]);
|
||||
//}
|
||||
// >> (hide)
|
||||
result = response;
|
||||
try {
|
||||
TKUnit.assert(typeof (result.headers) !== "undefined", "response.headers should be defined!");
|
||||
@ -416,27 +372,21 @@ export var test_request_responseHeadersShouldBeDefined = function (done) {
|
||||
catch (err) {
|
||||
done(err);
|
||||
}
|
||||
// << (hide)
|
||||
}, function (e) {
|
||||
//// Argument (e) is Error!
|
||||
// >> (hide)
|
||||
done(e);
|
||||
// << (hide)
|
||||
});
|
||||
// << http-get-response-headers
|
||||
};
|
||||
|
||||
export var test_request_responseContentShouldBeDefined = function (done) {
|
||||
var result: http.HttpResponse;
|
||||
|
||||
// >> http-get-response-content
|
||||
http.request({ url: "https://httpbin.org/get", method: "GET" }).then(function (response) {
|
||||
//// Argument (response) is HttpResponse!
|
||||
//// Content property of the response is HttpContent!
|
||||
var str = response.content.toString();
|
||||
var obj = response.content.toJSON();
|
||||
var img = response.content.toImage();
|
||||
// >> (hide)
|
||||
result = response;
|
||||
try {
|
||||
TKUnit.assert(typeof (result.content) !== "undefined", "response.content should be defined!");
|
||||
@ -445,14 +395,10 @@ export var test_request_responseContentShouldBeDefined = function (done) {
|
||||
catch (err) {
|
||||
done(err);
|
||||
}
|
||||
// << (hide)
|
||||
}, function (e) {
|
||||
//// Argument (e) is Error!
|
||||
// >> (hide)
|
||||
done(e);
|
||||
// << (hide)
|
||||
});
|
||||
// << http-get-response-content
|
||||
};
|
||||
|
||||
export var test_request_responseContentToStringShouldReturnString = function (done) {
|
||||
@ -496,7 +442,7 @@ export var test_request_responseContentToImageShouldReturnCorrectImage = functio
|
||||
response.content.toImage().then((source) => {
|
||||
result = source;
|
||||
try {
|
||||
TKUnit.assert(result instanceof require("image-source").ImageSource, "Result from toImage() should be valid promise of ImageSource object!");
|
||||
TKUnit.assert(result instanceof ImageSource, "Result from toImage() should be valid promise of ImageSource object!");
|
||||
done(null);
|
||||
}
|
||||
catch (err) {
|
||||
@ -685,7 +631,6 @@ export var test_request_jsonAsContentSentAndReceivedProperly = function (done) {
|
||||
content: JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })
|
||||
}).then(function (response) {
|
||||
// result = response.content.toJSON();
|
||||
// >> (hide)
|
||||
result = response.content.toJSON();
|
||||
try {
|
||||
TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
|
||||
@ -694,15 +639,11 @@ export var test_request_jsonAsContentSentAndReceivedProperly = function (done) {
|
||||
catch (err) {
|
||||
done(err);
|
||||
}
|
||||
// << (hide)
|
||||
// console.log(result);
|
||||
}, function (e) {
|
||||
// >> (hide)
|
||||
done(e);
|
||||
// << (hide)
|
||||
// console.log("Error occurred " + e);
|
||||
});
|
||||
// << http-post-json
|
||||
};
|
||||
|
||||
declare var Worker: any;
|
||||
|
@ -2,12 +2,11 @@
|
||||
import * as imageSource from "tns-core-modules/image-source";
|
||||
import * as gridModule from "tns-core-modules/ui/layouts/grid-layout";
|
||||
import * as enums from "tns-core-modules/ui/enums";
|
||||
import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout";
|
||||
import { Label } from "tns-core-modules/ui/label";
|
||||
import { Image } from "tns-core-modules/ui/image";
|
||||
|
||||
export function createPage() {
|
||||
var StackLayout = require("ui/layouts/stack-layout").StackLayout;
|
||||
var Label = require("ui/label").Label;
|
||||
var Image = require("ui/image").Image;
|
||||
|
||||
var stack = new StackLayout();
|
||||
var grid = new gridModule.GridLayout();
|
||||
stack.addChild(grid);
|
||||
@ -20,7 +19,7 @@ export function createPage() {
|
||||
var defaultImageSource = imageSource.fromFile(__dirname + "/test.png");
|
||||
|
||||
var img = new Image();
|
||||
img.source = defaultImageSource;
|
||||
img.src = defaultImageSource;
|
||||
|
||||
img.width = 80;
|
||||
img.height = 80;
|
||||
|
@ -1,5 +1,7 @@
|
||||
var observableModule = require("data/observable");
|
||||
var vm = new observableModule.Observable();
|
||||
import { Observable } from "tns-core-modules/data/observable";
|
||||
|
||||
var vm = new Observable();
|
||||
|
||||
export function onPageLoaded(args) {
|
||||
var page = args.object;
|
||||
vm.set("firstTitle", "fiiiirst");
|
||||
|
Reference in New Issue
Block a user