application_test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/application');
  20. var view,
  21. modals = [],
  22. removed = false,
  23. events = [
  24. {
  25. event: 'keydown',
  26. which: 13,
  27. key: 'Enter',
  28. html: '<div id="modal"><div class="modal-footer"><span class="btn-success"></span></div></div>',
  29. particle: '',
  30. length: 0
  31. },
  32. {
  33. event: 'keydown',
  34. keyCode: 13,
  35. key: 'Enter',
  36. html: '<div id="modal"><div class="modal-footer"><span class="btn-success"></span></div></div>',
  37. particle: '',
  38. length: 0
  39. },
  40. {
  41. event: 'keyup',
  42. which: 27,
  43. key: 'Esc',
  44. html: '<div id="modal"><div class="modal-header"><span class="close"></span></div></div>',
  45. particle: '',
  46. length: 0
  47. },
  48. {
  49. event: 'keyup',
  50. keyCode: 27,
  51. key: 'Esc',
  52. html: '<div id="modal"><div class="modal-header"><span class="close"></span></div></div>',
  53. particle: '',
  54. length: 0
  55. },
  56. {
  57. event: 'keydown',
  58. which: 13,
  59. key: 'Enter',
  60. html: '<div id="modal"><div class="modal-footer"><span></span></div></div>',
  61. particle: 'not ',
  62. length: 1
  63. },
  64. {
  65. event: 'keydown',
  66. keyCode: 13,
  67. key: 'Enter',
  68. html: '<div id="modal"><div class="modal-footer"><span></span></div></div>',
  69. particle: 'not ',
  70. length: 1
  71. },
  72. {
  73. event: 'keyup',
  74. which: 27,
  75. key: 'Esc',
  76. html: '<div id="modal"><div class="modal-header"><span></span></div></div>',
  77. particle: 'not ',
  78. length: 1
  79. },
  80. {
  81. event: 'keyup',
  82. keyCode: 27,
  83. key: 'Esc',
  84. html: '<div id="modal"><div class="modal-header"><span></span></div></div>',
  85. particle: 'not ',
  86. length: 1
  87. },
  88. {
  89. event: 'keydown',
  90. which: 13,
  91. key: 'Enter',
  92. html: '<div id="modal"><div class="modal-footer"><span class="btn-success" disabled="disabled"></span></div></div>',
  93. particle: 'not ',
  94. length: 1
  95. },
  96. {
  97. event: 'keydown',
  98. keyCode: 13,
  99. key: 'Enter',
  100. html: '<div id="modal"><div class="modal-footer"><span class="btn-success" disabled="disabled"></span></div></div>',
  101. particle: 'not ',
  102. length: 1
  103. },
  104. {
  105. event: 'keydown',
  106. key: 'Enter',
  107. html: '<div id="modal"><div class="modal-footer"><span class="btn-success"></span></div></div>',
  108. particle: 'not ',
  109. length: 1
  110. },
  111. {
  112. event: 'keyup',
  113. key: 'Esc',
  114. html: '<div id="modal"><div class="modal-footer"><span class="close"></span></div></div>',
  115. particle: 'not ',
  116. length: 1
  117. }
  118. ];
  119. describe('App.ApplicationView', function () {
  120. before(function () {
  121. if($('#modal').length) {
  122. removed = true;
  123. }
  124. while($('#modal').length) {
  125. modals.push({
  126. modal: $('#modal'),
  127. parent: $('modal').parent()
  128. });
  129. $('#modal').remove();
  130. }
  131. });
  132. beforeEach(function () {
  133. view = App.ApplicationView.create({
  134. template: null
  135. });
  136. });
  137. afterEach(function () {
  138. $('#modal').remove();
  139. });
  140. after(function () {
  141. if (removed) {
  142. modals.forEach(function (item) {
  143. item.parent.append(item.modal);
  144. });
  145. }
  146. });
  147. describe('#didInsertElement', function () {
  148. events.forEach(function (item) {
  149. it('should ' + item.particle + 'close modal window on ' + item.key + ' press', function () {
  150. $('body').append(item.html);
  151. $('span').click(function () {
  152. $('#modal').remove();
  153. });
  154. view.didInsertElement();
  155. var e = $.Event(item.event);
  156. e.which = item.which;
  157. e.keyCode = item.keyCode;
  158. $(document).trigger(e);
  159. expect($('#modal')).to.have.length(item.length);
  160. });
  161. });
  162. });
  163. });