backdrop.spec.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import Backdrop from '../../../src/util/backdrop'
  2. import { getTransitionDurationFromElement } from '../../../src/util/index'
  3. import { clearFixture, getFixture } from '../../helpers/fixture'
  4. const CLASS_BACKDROP = '.modal-backdrop'
  5. const CLASS_NAME_FADE = 'fade'
  6. const CLASS_NAME_SHOW = 'show'
  7. describe('Backdrop', () => {
  8. let fixtureEl
  9. beforeAll(() => {
  10. fixtureEl = getFixture()
  11. })
  12. afterEach(() => {
  13. clearFixture()
  14. const list = document.querySelectorAll(CLASS_BACKDROP)
  15. list.forEach(el => {
  16. document.body.removeChild(el)
  17. })
  18. })
  19. describe('show', () => {
  20. it('if it is "shown", should append the backdrop html once, on show, and contain "show" class', done => {
  21. const instance = new Backdrop({
  22. isVisible: true,
  23. isAnimated: false
  24. })
  25. const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
  26. expect(getElements().length).toEqual(0)
  27. instance.show()
  28. instance.show(() => {
  29. expect(getElements().length).toEqual(1)
  30. getElements().forEach(el => {
  31. expect(el.classList.contains(CLASS_NAME_SHOW)).toEqual(true)
  32. })
  33. done()
  34. })
  35. })
  36. it('if it is not "shown", should not append the backdrop html', done => {
  37. const instance = new Backdrop({
  38. isVisible: false,
  39. isAnimated: true
  40. })
  41. const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
  42. expect(getElements().length).toEqual(0)
  43. instance.show(() => {
  44. expect(getElements().length).toEqual(0)
  45. done()
  46. })
  47. })
  48. it('if it is "shown" and "animated", should append the backdrop html once, and contain "fade" class', done => {
  49. const instance = new Backdrop({
  50. isVisible: true,
  51. isAnimated: true
  52. })
  53. const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
  54. expect(getElements().length).toEqual(0)
  55. instance.show(() => {
  56. expect(getElements().length).toEqual(1)
  57. getElements().forEach(el => {
  58. expect(el.classList.contains(CLASS_NAME_FADE)).toEqual(true)
  59. })
  60. done()
  61. })
  62. })
  63. })
  64. describe('hide', () => {
  65. it('should remove the backdrop html', done => {
  66. const instance = new Backdrop({
  67. isVisible: true,
  68. isAnimated: true
  69. })
  70. const getElements = () => document.body.querySelectorAll(CLASS_BACKDROP)
  71. expect(getElements().length).toEqual(0)
  72. instance.show(() => {
  73. expect(getElements().length).toEqual(1)
  74. instance.hide(() => {
  75. expect(getElements().length).toEqual(0)
  76. done()
  77. })
  78. })
  79. })
  80. it('should remove "show" class', done => {
  81. const instance = new Backdrop({
  82. isVisible: true,
  83. isAnimated: true
  84. })
  85. const elem = instance._getElement()
  86. instance.show()
  87. instance.hide(() => {
  88. expect(elem.classList.contains(CLASS_NAME_SHOW)).toEqual(false)
  89. done()
  90. })
  91. })
  92. it('if it is not "shown", should not try to remove Node on remove method', done => {
  93. const instance = new Backdrop({
  94. isVisible: false,
  95. isAnimated: true
  96. })
  97. const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
  98. const spy = spyOn(instance, 'dispose').and.callThrough()
  99. expect(getElements().length).toEqual(0)
  100. expect(instance._isAppended).toEqual(false)
  101. instance.show(() => {
  102. instance.hide(() => {
  103. expect(getElements().length).toEqual(0)
  104. expect(spy).not.toHaveBeenCalled()
  105. expect(instance._isAppended).toEqual(false)
  106. done()
  107. })
  108. })
  109. })
  110. it('should not error if the backdrop no longer has a parent', done => {
  111. fixtureEl.innerHTML = '<div id="wrapper"></div>'
  112. const wrapper = fixtureEl.querySelector('#wrapper')
  113. const instance = new Backdrop({
  114. isVisible: true,
  115. isAnimated: true,
  116. rootElement: wrapper
  117. })
  118. const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
  119. instance.show(() => {
  120. wrapper.parentNode.removeChild(wrapper)
  121. instance.hide(() => {
  122. expect(getElements().length).toEqual(0)
  123. done()
  124. })
  125. })
  126. })
  127. })
  128. describe('click callback', () => {
  129. it('it should execute callback on click', done => {
  130. const spy = jasmine.createSpy('spy')
  131. const instance = new Backdrop({
  132. isVisible: true,
  133. isAnimated: false,
  134. clickCallback: () => spy()
  135. })
  136. const endTest = () => {
  137. setTimeout(() => {
  138. expect(spy).toHaveBeenCalled()
  139. done()
  140. }, 10)
  141. }
  142. instance.show(() => {
  143. const clickEvent = document.createEvent('MouseEvents')
  144. clickEvent.initEvent('mousedown', true, true)
  145. document.querySelector(CLASS_BACKDROP).dispatchEvent(clickEvent)
  146. endTest()
  147. })
  148. })
  149. })
  150. describe('animation callbacks', () => {
  151. it('if it is animated, should show and hide backdrop after counting transition duration', done => {
  152. const instance = new Backdrop({
  153. isVisible: true,
  154. isAnimated: true
  155. })
  156. const spy2 = jasmine.createSpy('spy2')
  157. const execDone = () => {
  158. setTimeout(() => {
  159. expect(spy2).toHaveBeenCalledTimes(2)
  160. done()
  161. }, 10)
  162. }
  163. instance.show(spy2)
  164. instance.hide(() => {
  165. spy2()
  166. execDone()
  167. })
  168. expect(spy2).not.toHaveBeenCalled()
  169. })
  170. it('if it is not animated, should show and hide backdrop without delay', done => {
  171. const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
  172. const instance = new Backdrop({
  173. isVisible: true,
  174. isAnimated: false
  175. })
  176. const spy2 = jasmine.createSpy('spy2')
  177. instance.show(spy2)
  178. instance.hide(spy2)
  179. setTimeout(() => {
  180. expect(spy2).toHaveBeenCalled()
  181. expect(spy).not.toHaveBeenCalled()
  182. done()
  183. }, 10)
  184. })
  185. it('if it is not "shown", should not call delay callbacks', done => {
  186. const instance = new Backdrop({
  187. isVisible: false,
  188. isAnimated: true
  189. })
  190. const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
  191. instance.show()
  192. instance.hide(() => {
  193. expect(spy).not.toHaveBeenCalled()
  194. done()
  195. })
  196. })
  197. })
  198. describe('rootElement initialization', () => {
  199. it('Should be appended on "document.body" by default', done => {
  200. const instance = new Backdrop({
  201. isVisible: true
  202. })
  203. const getElement = () => document.querySelector(CLASS_BACKDROP)
  204. instance.show(() => {
  205. expect(getElement().parentElement).toEqual(document.body)
  206. done()
  207. })
  208. })
  209. it('Should find the rootElement if passed as a string', done => {
  210. const instance = new Backdrop({
  211. isVisible: true,
  212. rootElement: 'body'
  213. })
  214. const getElement = () => document.querySelector(CLASS_BACKDROP)
  215. instance.show(() => {
  216. expect(getElement().parentElement).toEqual(document.body)
  217. done()
  218. })
  219. })
  220. it('Should appended on any element given by the proper config', done => {
  221. fixtureEl.innerHTML = [
  222. '<div id="wrapper">',
  223. '</div>'
  224. ].join('')
  225. const wrapper = fixtureEl.querySelector('#wrapper')
  226. const instance = new Backdrop({
  227. isVisible: true,
  228. rootElement: wrapper
  229. })
  230. const getElement = () => document.querySelector(CLASS_BACKDROP)
  231. instance.show(() => {
  232. expect(getElement().parentElement).toEqual(wrapper)
  233. done()
  234. })
  235. })
  236. })
  237. })