diff --git a/test/index.js b/test/index.js index 176ccd2f80c..eed7a1d230f 100644 --- a/test/index.js +++ b/test/index.js @@ -19,40 +19,71 @@ jasmine.fixture = { jasmine.triggerMouseEvent = triggerMouseEvent; function isObject(value) { - return value !== null && Object.prototype.toString.call(value) === '[object Object]'; + return Object.prototype.toString.call(value) === '[object Object]'; } function isArray(value) { - return value !== null && Object.prototype.toString.call(value) === '[object Array]'; + return Object.prototype.toString.call(value) === '[object Array]'; } -function compareOption(val, act, path) { +function fmt(val) { + if (typeof val === 'string') { + return `"${val}"`; + } + if (isArray(val) || isObject(val)) { + return JSON.stringify(val); + } + return `${val}`; +} + +function compareArray(actual, expected, path) { let ret = true; + const diff = []; - if (isObject(val)) { - ret = compareOptions(act, val, path); - } else if (isArray(val)) { - ret = val.length === act.length; - if (ret) { - for (let i = 0; i < val.length; i++) { - if (!compareOption(val[i], act[i], path)) { - ret = false; - break; - } + if (!isArray(actual)) { + diff.push(`${path}: Expected ${fmt(actual)} to be an array`); + ret = false; + actual = {}; + } + + if (actual.length !== expected.length) { + diff.push(`${path}.length: Expected ${actual.length} to equal ${expected.length}`); + ret = false; + } + for (let i = 0; i < expected.length; i++) { + const act = actual[i]; + const exp = expected[i]; + const cmp = compareOption(act, exp, `${path}[${i}]`); + if (isObject(cmp)) { + if (!cmp.pass) { + diff.push(cmp.message); + ret = false; } + } else if (!cmp) { + diff.push(`${path}[${i}]: Expected ${fmt(act)} to equal ${fmt(exp)}`); + ret = false; } - } else { - ret = act === val; } - return ret; + return { + pass: ret, + message: diff.join('\n') + }; } -function compareOptions(actual, expected, path = '') { +function compareObject(actual, expected, path = '') { let ret = true; - let diff = []; + const diff = []; + + if (!isObject(actual)) { + diff.push(`${path}: Expected ${fmt(actual)} to be an object`); + ret = false; + actual = {}; + } + if (path !== '') { path = path + '.'; } + for (const key in expected) { if (typeof key === 'string' && key.startsWith('_')) { continue; @@ -60,12 +91,17 @@ function compareOptions(actual, expected, path = '') { if (!Object.prototype.hasOwnProperty.call(expected, key)) { continue; } - const val = expected[key]; const act = actual[key]; - ret = compareOption(val, act, `${path}${key}`); - if (!ret) { - diff.push(`${path}${key}: Expected '${actual[key]}' to be '${val}'`); - break; + const exp = expected[key]; + const cmp = compareOption(act, exp, `${path}${key}`); + if (isObject(cmp)) { + if (!cmp.pass) { + diff.push(cmp.message); + ret = false; + } + } else if (!cmp) { + diff.push(`${path}${key}: Expected ${fmt(act)} to equal ${fmt(exp)}`); + ret = false; } } return { @@ -74,13 +110,26 @@ function compareOptions(actual, expected, path = '') { }; } +function compareOption(actual, expected, path) { + let ret = true; + + if (isObject(expected)) { + ret = compareObject(actual, expected, path); + } else if (isArray(expected)) { + ret = compareArray(actual, expected, path); + } else { + ret = actual === expected; + } + return ret; +} + beforeEach(function() { addMatchers(); jasmine.addMatchers({ toEqualOptions() { return { compare(actual, expected) { - return compareOptions(actual, expected); + return compareObject(actual, expected); } }; } diff --git a/test/specs/helpers.config.tests.js b/test/specs/helpers.config.tests.js index ad46f9f2199..7fd801be6ac 100644 --- a/test/specs/helpers.config.tests.js +++ b/test/specs/helpers.config.tests.js @@ -174,7 +174,7 @@ describe('Chart.helpers.config', function() { mainScope: (ctx) => ({ mainTest: ctx.contextValue, subScope: { - subTest: 'a' + subText: 'a' } }) }; @@ -205,20 +205,22 @@ describe('Chart.helpers.config', function() { labels: [{text: 'a'}, {text: 'b'}, {value: 1}] }; const opts = _attachContext(_createResolver([options, defaults]), {text: 'context'}); - expect(opts.labels).toEqualOptions([ - { - text: 'a', - value: 42 - }, - { - text: 'b', - value: 42 - }, - { - text: 'context', - value: 1 - } - ]); + expect(opts).toEqualOptions({ + labels: [ + { + text: 'a', + value: 42 + }, + { + text: 'b', + value: 42 + }, + { + text: 'context', + value: 1 + } + ] + }); }); it('should support overriding options', function() {