Skip to content

Commit ec93556

Browse files
committed
fix(test_lib): support comparing Maps in nested structures
1 parent af41fa9 commit ec93556

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

modules/test_lib/src/test_lib.es6

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,47 @@ window.print = function(msg) {
2020
}
2121
};
2222

23+
// Some Map polyfills don't polyfill Map.toString correctly, which
24+
// gives us bad error messages in tests.
25+
// The only way to do this in Jasmine is to monkey patch a method
26+
// to the object :-(
27+
window.Map.prototype.jasmineToString = function() {
28+
var m = this;
29+
if (!m) {
30+
return ''+m;
31+
}
32+
var res = [];
33+
m.forEach( (v,k) => {
34+
res.push(`${k}:${v}`);
35+
});
36+
return `{ ${res.join(',')} }`;
37+
}
38+
2339
window.beforeEach(function() {
2440
jasmine.addMatchers({
25-
// Custom handler for Map to give nice error messages in JavaScript
41+
// Custom handler for Map as Jasmine does not support it yet
2642
toEqual: function(util, customEqualityTesters) {
2743
return {
2844
compare: function(actual, expected) {
29-
var pass;
30-
if (actual instanceof Map) {
31-
pass = actual.size === expected.size;
32-
if (pass) {
33-
actual.forEach( (v,k) => {
34-
pass = pass && util.equals(v, expected.get(k));
35-
});
36-
}
37-
return {
38-
pass: pass,
39-
get message() {
40-
return `Expected ${mapToString(actual)} ${(pass ? 'not' : '')} to equal to ${mapToString(expected)}`;
41-
}
42-
};
43-
} else {
44-
return {
45-
pass: util.equals(actual, expected)
46-
}
47-
}
45+
return {
46+
pass: util.equals(actual, expected, [compareMap])
47+
};
4848
}
4949
};
50+
51+
function compareMap(actual, expected) {
52+
if (actual instanceof Map) {
53+
var pass = actual.size === expected.size;
54+
if (pass) {
55+
actual.forEach( (v,k) => {
56+
pass = pass && util.equals(v, expected.get(k));
57+
});
58+
}
59+
return pass;
60+
} else {
61+
return undefined;
62+
}
63+
}
5064
},
5165

5266
toBePromise: function() {
@@ -134,17 +148,6 @@ export class SpyObject {
134148
}
135149

136150

137-
function mapToString(m) {
138-
if (!m) {
139-
return ''+m;
140-
}
141-
var res = [];
142-
m.forEach( (v,k) => {
143-
res.push(`${k}:${v}`);
144-
});
145-
return `{ ${res.join(',')} }`;
146-
}
147-
148151
function elementText(n) {
149152
var hasShadowRoot = (n) => n instanceof Element && n.shadowRoot;
150153
var hasNodes = (n) => n.childNodes && n.childNodes.length > 0;

modules/test_lib/test/test_lib_spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ export function main() {
2626
expect(falseActual).not.toEqual(expected);
2727
});
2828

29-
it('should work for arrays of maps', () => {
29+
it('should work for arrays of string maps', () => {
3030
expect([{'a':'b'}]).toEqual([{'a':'b'}]);
3131
});
32+
33+
it('should work for arrays of real maps', () => {
34+
expect([MapWrapper.createFromStringMap({'a':'b'})]).toEqual([MapWrapper.createFromStringMap({'a':'b'})]);
35+
expect([MapWrapper.createFromStringMap({'a':'b'})]).not.toEqual([MapWrapper.createFromStringMap({'a':'c'})]);
36+
});
3237
});
3338

3439
describe('toEqual for Maps', () => {

0 commit comments

Comments
 (0)