ajax_queue_test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. sinon.spy(App.ajax, 'send');
  26. });
  27. afterEach(function() {
  28. ajaxQueue.clear();
  29. ajaxQueue.runNextRequest.restore();
  30. ajaxQueue.finishedCallback.restore();
  31. App.ajax.send.restore();
  32. });
  33. describe('#clear', function() {
  34. it('should clear queue', function() {
  35. ajaxQueue.addRequest({name:'some', sender: Em.Object.create()});
  36. ajaxQueue.clear();
  37. expect(ajaxQueue.get('queue.length')).to.equal(0);
  38. });
  39. });
  40. describe('#addRequest', function() {
  41. it('should add request', function() {
  42. ajaxQueue.addRequest({name:'some', sender: Em.Object.create()});
  43. expect(ajaxQueue.get('queue.length')).to.equal(1);
  44. });
  45. it('should throw `name` error', function() {
  46. expect(function() {ajaxQueue.addRequest({name:'', sender: Em.Object.create()})}).to.throw(Error);
  47. });
  48. it('should throw `sender` error', function() {
  49. expect(function() {ajaxQueue.addRequest({name:'some', sender: {}})}).to.throw(Error);
  50. });
  51. });
  52. describe('#addRequests', function() {
  53. it('should add requests', function() {
  54. ajaxQueue.addRequests(Em.A([
  55. {name:'some', sender: Em.Object.create()},
  56. {name:'some2', sender: Em.Object.create()}
  57. ]));
  58. expect(ajaxQueue.get('queue.length')).to.equal(2);
  59. });
  60. it('should throw `name` error', function() {
  61. expect(function() {ajaxQueue.addRequests(Em.A([
  62. {name:'some', sender: Em.Object.create()},
  63. {name:'', sender: Em.Object.create()}
  64. ]));}).to.throw(Error);
  65. });
  66. it('should throw `sender` error', function() {
  67. expect(function() {ajaxQueue.addRequests(Em.A([
  68. {name:'some', sender: Em.Object.create()},
  69. {name:'some2', sender: {}}
  70. ]));}).to.throw(Error);
  71. });
  72. });
  73. describe('#start', function() {
  74. it('should call runNextRequest', function() {
  75. ajaxQueue.start();
  76. expect(ajaxQueue.runNextRequest.called).to.equal(true);
  77. });
  78. });
  79. describe('#runNextRequest', function() {
  80. it('for empty queue App.ajax.send shouldn\'t be called', function() {
  81. ajaxQueue.clear();
  82. ajaxQueue.runNextRequest();
  83. expect(App.ajax.send.called).to.equal(false);
  84. });
  85. it('when queue is empty finishedCallback should be called', function() {
  86. ajaxQueue.clear();
  87. ajaxQueue.runNextRequest();
  88. expect(ajaxQueue.finishedCallback.called).to.equal(true);
  89. });
  90. it('if abortOnError is false queue shouldn\'t be interrupted', function() {
  91. ajaxQueue.clear();
  92. ajaxQueue.set('abortOnError', false);
  93. ajaxQueue.addRequest({name:'some_fake', sender: Em.Object.create()}).addRequest({name: 'some_fake2', sender: Em.Object.create()}).start();
  94. expect(ajaxQueue.runNextRequest.callCount).to.equal(3); // One for empty-queue
  95. });
  96. });
  97. });