reload_popup_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. require('mixins/common/reload_popup');
  20. describe('App.ReloadPopupMixin', function () {
  21. var obj;
  22. beforeEach(function () {
  23. obj = Em.Object.create(App.ReloadPopupMixin);
  24. });
  25. describe('#showReloadPopup', function () {
  26. var spanRegExp = new RegExp('<span>([\\s\\S]+)<\/span>'),
  27. cases = [
  28. {
  29. result: Em.I18n.t('app.reloadPopup.text'),
  30. title: 'should show modal popup with default message'
  31. },
  32. {
  33. text: 'text',
  34. result: 'text',
  35. title: 'should show modal popup with custom message'
  36. }
  37. ];
  38. beforeEach(function () {
  39. sinon.stub(App.ModalPopup, 'show', function (popup) {
  40. return popup.body;
  41. });
  42. });
  43. afterEach(function () {
  44. App.ModalPopup.show.restore();
  45. });
  46. cases.forEach(function (item) {
  47. it(item.title, function () {
  48. obj.showReloadPopup(item.text);
  49. expect(obj.get('reloadPopup').match(spanRegExp)[1]).to.equal(item.result);
  50. });
  51. });
  52. });
  53. describe('#closeReloadPopup', function () {
  54. it('should hide modal popup', function () {
  55. obj.set('retryCount', 1);
  56. obj.showReloadPopup();
  57. obj.closeReloadPopup();
  58. expect(obj.get('reloadPopup')).to.be.null;
  59. expect(obj.get('retryCount')).to.equal(0);
  60. });
  61. });
  62. describe('#reloadSuccessCallback', function () {
  63. it('should hide modal popup', function () {
  64. obj.set('retryCount', 1);
  65. obj.showReloadPopup();
  66. obj.reloadSuccessCallback();
  67. expect(obj.get('reloadPopup')).to.be.null;
  68. expect(obj.get('retryCount')).to.equal(0);
  69. });
  70. });
  71. describe('#reloadErrorCallback', function () {
  72. var cases = [
  73. {
  74. args: [{status: 404}, null, null, {}, {shouldUseDefaultHandler: true}],
  75. closeReloadPopupCallCount: 1,
  76. consoleLogCallCount: 0,
  77. defaultErrorHandlerCallCount: 1,
  78. showReloadPopupCallCount: 0,
  79. setTimeoutCount: 0,
  80. title: 'status received, no console message, default error handler'
  81. },
  82. {
  83. args: [{status: 404}, null, null, {}, {errorLogMessage: 'error'}],
  84. closeReloadPopupCallCount: 1,
  85. consoleLogCallCount: 1,
  86. defaultErrorHandlerCallCount: 0,
  87. showReloadPopupCallCount: 0,
  88. setTimeoutCount: 0,
  89. title: 'status received, console message displayed, no default error handler'
  90. },
  91. {
  92. args: [{status: 0}, null, null, {}, {times: 5}],
  93. retryCount: 5,
  94. retryCountResult: 5,
  95. closeReloadPopupCallCount: 0,
  96. consoleLogCallCount: 0,
  97. defaultErrorHandlerCallCount: 0,
  98. showReloadPopupCallCount: 1,
  99. setTimeoutCount: 0,
  100. title: 'no status received, custom retries count, max retries reached'
  101. },
  102. {
  103. args: [{status: 0}, null, null, {}, {}],
  104. retryCount: 2,
  105. retryCountResult: 3,
  106. closeReloadPopupCallCount: 0,
  107. consoleLogCallCount: 0,
  108. defaultErrorHandlerCallCount: 0,
  109. showReloadPopupCallCount: 1,
  110. setTimeoutCount: 0,
  111. title: 'no status received, default retries count, max retries not reached, no callback'
  112. },
  113. {
  114. args: [{status: 0}, null, null, {}, {callback: Em.K}],
  115. retryCount: 2,
  116. retryCountResult: 3,
  117. closeReloadPopupCallCount: 0,
  118. consoleLogCallCount: 0,
  119. defaultErrorHandlerCallCount: 0,
  120. showReloadPopupCallCount: 1,
  121. setTimeoutCount: 1,
  122. title: 'no status received, default retries count, max retries not reached, callback specified'
  123. }
  124. ];
  125. beforeEach(function () {
  126. sinon.stub(obj, 'closeReloadPopup', Em.K);
  127. sinon.stub(console, 'log', Em.K);
  128. sinon.stub(App.ajax, 'defaultErrorHandler', Em.K);
  129. sinon.stub(obj, 'showReloadPopup', Em.K);
  130. sinon.stub(App, 'get').withArgs('maxRetries').returns(3);
  131. sinon.stub(window, 'setTimeout', Em.K);
  132. });
  133. afterEach(function () {
  134. obj.closeReloadPopup.restore();
  135. console.log.restore();
  136. App.ajax.defaultErrorHandler.restore();
  137. obj.showReloadPopup.restore();
  138. App.get.restore();
  139. window.setTimeout.restore();
  140. });
  141. cases.forEach(function (item) {
  142. it(item.title, function () {
  143. if (!Em.isNone(item.retryCount)) {
  144. obj.set('retryCount', item.retryCount);
  145. }
  146. obj.reloadErrorCallback.apply(obj, item.args);
  147. expect(obj.closeReloadPopup.callCount).to.equal(item.closeReloadPopupCallCount);
  148. expect(console.log.callCount).to.equal(item.consoleLogCallCount);
  149. expect(App.ajax.defaultErrorHandler.callCount).to.equal(item.defaultErrorHandlerCallCount);
  150. expect(obj.showReloadPopup.callCount).to.equal(item.showReloadPopupCallCount);
  151. expect(window.setTimeout.callCount).to.equal(item.setTimeoutCount);
  152. if (!Em.isNone(item.retryCountResult)) {
  153. obj.set('retryCount', item.retryCountResult);
  154. }
  155. });
  156. });
  157. });
  158. });