Skip to content

Commit

Permalink
Update toEqualOptions, fix couple of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Feb 10, 2021
1 parent 56e453f commit 2c258c9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 39 deletions.
97 changes: 73 additions & 24 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,89 @@ 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;
}
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 {
Expand All @@ -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);
}
};
}
Expand Down
32 changes: 17 additions & 15 deletions test/specs/helpers.config.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('Chart.helpers.config', function() {
mainScope: (ctx) => ({
mainTest: ctx.contextValue,
subScope: {
subTest: 'a'
subText: 'a'
}
})
};
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 2c258c9

Please sign in to comment.