77goog . module ( 'goog.ui.ac.RichRemoteArrayMatcherTest' ) ;
88goog . setTestOnly ( ) ;
99
10+ const ArgumentMatcher = goog . require ( 'goog.testing.mockmatchers.ArgumentMatcher' ) ;
1011const MockControl = goog . require ( 'goog.testing.MockControl' ) ;
1112const NetXhrIo = goog . require ( 'goog.testing.net.XhrIo' ) ;
1213const RichRemoteArrayMatcher = goog . require ( 'goog.ui.ac.RichRemoteArrayMatcher' ) ;
@@ -18,8 +19,26 @@ const url = 'http://www.google.com';
1819const token = 'goog' ;
1920const maxMatches = 5 ;
2021
21- const responseJsonText = '[["type1", "eric", "larry", "sergey"]]' ;
22- const responseJsonType1 = [ 'eric' , 'larry' , 'sergey' ] ;
22+ const responseJsonText =
23+ '[["type1", {"name":"eric"}, {"name":"larry"}, {"name":"sergey"}]]' ;
24+ // This matcher is used to match the value used in the `matchHandler` callback
25+ // in tests.
26+ // The `RichRemoteArrayMatcher` takes in the parsed `responseJsonTest`
27+ // above and augments each object within the array with methods that it defines.
28+ // By default mocks do === comparison between the expected and actual value,
29+ // so to avoid copying those method implementations into the test, we instead
30+ // implement a matcher that checks to see that the value given to the callback
31+ // is roughly what we expected it to be: an array whose objects have the names
32+ // listed above.
33+ // Effectively, this is structurally matching the following:
34+ // [{name: 'eric'},{name:'larry'},{name:'sergey'}]
35+ const ignoresRenderAndSelectMatcher = new ArgumentMatcher ( ( arg ) => {
36+ if ( ! Array . isArray ( arg ) ) {
37+ return false ;
38+ }
39+ return arg [ 0 ] . name === 'eric' && arg [ 1 ] . name === 'larry' &&
40+ arg [ 2 ] . name === 'sergey' ;
41+ } , 'matchesType1' ) ;
2342
2443let mockControl ;
2544let mockMatchHandler ;
@@ -42,7 +61,7 @@ testSuite({
4261
4362 testRequestMatchingRows ( ) {
4463 const matcher = new RichRemoteArrayMatcher ( url ) ;
45- mockMatchHandler ( token , responseJsonType1 ) ;
64+ mockMatchHandler ( token , ignoresRenderAndSelectMatcher ) ;
4665 mockControl . $replayAll ( ) ;
4766 matcher . requestMatchingRows ( token , maxMatches , mockMatchHandler ) ;
4867 matcher . xhr_ . simulateResponse ( 200 , responseJsonText ) ;
@@ -56,7 +75,7 @@ testSuite({
5675 assertEquals ( 'type1' , type ) ;
5776 return response ;
5877 } ) ;
59- mockMatchHandler ( token , responseJsonType1 ) ;
78+ mockMatchHandler ( token , ignoresRenderAndSelectMatcher ) ;
6079 mockControl . $replayAll ( ) ;
6180 matcher . requestMatchingRows ( token , maxMatches , mockMatchHandler ) ;
6281 matcher . xhr_ . simulateResponse ( 200 , responseJsonText ) ;
0 commit comments