modal_popup_test.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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('views/common/modal_popup');
  20. describe('App.ModalPopup', function() {
  21. var popup;
  22. beforeEach(function () {
  23. popup = App.ModalPopup.create(
  24. {
  25. primary: 'test',
  26. secondary: 'test',
  27. header: 'test',
  28. body: '<p>text</p><input type="text"><input type="checkbox"><input type="button">',
  29. $: function () {
  30. return $(this);
  31. }
  32. }
  33. );
  34. });
  35. describe('#didInsertElement', function () {
  36. beforeEach(function () {
  37. this.spy = sinon.spy(popup, "focusElement");
  38. });
  39. afterEach(function () {
  40. this.spy.restore();
  41. });
  42. it('should focus on the first input element', function () {
  43. popup.didInsertElement();
  44. expect(this.spy.called).to.be.true;
  45. });
  46. });
  47. describe('#escapeKeyPressed', function () {
  48. var returnedValue,
  49. event = {
  50. preventDefault: Em.K,
  51. stopPropagation: Em.K
  52. },
  53. cases = [
  54. {
  55. buttons: [],
  56. preventDefaultCallCount: 0,
  57. stopPropagationCallCount: 0,
  58. clickCallCount: 0,
  59. returnedValue: undefined,
  60. title: 'no close button'
  61. },
  62. {
  63. buttons: [{}],
  64. preventDefaultCallCount: 1,
  65. stopPropagationCallCount: 1,
  66. clickCallCount: 1,
  67. returnedValue: false,
  68. title: 'close button available'
  69. }
  70. ];
  71. cases.forEach(function (item) {
  72. describe(item.title, function () {
  73. beforeEach(function () {
  74. item.buttons.click = Em.K;
  75. sinon.stub(popup, '$').returns({
  76. find: function () {
  77. return {
  78. last: function () {
  79. return item.buttons;
  80. }
  81. }
  82. }
  83. });
  84. sinon.spy(item.buttons, 'click');
  85. sinon.spy(event, 'preventDefault');
  86. sinon.spy(event, 'stopPropagation');
  87. returnedValue = popup.escapeKeyPressed(event);
  88. });
  89. afterEach(function () {
  90. popup.$.restore();
  91. item.buttons.click.restore();
  92. event.preventDefault.restore();
  93. event.stopPropagation.restore();
  94. });
  95. it('prevent default behaviour', function () {
  96. expect(event.preventDefault.callCount).to.equal(item.preventDefaultCallCount);
  97. });
  98. it('stop event propagation', function () {
  99. expect(event.stopPropagation.callCount).to.equal(item.stopPropagationCallCount);
  100. });
  101. it('close button click', function () {
  102. expect(item.buttons.click.callCount).to.equal(item.clickCallCount);
  103. });
  104. it('returned value', function () {
  105. expect(returnedValue).to.equal(item.returnedValue);
  106. });
  107. });
  108. });
  109. });
  110. describe('#enterKeyPressed', function () {
  111. var returnedValue,
  112. event = {
  113. preventDefault: Em.K,
  114. stopPropagation: Em.K
  115. },
  116. cases = [
  117. {
  118. buttons: [],
  119. isTextArea: true,
  120. preventDefaultCallCount: 0,
  121. stopPropagationCallCount: 0,
  122. clickCallCount: 0,
  123. returnedValue: undefined,
  124. title: 'focus on textarea, no primary button'
  125. },
  126. {
  127. buttons: [],
  128. isTextArea: false,
  129. preventDefaultCallCount: 0,
  130. stopPropagationCallCount: 0,
  131. clickCallCount: 0,
  132. returnedValue: undefined,
  133. title: 'no focus on textarea, no primary button'
  134. },
  135. {
  136. buttons: [{}],
  137. isTextArea: true,
  138. disabled: 'disabled',
  139. preventDefaultCallCount: 0,
  140. stopPropagationCallCount: 0,
  141. clickCallCount: 0,
  142. returnedValue: undefined,
  143. title: 'focus on textarea, primary button disabled'
  144. },
  145. {
  146. buttons: [{}],
  147. isTextArea: false,
  148. disabled: 'disabled',
  149. preventDefaultCallCount: 0,
  150. stopPropagationCallCount: 0,
  151. clickCallCount: 0,
  152. returnedValue: undefined,
  153. title: 'no focus on textarea, primary button disabled'
  154. },
  155. {
  156. buttons: [{}],
  157. isTextArea: true,
  158. disabled: '',
  159. preventDefaultCallCount: 0,
  160. stopPropagationCallCount: 0,
  161. clickCallCount: 0,
  162. returnedValue: undefined,
  163. title: 'focus on textarea, primary button enabled'
  164. },
  165. {
  166. buttons: [{}],
  167. isTextArea: false,
  168. disabled: '',
  169. preventDefaultCallCount: 1,
  170. stopPropagationCallCount: 1,
  171. clickCallCount: 1,
  172. returnedValue: false,
  173. title: 'no focus on textarea, primary button enabled'
  174. }
  175. ];
  176. cases.forEach(function (item) {
  177. describe(item.title, function () {
  178. beforeEach(function () {
  179. item.buttons.click = Em.K;
  180. item.buttons.attr = function () {
  181. return item.disabled;
  182. };
  183. sinon.stub(popup, '$').returns({
  184. find: function () {
  185. return {
  186. last: function () {
  187. return item.buttons;
  188. }
  189. }
  190. }
  191. });
  192. sinon.stub(window, '$').withArgs('*:focus').returns({
  193. is: function () {
  194. return item.isTextArea
  195. }
  196. });
  197. sinon.spy(item.buttons, 'click');
  198. sinon.spy(event, 'preventDefault');
  199. sinon.spy(event, 'stopPropagation');
  200. returnedValue = popup.enterKeyPressed(event);
  201. });
  202. afterEach(function () {
  203. popup.$.restore();
  204. window.$.restore();
  205. item.buttons.click.restore();
  206. event.preventDefault.restore();
  207. event.stopPropagation.restore();
  208. });
  209. it('prevent default behaviour', function () {
  210. expect(event.preventDefault.callCount).to.equal(item.preventDefaultCallCount);
  211. });
  212. it('stop event propagation', function () {
  213. expect(event.stopPropagation.callCount).to.equal(item.stopPropagationCallCount);
  214. });
  215. it('primary button click', function () {
  216. expect(item.buttons.click.callCount).to.equal(item.clickCallCount);
  217. });
  218. it('returned value', function () {
  219. expect(returnedValue).to.equal(item.returnedValue);
  220. });
  221. });
  222. });
  223. });
  224. });