]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Fix backdrop `rootElement` not initialized in Modal (#33853)
authorNagarjun Bodduna <boddunan@gmail.com>
Mon, 10 May 2021 18:17:53 +0000 (23:47 +0530)
committerGitHub <noreply@github.com>
Mon, 10 May 2021 18:17:53 +0000 (21:17 +0300)
* Initialize default value of rootElement before using

* Remove redundant test | put rootElement tests together

Co-authored-by: GeoSot <geo.sotis@gmail.com>
js/src/util/backdrop.js
js/tests/unit/util/backdrop.spec.js

index 775c09ec03d5ced3c69c9046b399cbb480afc6c1..ad9fcb92fac22593cc9cf0059c084c40d78cb83c 100644 (file)
@@ -89,6 +89,8 @@ class Backdrop {
       ...Default,
       ...(typeof config === 'object' ? config : {})
     }
+
+    config.rootElement = config.rootElement || document.body
     typeCheckConfig(NAME, config, DefaultType)
     return config
   }
index 0a20a13bc527b234324f7c101b4c9617243a2127..ae342b09290a6c785d171a9a7d5ebd2d77a256cb 100644 (file)
@@ -73,35 +73,6 @@ describe('Backdrop', () => {
         done()
       })
     })
-
-    it('Should be appended on "document.body" by default', done => {
-      const instance = new Backdrop({
-        isVisible: true
-      })
-      const getElement = () => document.querySelector(CLASS_BACKDROP)
-      instance.show(() => {
-        expect(getElement().parentElement).toEqual(document.body)
-        done()
-      })
-    })
-
-    it('Should appended on any element given by the proper config', done => {
-      fixtureEl.innerHTML = [
-        '<div id="wrapper">',
-        '</div>'
-      ].join('')
-
-      const wrapper = fixtureEl.querySelector('#wrapper')
-      const instance = new Backdrop({
-        isVisible: true,
-        rootElement: wrapper
-      })
-      const getElement = () => document.querySelector(CLASS_BACKDROP)
-      instance.show(() => {
-        expect(getElement().parentElement).toEqual(wrapper)
-        done()
-      })
-    })
   })
 
   describe('hide', () => {
@@ -238,4 +209,47 @@ describe('Backdrop', () => {
       })
     })
   })
+
+  describe('rootElement initialization', () => {
+    it('Should be appended on "document.body" by default', done => {
+      const instance = new Backdrop({
+        isVisible: true
+      })
+      const getElement = () => document.querySelector(CLASS_BACKDROP)
+      instance.show(() => {
+        expect(getElement().parentElement).toEqual(document.body)
+        done()
+      })
+    })
+
+    it('Should default parent element to "document.body" when config value is null', done => {
+      const instance = new Backdrop({
+        isVisible: true,
+        rootElement: null
+      })
+      const getElement = () => document.querySelector(CLASS_BACKDROP)
+      instance.show(() => {
+        expect(getElement().parentElement).toEqual(document.body)
+        done()
+      })
+    })
+
+    it('Should appended on any element given by the proper config', done => {
+      fixtureEl.innerHTML = [
+        '<div id="wrapper">',
+        '</div>'
+      ].join('')
+
+      const wrapper = fixtureEl.querySelector('#wrapper')
+      const instance = new Backdrop({
+        isVisible: true,
+        rootElement: wrapper
+      })
+      const getElement = () => document.querySelector(CLASS_BACKDROP)
+      instance.show(() => {
+        expect(getElement().parentElement).toEqual(wrapper)
+        done()
+      })
+    })
+  })
 })