@@ -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+
2339window . 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-
148151function elementText ( n ) {
149152 var hasShadowRoot = ( n ) => n instanceof Element && n . shadowRoot ;
150153 var hasNodes = ( n ) => n . childNodes && n . childNodes . length > 0 ;
0 commit comments