modalSpec.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. describe('Modal', function(){
  2. var tpl = '<div width="width:400px; height: 400px; background:black;display:block;"> <h1> Test Content </h1> </div>';
  3. beforeEach(module('morph'));
  4. beforeEach(module('morph.directives'));
  5. beforeEach(inject(function ($rootScope){
  6. $rootScope.settings = {
  7. modal: {
  8. template: tpl
  9. }
  10. };
  11. $rootScope.$digest();
  12. }));
  13. // basic modal functionality tests
  14. it('should compile a template', function(){
  15. inject(function ($compile, $document, $rootScope, $rootElement) {
  16. var morphable = $compile('<div><button ng-morph-modal="settings"> Test </button></div>')($rootScope);
  17. $rootElement.append(morphable);
  18. angular.element($document[0].body).append($rootElement);
  19. var content = angular.element($document[0].body).find('div')[2];
  20. expect(content.children.length).to.be(1);
  21. });
  22. });
  23. it('should create a fade element', function(){
  24. inject(function ($compile, $document, $rootScope, $rootElement) {
  25. var morphable = $compile('<div><button ng-morph-modal="settings"> Test </button></div>')($rootScope);
  26. $rootElement.append(morphable);
  27. angular.element($document[0].body).append($rootElement);
  28. expect($rootElement.children()[0].children.length).to.be(3);
  29. });
  30. });
  31. it('should not create a fade element', function(){
  32. inject(function ($compile, $document, $rootScope, $rootElement) {
  33. $rootScope.settings.modal.fade = false;
  34. var morphable = $compile('<div><button ng-morph-modal="settings"> Test </button></div>')($rootScope);
  35. $rootElement.append(morphable);
  36. angular.element($document[0].body).append($rootElement);
  37. expect($rootElement.children()[0].children.length).to.be(2);
  38. });
  39. });
  40. it('should open modal when directive element is clicked', function() {
  41. inject(function ($compile, $document, $rootScope, $rootElement) {
  42. var morphable = $compile('<div><button ng-morph-modal="settings"> Test </button></div>')($rootScope);
  43. $rootElement.append(morphable);
  44. angular.element($document[0].body).append($rootElement);
  45. runs(function() {
  46. morphable.find("button")[0].click();
  47. });
  48. waitsFor(function() {
  49. return getComputedStyle(morphable.find("div")[0], null).opacity === "1";
  50. }, "wrapper should be visible to user", 35);
  51. });
  52. });
  53. it('should close modal when closeEl is clicked', function() {
  54. inject(function ($compile, $document, $rootScope, $rootElement) {
  55. var tplWithCloseEl = '<div style="width:400px; height: 400px; background:black;display:block;"> <span class="close-x">x</span> <h1> Test Content </h1> </div>';
  56. $rootScope.settings = {
  57. closeEl: ".close-x",
  58. trigger: 'click',
  59. modal: {
  60. template: tplWithCloseEl
  61. }
  62. };
  63. var morphable = $compile('<div><button ng-morph-modal="settings"> Test </button></div>')($rootScope);
  64. $rootElement.append(morphable);
  65. angular.element($document[0].body).append($rootElement);
  66. runs(function() {
  67. morphable.find("button")[0].click();
  68. });
  69. waitsFor(function() {
  70. return getComputedStyle(morphable.find("div")[0], null).opacity === "1";
  71. }, "wrapper should be visible to user", 35);
  72. runs(function() {
  73. morphable.children(".close-x")[0].click();
  74. });
  75. waitsFor(function() {
  76. return getComputedStyle(morphable.find("div")[0], null).opacity === '0' ;
  77. }, "wrapper should be hidden to user", 850);
  78. });
  79. });
  80. // nested morphable tests
  81. it('should compile a template containing a nested modal morphable', function () {
  82. inject(function ($compile, $document, $rootScope, $rootElement) {
  83. var tplContainingMorphable = '<div style="width:400px; height: 400px; background:black;display:block;"> <button ng-morph-modal="nestedModalSettings"> Nested Morphable </button></div>';
  84. var nestedModaltpl = '<div id="nested-modal" width="width:400px; height: 400px; background:black;display:block;"> <span class="close-x">x</span></div>';
  85. $rootScope.modalSettings = {
  86. closeEl: ".close-x",
  87. trigger: 'click',
  88. modal: {
  89. template: tplContainingMorphable
  90. }
  91. };
  92. $rootScope.nestedModalSettings = {
  93. closeEl: ".close-x",
  94. trigger: 'click',
  95. modal: {
  96. template: nestedModaltpl
  97. }
  98. };
  99. var morphable = $compile('<div><button ng-morph-modal="modalSettings"> Test </button></div>')($rootScope);
  100. $rootElement.append(morphable);
  101. angular.element($document[0].body).append($rootElement);
  102. runs(function() {
  103. morphable.find("button")[0].click();
  104. });
  105. expect(morphable[0].querySelector("#nested-modal").childNodes.length).to.be(2);
  106. });
  107. });
  108. it('should open a modal when a nested directive element is clicked', function () {
  109. inject(function ($compile, $document, $rootScope, $rootElement) {
  110. var tplContainingMorphable = '<div id="tpl-containing-morphable" style="width:400px; height: 400px; background:black;display:block;"> <button ng-morph-modal="nestedModalSettings"> Nested Morphable </button></div>';
  111. var nestedModaltpl = '<div id="nested-modal" width="width:400px; height: 400px; background:black;display:block;"> <span class="close-x">x</span></div>';
  112. $rootScope.modalSettings = {
  113. closeEl: ".close-x",
  114. trigger: 'click',
  115. modal: {
  116. template: tplContainingMorphable
  117. }
  118. };
  119. $rootScope.nestedModalSettings = {
  120. closeEl: ".close-x",
  121. trigger: 'click',
  122. modal: {
  123. template: nestedModaltpl
  124. }
  125. };
  126. var morphable = $compile('<div><button ng-morph-modal="modalSettings"> Test </button></div>')($rootScope);
  127. $rootElement.append(morphable);
  128. angular.element($document[0].body).append($rootElement);
  129. // open first modal
  130. runs(function() {
  131. morphable.find("button")[0].click();
  132. });
  133. waitsFor(function() {
  134. return getComputedStyle(morphable.find("div")[0], null).opacity === "1";
  135. }, "wrapper should be visible to user", 35);
  136. // open nested modal
  137. runs(function() {
  138. var tpl = angular.element(morphable[0].querySelector("#tpl-containing-morphable"));
  139. tpl.find("button")[0].click();
  140. });
  141. waitsFor(function() {
  142. return getComputedStyle(morphable[0].querySelector("#nested-modal"), null).visibility === "visible";
  143. }, "nested modal should be visible to user", 35);
  144. });
  145. });
  146. });