|
| 1 | +import TKUnit = require("Tests/TKUnit"); |
| 2 | +var timer = require("timer/timer"); |
| 3 | + |
| 4 | +// <snippet name="timer"> |
| 5 | +// # Timer module |
| 6 | +// ``` JavaScript |
| 7 | +// require("globals"); |
| 8 | +//// OR |
| 9 | +// var timer = require("timer"); |
| 10 | +// ``` |
| 11 | +// </snippet> |
| 12 | + |
| 13 | +export var test_setTimeout_isDefined = function () { |
| 14 | + TKUnit.assert(typeof (timer.setTimeout) !== "undefined", "Method timer.setTimeout() should be defined!"); |
| 15 | +}; |
| 16 | + |
| 17 | +export var test_clearTimeout_isDefined = function () { |
| 18 | + TKUnit.assert(typeof (timer.clearTimeout) !== "undefined", "Method timer.clearTimeout() should be defined!"); |
| 19 | +}; |
| 20 | + |
| 21 | +export var test_setInterval_isDefined = function () { |
| 22 | + TKUnit.assert(typeof (timer.setInterval) !== "undefined", "Method timer.setInterval() should be defined!"); |
| 23 | +}; |
| 24 | + |
| 25 | +export var test_clearInterval_isDefined = function () { |
| 26 | + TKUnit.assert(typeof (timer.clearInterval) !== "undefined", "Method timer.clearInterval() should be defined!"); |
| 27 | +}; |
| 28 | + |
| 29 | +export var test_setTimeout = function () { |
| 30 | + var completed: boolean; |
| 31 | + var isReady = function () { return completed; } |
| 32 | + |
| 33 | + // <snippet name="timer"> |
| 34 | + // ### Evaluates an expression after 0 milliseconds. |
| 35 | + // ``` JavaScript |
| 36 | + timer.setTimeout(function () { |
| 37 | + // <hide> |
| 38 | + completed = true; |
| 39 | + // </hide> |
| 40 | + }); |
| 41 | + // ``` |
| 42 | + // </snippet> |
| 43 | + |
| 44 | + TKUnit.waitUntilReady(isReady, 0.5); |
| 45 | + TKUnit.assert(completed, "Callback should be called!"); |
| 46 | +}; |
| 47 | + |
| 48 | +export var test_setTimeout_callbackCalledAfterSpecifedTime = function () { |
| 49 | + var completed: boolean; |
| 50 | + var isReady = function () { return completed; } |
| 51 | + |
| 52 | + // <snippet name="timer"> |
| 53 | + // ### Evaluates an expression after a specified number of milliseconds. |
| 54 | + // ``` JavaScript |
| 55 | + timer.setTimeout(function () { |
| 56 | + // <hide> |
| 57 | + completed = true; |
| 58 | + // </hide> |
| 59 | + }, 500); |
| 60 | + // ``` |
| 61 | + // </snippet> |
| 62 | + |
| 63 | + TKUnit.waitUntilReady(isReady, 1); |
| 64 | + TKUnit.assert(completed, "Callback should be called after specified time!"); |
| 65 | +}; |
| 66 | + |
| 67 | +export var test_setTimeout_callbackNotCalled = function () { |
| 68 | + var completed: boolean; |
| 69 | + var isReady = function () { return completed; } |
| 70 | + |
| 71 | + timer.setTimeout(function () { |
| 72 | + completed = true; |
| 73 | + }, 1000); |
| 74 | + |
| 75 | + TKUnit.waitUntilReady(isReady, 0.5); |
| 76 | + TKUnit.assert(!completed, "Callback should be called after specified time!"); |
| 77 | +}; |
| 78 | + |
| 79 | +export var test_setTimeout_shouldReturnNumber = function () { |
| 80 | + var id = timer.setTimeout(function () { }); |
| 81 | + TKUnit.assert(typeof id === "number", "Callback should return number!"); |
| 82 | +}; |
| 83 | + |
| 84 | +export var test_setTimeout_callbackShouldBeCleared = function () { |
| 85 | + var completed: boolean; |
| 86 | + var isReady = function () { return completed; } |
| 87 | + |
| 88 | + // <snippet name="timer"> |
| 89 | + // ### Cancels the evaluation with the clearTimeout method. |
| 90 | + // ``` JavaScript |
| 91 | + var id = timer.setTimeout(function () { |
| 92 | + // <hide> |
| 93 | + completed = true; |
| 94 | + // </hide> |
| 95 | + }, 2000); |
| 96 | + |
| 97 | + //// Clear timeout with specified id. |
| 98 | + timer.clearTimeout(id); |
| 99 | + |
| 100 | + // ``` |
| 101 | + // </snippet> |
| 102 | + |
| 103 | + TKUnit.waitUntilReady(isReady, 3); |
| 104 | + TKUnit.assert(!completed, "Callback should be cleared when clearTimeout() is executed for specified id!"); |
| 105 | +}; |
| 106 | + |
| 107 | +export var test_setInterval_callbackCalledDuringPeriod = function () { |
| 108 | + var counter = 0; |
| 109 | + var expected = 4; |
| 110 | + var isReady = function () { return counter >= expected; } |
| 111 | + |
| 112 | + // <snippet name="timer"> |
| 113 | + // ### Evaluates an expression each time a specified number of milliseconds has elapsed. |
| 114 | + // ``` JavaScript |
| 115 | + timer.setInterval(function () { |
| 116 | + // <hide> |
| 117 | + counter++; |
| 118 | + // </hide> |
| 119 | + }, 100); |
| 120 | + // ``` |
| 121 | + // </snippet> |
| 122 | + |
| 123 | + TKUnit.waitUntilReady(isReady, 0.5); |
| 124 | + TKUnit.assert(isReady(), "Callback should be raised at least" + expected + "times! Callback raised " + counter + " times."); |
| 125 | +}; |
| 126 | + |
| 127 | +export var test_setInterval_callbackShouldBeCleared = function () { |
| 128 | + var counter = 0; |
| 129 | + var expected = 1; |
| 130 | + var isReady = function () { return counter == expected; } |
| 131 | + |
| 132 | + // <snippet name="timer"> |
| 133 | + // ### Cancel the interval previously started using the setInterval method. |
| 134 | + // ``` JavaScript |
| 135 | + var id = timer.setInterval(function () { |
| 136 | + // <hide> |
| 137 | + counter++; |
| 138 | + // </hide> |
| 139 | + timer.clearInterval(id); |
| 140 | + }, 100); |
| 141 | + // ``` |
| 142 | + // </snippet> |
| 143 | + |
| 144 | + TKUnit.waitUntilReady(isReady, 0.5); |
| 145 | + TKUnit.assert(isReady(), "Callback should be raised only once!"); |
| 146 | +}; |
0 commit comments