Files
NativeScript/tests/app/timer/timer-tests.ts
Alexander Vakrilov cc97a16800 feat: Scoped Packages (#7911)
* chore: move tns-core-modules to nativescript-core

* chore: preparing compat generate script

* chore: add missing definitions

* chore: no need for http-request to be private

* chore: packages chore

* test: generate tests for tns-core-modules

* chore: add anroid module for consistency

* chore: add .npmignore

* chore: added privateModulesWhitelist

* chore(webpack): added bundle-entry-points

* chore: scripts

* chore: tests changed to use @ns/core

* test: add scoped-packages test project

* test: fix types

* test: update test project

* chore: build scripts

* chore: update build script

* chore: npm scripts cleanup

* chore: make the compat pgk work with old wp config

* test: generate diff friendly tests

* chore: create barrel exports

* chore: move files after rebase

* chore: typedoc config

* chore: compat mode

* chore: review of barrels

* chore: remove tns-core-modules import after rebase

* chore: dev workflow setup

* chore: update developer-workflow

* docs: experiment with API extractor

* chore: api-extractor and barrel exports

* chore: api-extractor configs

* chore: generate d.ts rollup with api-extractor

* refactor: move methods inside Frame

* chore: fic tests to use Frame static methods

* refactor: create Builder class

* refactor: use Builder class in tests

* refactor: include Style in ui barrel

* chore: separate compat build script

* chore: fix tslint errors

* chore: update NATIVESCRIPT_CORE_ARGS

* chore: fix compat pack

* chore: fix ui-test-app build with linked modules

* chore: Application, ApplicationSettings, Connectivity and Http

* chore: export Trace, Profiling and Utils

* refactor: Static create methods for ImageSource

* chore: fix deprecated usages of ImageSource

* chore: move Span and FormattedString to ui

* chore: add events-args and ImageSource to index files

* chore: check for CLI >= 6.2 when building for IOS

* chore: update travis build

* chore: copy Pod file to compat package

* chore: update error msg ui-tests-app

* refactor: Apply suggestions from code review

Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com>

* chore: typings and refs

* chore: add missing d.ts files for public API

* chore: adress code review FB

* chore: update api-report

* chore: dev-workflow for other apps

* chore: api update

* chore: update api-report
2019-10-17 00:45:33 +03:00

229 lines
6.1 KiB
TypeScript

import * as TKUnit from "../tk-unit";
import * as timer from "@nativescript/core/timer";
// >> timer-require
// require("globals");
//// OR
// var timer = require("timer");
// << timer-require
export function test_setTimeout_isDefined() {
TKUnit.assertNotEqual(timer.setTimeout, undefined, "Method timer.setTimeout() should be defined!");
}
export function test_clearTimeout_isDefined() {
TKUnit.assertNotEqual(timer.clearTimeout, undefined, "Method timer.clearTimeout() should be defined!");
}
export function test_setInsDefined() {
TKUnit.assertNotEqual(timer.setInterval, undefined, "Method timer.setInterval() should be defined!");
}
export function test_clear_isDefined() {
TKUnit.assertNotEqual(timer.clearInterval, undefined, "Method timer.clearInterval() should be defined!");
}
export function test_setTimeout() {
let completed: boolean;
// >> timer-set-zero
const id = timer.setTimeout(() => {
// >> (hide)
completed = true;
// << (hide)
});
// << timer-set-zero
TKUnit.waitUntilReady(() => completed, 0.5, false);
timer.clearTimeout(id);
TKUnit.assert(completed, "Callback should be called!");
}
export function test_setTimeout_extraArgs() {
let completed: boolean;
let rnd: number = Math.random();
// >> timer-set-zero-args
const id = timer.setTimeout((arg) => {
// >> (hide)
completed = rnd === arg;
// << (hide)
}, 0, rnd);
// << timer-set-zero-args
TKUnit.waitUntilReady(() => completed, 0.5, false);
timer.clearTimeout(id);
TKUnit.assert(completed, "Callback called with expected argument!");
}
export function test_setTimeout_callbackCalledAfterSpecifiedTime() {
let completed = false;
// >> timer-set-ten
const id = timer.setTimeout(() => {
// >> (hide)
completed = true;
// << (hide)
}, 10);
// << timer-set-ten
TKUnit.waitUntilReady(() => completed, 1);
timer.clearTimeout(id);
TKUnit.assert(completed, "Callback should be called after the specified time!");
}
export function test_setTimeout_callbackCalledWithBooleanPeriod() {
let completed = false;
// >> timer-set-false
const id = timer.setTimeout(() => {
// >> (hide)
completed = true;
// << (hide)
// @ts-ignore
}, false);
// << timer-set-false
TKUnit.waitUntilReady(() => completed, 1);
timer.clearTimeout(id);
TKUnit.assert(completed, "Callback should be called in 0 seconds!");
}
export function test_setTimeout_callbackNotCalled() {
let completed = false;
const id = timer.setTimeout(() => completed = true, 10);
timer.clearTimeout(id);
TKUnit.wait(30 / 1000);
TKUnit.assert(!completed, "Callback should not be called after the specified time!");
}
export function test_setTimeout_shouldReturnNumber() {
let id = timer.setTimeout(() => {
//
});
timer.clearTimeout(id);
TKUnit.assertTrue(typeof id === "number", "Callback should return number!");
}
export function test_setTimeout_callbackShouldBeCleared() {
let completed = false;
// >> timer-set-fifty
const id = timer.setTimeout(() => {
// >> (hide)
completed = true;
// << (hide)
}, 50);
//// Clear timeout with specified id.
timer.clearTimeout(id);
// << timer-set-fifty
TKUnit.wait(0.060);
timer.clearTimeout(id);
TKUnit.assert(!completed, "Callback should be cleared when clearTimeout() is executed for specified id!");
}
export function test_setInterval_callbackCalledDuringPeriod(done) {
let counter = 0;
const expected = 4;
const timeLimit = 300;
const start = TKUnit.time();
// >> timer-set-expression
const id = timer.setInterval(() => {
// >> (hide)
counter++;
if (counter === expected) {
const end = TKUnit.time();
timer.clearInterval(id);
const time = end - start;
done(time > timeLimit ? new Error(`setInterval too slow. Actual time: ${time} timelimit: ${timeLimit}`) : null);
}
// << (hide)
}, 50);
// << timer-set-expression
}
export function test_setInterval_callbackCalledWithExtraArgs(done) {
let counter: number = 0;
const rnd: number = Math.random();
const start = TKUnit.time();
const id = timer.setInterval((arg) => {
counter += arg === rnd ? 1 : -1;
if (counter === 4) {
const end = TKUnit.time();
timer.clearInterval(id);
done(end - start > 250 ? new Error("setInterval too slow.") : null);
}
}, 50, rnd);
}
export function test_setInterval_callbackShouldBeCleared(done) {
const start = TKUnit.time();
// >> timer-set-interval
const id = timer.setInterval(() => {
// >> (hide)
const end = TKUnit.time();
timer.clearInterval(id);
done(end - start > 150 ? new Error("setInterval too slow.") : null);
// << (hide)
timer.clearInterval(id);
}, 50);
// << timer-set-interval
}
export function test_clearTimeout_multipleTimes_afterTick() {
let completed = false;
const id = timer.setTimeout(() => {
completed = true;
});
TKUnit.waitUntilReady(() => completed, 0.5);
TKUnit.assert(completed, "Callback should be called");
timer.clearTimeout(id);
timer.clearTimeout(id);
}
export function test_clearTimeout_immediatelyAfterCreate() {
let completed = false;
const id = timer.setTimeout(() => {
completed = true;
});
timer.clearTimeout(id);
TKUnit.wait(0.02);
TKUnit.assert(!completed, "Callback should not be called");
}
export function test_clearInterval_immediatelyAfterCreate() {
let completed = false;
const id = timer.setInterval(() => {
completed = true;
});
timer.clearInterval(id);
TKUnit.wait(0.05);
TKUnit.assert(!completed, "Callback should not be called");
}
export function test_clearTimeout_insideCallback() {
let completed = false;
let id = timer.setTimeout(() => {
completed = true;
timer.clearTimeout(id);
});
TKUnit.waitUntilReady(() => completed, 0.5);
TKUnit.assert(completed, "Callback should be called");
}