remotearraymatcher_test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.ui.ac.RemoteArrayMatcherTest');
  15. goog.setTestOnly('goog.ui.ac.RemoteArrayMatcherTest');
  16. goog.require('goog.net.XhrIo');
  17. goog.require('goog.testing.MockControl');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.testing.net.XhrIo');
  20. goog.require('goog.ui.ac.RemoteArrayMatcher');
  21. var url = 'http://www.google.com';
  22. var token = 'goog';
  23. var maxMatches = 5;
  24. var fullToken = 'google';
  25. var responseJsonText = '["eric", "larry", "sergey", "marissa", "pupius"]';
  26. var responseJson = JSON.parse(responseJsonText);
  27. var mockControl;
  28. var mockMatchHandler;
  29. function setUp() {
  30. goog.net.XhrIo = goog.testing.net.XhrIo;
  31. mockControl = new goog.testing.MockControl();
  32. mockMatchHandler = mockControl.createFunctionMock();
  33. }
  34. function testRequestMatchingRows_noSimilarTrue() {
  35. var matcher = new goog.ui.ac.RemoteArrayMatcher(url);
  36. mockMatchHandler(token, responseJson);
  37. mockControl.$replayAll();
  38. matcher.requestMatchingRows(token, maxMatches, mockMatchHandler, fullToken);
  39. matcher.xhr_.simulateResponse(200, responseJsonText);
  40. mockControl.$verifyAll();
  41. mockControl.$resetAll();
  42. }
  43. function testRequestMatchingRows_twoCalls() {
  44. var matcher = new goog.ui.ac.RemoteArrayMatcher(url);
  45. var dummyMatchHandler = mockControl.createFunctionMock();
  46. mockMatchHandler(token, responseJson);
  47. mockControl.$replayAll();
  48. matcher.requestMatchingRows(token, maxMatches, dummyMatchHandler, fullToken);
  49. matcher.requestMatchingRows(token, maxMatches, mockMatchHandler, fullToken);
  50. matcher.xhr_.simulateResponse(200, responseJsonText);
  51. mockControl.$verifyAll();
  52. mockControl.$resetAll();
  53. }