From 4dd1fd2dd5a89e799d65733566903c7542989ede Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 10 Nov 2016 12:09:20 +0100 Subject: [PATCH] feat(Utils): add tests for `includes` --- tests/ui/scripts/Utils/utils.js | 6 ++++- tests/ui/scripts/Utils/utils.spec.qml | 38 +++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/tests/ui/scripts/Utils/utils.js b/tests/ui/scripts/Utils/utils.js index 9bb6a0d49..0692b518e 100644 --- a/tests/ui/scripts/Utils/utils.js +++ b/tests/ui/scripts/Utils/utils.js @@ -343,7 +343,11 @@ function includes (obj, value, startIndex) { var length = obj.length for (var i = startIndex; i < length; i++) { - if (value === obj[i]) { + if ( + value === obj[i] || + // Check `NaN`. + (value !== value && obj[i] !== obj[i]) + ) { return true } } diff --git a/tests/ui/scripts/Utils/utils.spec.qml b/tests/ui/scripts/Utils/utils.spec.qml index c003b9c47..a68575a2c 100644 --- a/tests/ui/scripts/Utils/utils.spec.qml +++ b/tests/ui/scripts/Utils/utils.spec.qml @@ -181,7 +181,40 @@ TestCase { // ----------------------------------------------------------------- - function test_isArray_data () { + function test_includes_data () { + return [ + // Test arrays. + { input: [], value: 0, output: false }, + { input: [ 1, 2, 3 ], value: 4, output: false }, + { input: [ 6 ], value: 6, output: true }, + { input: [ 4, 8, 'foo' ], value: 8, output: true }, + { input: [ 12, NaN, 47 ], value: NaN, output: true }, + { input: Array(1), value: undefined, output: true }, + { input: [ 'a', 'b', 'c' ], startIndex: 1, value: 'a', output: false }, + { input: [ 6, 5, 4, 9 ], startIndex: 3, value: 9, output: true }, + + // Test objects. + { input: {}, value: 0, output: false }, + { input: { a: 1, b: 2, c: 3 }, value: 4, output: false }, + { input: { a: 6 }, value: 6, output: true }, + { input: { a: 4, b: 8, c: 'foo' }, value: 8, output: true }, + { input: { a: 12, b: NaN, c: 47 }, value: NaN, output: true }, + { input: new Object(), value: undefined, output: false }, + { input: { a: 'a', b: 'b', c: 'c' }, startIndex: 1, value: 'a', output: false }, + { input: { a: 6, b: 5, c: 4, d: 9 }, startIndex: 3, value: 9, output: true }, + ] + } + + function test_includes (data) { + compare( + Utils.includes(data.input, data.value, data.startIndex), + data.output + ) + } + + // ----------------------------------------------------------------- + + function test_isArray_data () { return [ { input: [], output: true }, { input: {}, output: false }, @@ -190,7 +223,8 @@ TestCase { { input: new Error, output: false }, { input: true, output: false }, { input: 42, output: false }, - { input: new Array(), output: true } + { input: new Array(), output: true }, + { input: [ 15, new Date(), 'ij' ], output: true } ] }