ajax_queue_test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var ajaxQueue;
  20. describe('App.ajaxQueue', function () {
  21. beforeEach(function() {
  22. ajaxQueue = App.ajaxQueue.create();
  23. sinon.spy(ajaxQueue, 'runNextRequest');
  24. sinon.spy(ajaxQueue, 'finishedCallback');
  25. });
  26. afterEach(function() {
  27. ajaxQueue.clear();
  28. ajaxQueue.runNextRequest.restore();
  29. ajaxQueue.finishedCallback.restore();
  30. });
  31. describe('#clear', function() {
  32. it('should clear queue', function() {
  33. ajaxQueue.addRequest({name:'some', sender: Em.Object.create()});
  34. ajaxQueue.clear();
  35. expect(ajaxQueue.get('queue.length')).to.equal(0);
  36. });
  37. });
  38. describe('#addRequest', function() {
  39. it('should add request', function() {
  40. ajaxQueue.addRequest({name:'some', sender: Em.Object.create()});
  41. expect(ajaxQueue.get('queue.length')).to.equal(1);
  42. });
  43. it('should throw `name` error', function() {
  44. expect(function() {ajaxQueue.addRequest({name:'', sender: Em.Object.create()})}).to.throw(Error);
  45. });
  46. it('should throw `sender` error', function() {
  47. expect(function() {ajaxQueue.addRequest({name:'some', sender: {}})}).to.throw(Error);
  48. });
  49. });
  50. describe('#addRequests', function() {
  51. it('should add requests', function() {
  52. ajaxQueue.addRequests(Em.A([
  53. {name:'some', sender: Em.Object.create()},
  54. {name:'some2', sender: Em.Object.create()}
  55. ]));
  56. expect(ajaxQueue.get('queue.length')).to.equal(2);
  57. });
  58. it('should throw `name` error', function() {
  59. expect(function() {ajaxQueue.addRequests(Em.A([
  60. {name:'some', sender: Em.Object.create()},
  61. {name:'', sender: Em.Object.create()}
  62. ]));}).to.throw(Error);
  63. });
  64. it('should throw `sender` error', function() {
  65. expect(function() {ajaxQueue.addRequests(Em.A([
  66. {name:'some', sender: Em.Object.create()},
  67. {name:'some2', sender: {}}
  68. ]));}).to.throw(Error);
  69. });
  70. });
  71. describe('#start', function() {
  72. it('should call runNextRequest', function() {
  73. ajaxQueue.start();
  74. expect(ajaxQueue.runNextRequest.called).to.equal(true);
  75. });
  76. });
  77. describe('#runNextRequest', function() {
  78. it('for empty queue App.ajax.send shouldn\'t be called', function() {
  79. ajaxQueue.clear();
  80. ajaxQueue.runNextRequest();
  81. expect(App.ajax.send.called).to.equal(false);
  82. });
  83. it('when queue is empty finishedCallback should be called', function() {
  84. ajaxQueue.clear();
  85. ajaxQueue.runNextRequest();
  86. expect(ajaxQueue.finishedCallback.called).to.equal(true);
  87. });
  88. });
  89. });