reload_popup_test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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('#popupText', function () {
  26. var cases = [
  27. {
  28. result: Em.I18n.t('app.reloadPopup.text'),
  29. title: 'should show modal popup with default message'
  30. },
  31. {
  32. text: 'text',
  33. result: 'text',
  34. title: 'should show modal popup with custom message'
  35. }
  36. ];
  37. cases.forEach(function (item) {
  38. it(item.title, function () {
  39. expect(obj.popupText(item.text)).to.equal(item.result);
  40. });
  41. });
  42. });
  43. describe('#closeReloadPopup', function () {
  44. it('should hide modal popup', function () {
  45. obj.showReloadPopup();
  46. obj.closeReloadPopup();
  47. expect(obj.get('reloadPopup')).to.be.null;
  48. });
  49. });
  50. describe('#reloadSuccessCallback', function () {
  51. it('should hide modal popup', function () {
  52. obj.showReloadPopup();
  53. obj.reloadSuccessCallback();
  54. expect(obj.get('reloadPopup')).to.be.null;
  55. });
  56. });
  57. describe('#reloadErrorCallback', function () {
  58. var clock,
  59. cases = [
  60. {
  61. args: [{status: 404}, null, null, {}, {shouldUseDefaultHandler: true}],
  62. closeReloadPopupCallCount: 1,
  63. defaultErrorHandlerCallCount: 1,
  64. showReloadPopupCallCount: 0,
  65. isCallbackCalled: false,
  66. title: 'status received, default error handler'
  67. },
  68. {
  69. args: [{status: 404}, null, null, {}, {}],
  70. closeReloadPopupCallCount: 1,
  71. defaultErrorHandlerCallCount: 0,
  72. showReloadPopupCallCount: 0,
  73. isCallbackCalled: false,
  74. title: 'status received, no default error handler'
  75. },
  76. {
  77. args: [{status: 0}, null, null, {}, {}],
  78. closeReloadPopupCallCount: 0,
  79. defaultErrorHandlerCallCount: 0,
  80. showReloadPopupCallCount: 1,
  81. isCallbackCalled: false,
  82. title: 'no status received, no callback'
  83. },
  84. {
  85. args: [{status: 0}, null, null, {}, {callback: Em.K, timeout: 2000}],
  86. timeout: 1999,
  87. closeReloadPopupCallCount: 0,
  88. defaultErrorHandlerCallCount: 0,
  89. showReloadPopupCallCount: 1,
  90. isCallbackCalled: false,
  91. title: 'no status received, callback specified, custom timeout, not enough time passed'
  92. },
  93. {
  94. args: [{status: 0}, null, null, {}, {callback: Em.K}],
  95. timeout: 999,
  96. closeReloadPopupCallCount: 0,
  97. defaultErrorHandlerCallCount: 0,
  98. showReloadPopupCallCount: 1,
  99. isCallbackCalled: false,
  100. title: 'no status received, callback specified, default timeout, not enough time passed'
  101. },
  102. {
  103. args: [{status: 0}, null, null, {}, {callback: Em.K, args: [{}], timeout: 2000}],
  104. timeout: 2000,
  105. closeReloadPopupCallCount: 0,
  106. defaultErrorHandlerCallCount: 0,
  107. showReloadPopupCallCount: 1,
  108. isCallbackCalled: true,
  109. callbackArgs: [{}],
  110. title: 'no status received, callback with arguments specified, custom timeout, enough time passed'
  111. },
  112. {
  113. args: [{status: 0}, null, null, {}, {callback: Em.K}],
  114. timeout: 1000,
  115. closeReloadPopupCallCount: 0,
  116. defaultErrorHandlerCallCount: 0,
  117. showReloadPopupCallCount: 1,
  118. isCallbackCalled: true,
  119. callbackArgs: [],
  120. title: 'no status received, callback with no arguments specified, default timeout, enough time passed'
  121. }
  122. ];
  123. cases.forEach(function (item) {
  124. describe(item.title, function () {
  125. beforeEach(function () {
  126. sinon.stub(obj, 'closeReloadPopup', Em.K);
  127. sinon.stub(App.ajax, 'defaultErrorHandler', Em.K);
  128. sinon.stub(obj, 'showReloadPopup', Em.K);
  129. sinon.stub(App, 'get').withArgs('timeout').returns(1000);
  130. if (item.args[4].callback) {
  131. sinon.spy(item.args[4], 'callback');
  132. }
  133. clock = sinon.useFakeTimers();
  134. obj.reloadErrorCallback.apply(obj, item.args);
  135. clock.tick(item.timeout);
  136. });
  137. afterEach(function () {
  138. obj.closeReloadPopup.restore();
  139. App.ajax.defaultErrorHandler.restore();
  140. obj.showReloadPopup.restore();
  141. App.get.restore();
  142. if (item.args[4].callback) {
  143. item.args[4].callback.restore();
  144. }
  145. clock.restore();
  146. });
  147. it('closeReloadPopup call', function () {
  148. expect(obj.closeReloadPopup.callCount).to.equal(item.closeReloadPopupCallCount);
  149. });
  150. it('defaultErrorHandler call', function () {
  151. expect(App.ajax.defaultErrorHandler.callCount).to.equal(item.defaultErrorHandlerCallCount);
  152. });
  153. it('showReloadPopup call', function () {
  154. expect(obj.showReloadPopup.callCount).to.equal(item.showReloadPopupCallCount);
  155. });
  156. if (item.isCallbackCalled) {
  157. it('callback call', function () {
  158. expect(item.args[4].callback.calledOnce).to.be.true;
  159. });
  160. it('callback context', function () {
  161. expect(item.args[4].callback.calledOn(obj)).to.be.true;
  162. });
  163. it('callback arguments', function () {
  164. expect(item.args[4].callback.firstCall.args).to.eql(item.callbackArgs);
  165. });
  166. }
  167. });
  168. });
  169. });
  170. });