Commit 5d65384b authored by Ronan Abhamon's avatar Ronan Abhamon

feat(utils.spec.qml): add tests on `clearTimeout`, `isString`

parent cb131ed7
......@@ -50,8 +50,6 @@ function openWindow (window, parent, options) {
object.show()
}
// -------------------------------------------------------------------
// Display a simple ConfirmDialog component.
// Wrap the openWindow function.
function openConfirmDialog (parent, options) {
......@@ -71,6 +69,15 @@ function openConfirmDialog (parent, options) {
// -------------------------------------------------------------------
function _computeOptimizedCb (func, context) {
return (context != null)
? (function () {
return func.apply(context, arguments)
}) : func
}
// -------------------------------------------------------------------
function snakeToCamel (s) {
return s.replace(/(\_\w)/g, function (matches) {
return matches[1].toUpperCase()
......@@ -97,7 +104,21 @@ function setTimeout (delay, cb) {
}
function clearTimeout (timer) {
timer.destroy() // Unnecessary call: `timer.stop()`
timer.stop() // NECESSARY.
timer.destroy()
}
// -------------------------------------------------------------------
function times (n, cb, context) {
var arr = Array(Math.max(0, n))
cb = _computeOptimizedCb(cb, context, 1)
for (var i = 0; i < n; i++) {
arr[i] = cb(i)
}
return arr
}
// -------------------------------------------------------------------
......
......@@ -25,6 +25,8 @@ TestCase {
compare(Utils.snakeToCamel(data.input), data.output)
}
// -----------------------------------------------------------------
function test_setTimeoutWithoutParent () {
try {
Utils.setTimeout(0, function () {
......@@ -58,4 +60,54 @@ TestCase {
fail('`setTimeout` failed because callback it was not called in due course')
}
}
// -----------------------------------------------------------------
function test_clearTimeout_data () {
return [
{ time: 0 },
{ time: 100 }
]
}
function test_clearTimeout (data) {
var failed = false
var timeout = Utils.setTimeout.call(testCase, data.time, function () {
failed = true
})
// Simulate time
Utils.times(500000, function (i) {
// Nothing.
})
if (failed) {
fail('`setTimeout` callback was called')
}
Utils.clearTimeout(timeout)
wait(100)
if (failed) {
fail('`setTimeout` callback was called')
}
}
// -----------------------------------------------------------------
function test_isString_data () {
return [
{ input: 'foo', output: true },
{ input: Object('bar'), output: true },
{ input: [ 0 ], output: false },
{ input: /baz/, output: false },
{ input: new Error, output: false },
{ input: true, output: false },
{ input: 42, output: false }
]
}
function test_isString (data) {
compare(Utils.isString(data.input), data.output)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment