]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Popover.js: Accept empty content through `data-bs-content` (#35514)
authorGeoSot <geo.sotis@gmail.com>
Wed, 15 Dec 2021 08:41:31 +0000 (10:41 +0200)
committerGitHub <noreply@github.com>
Wed, 15 Dec 2021 08:41:31 +0000 (10:41 +0200)
Co-authored-by: XhmikosR <xhmikosr@gmail.com>
js/src/popover.js
js/tests/unit/popover.spec.js

index 375eb8b0ab208c93ddf2aa9cee57988b575ef691..b62b6a212323e536863d7e4cf2d22372da4e894a 100644 (file)
@@ -34,7 +34,7 @@ const Default = {
 
 const DefaultType = {
   ...Tooltip.DefaultType,
-  content: '(string|element|function)'
+  content: '(null|string|element|function)'
 }
 
 const Event = {
index a2906ade710c509f88a7ba013a5e858e823d07af..a04bd21c60e5da3b879b0bebc087603974c2cf5f 100644 (file)
@@ -1,4 +1,5 @@
 import Popover from '../../src/popover'
+import EventHandler from '../../src/dom/event-handler'
 import { clearFixture, getFixture, jQueryMock } from '../helpers/fixture'
 
 describe('Popover', () => {
@@ -96,59 +97,74 @@ describe('Popover', () => {
       popover.show()
     })
 
-    it('should show a popover with just content', done => {
-      fixtureEl.innerHTML = '<a href="#">BS twitter</a>'
+    it('should show a popover with just content without having header', done => {
+      fixtureEl.innerHTML = '<a href="#">Nice link</a>'
 
       const popoverEl = fixtureEl.querySelector('a')
       const popover = new Popover(popoverEl, {
-        content: 'Popover content'
+        content: 'Some beautiful content :)'
       })
 
       popoverEl.addEventListener('shown.bs.popover', () => {
         const popoverDisplayed = document.querySelector('.popover')
 
         expect(popoverDisplayed).not.toBeNull()
-        expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Popover content')
+        expect(popoverDisplayed.querySelector('.popover-header')).toBeNull()
+        expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Some beautiful content :)')
         done()
       })
 
       popover.show()
     })
 
-    it('should show a popover with just content without having header', done => {
+    it('should show a popover with just title without having body', done => {
       fixtureEl.innerHTML = '<a href="#">Nice link</a>'
 
       const popoverEl = fixtureEl.querySelector('a')
       const popover = new Popover(popoverEl, {
-        content: 'Some beautiful content :)'
+        title: 'Title which does not require content'
       })
 
       popoverEl.addEventListener('shown.bs.popover', () => {
         const popoverDisplayed = document.querySelector('.popover')
 
         expect(popoverDisplayed).not.toBeNull()
-        expect(popoverDisplayed.querySelector('.popover-header')).toBeNull()
-        expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Some beautiful content :)')
+        expect(popoverDisplayed.querySelector('.popover-body')).toBeNull()
+        expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Title which does not require content')
         done()
       })
 
       popover.show()
     })
 
-    it('should show a popover with just title without having body', done => {
-      fixtureEl.innerHTML = '<a href="#">Nice link</a>'
+    it('should show a popover with just title without having body using data-attribute to get config', done => {
+      fixtureEl.innerHTML = '<a href="#" data-bs-content="" title="Title which does not require content">Nice link</a>'
 
       const popoverEl = fixtureEl.querySelector('a')
-      const popover = new Popover(popoverEl, {
-        title: 'Title, which does not require content'
-      })
+      const popover = new Popover(popoverEl)
 
       popoverEl.addEventListener('shown.bs.popover', () => {
         const popoverDisplayed = document.querySelector('.popover')
 
         expect(popoverDisplayed).not.toBeNull()
         expect(popoverDisplayed.querySelector('.popover-body')).toBeNull()
-        expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Title, which does not require content')
+        expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Title which does not require content')
+        done()
+      })
+
+      popover.show()
+    })
+
+    it('should NOT show a popover without `title` and `content`', done => {
+      fixtureEl.innerHTML = '<a href="#" data-bs-content="" title="">Nice link</a>'
+
+      const popoverEl = fixtureEl.querySelector('a')
+      const popover = new Popover(popoverEl, { animation: false })
+      spyOn(EventHandler, 'trigger').and.callThrough()
+
+      setTimeout(() => {
+        expect(EventHandler.trigger).not.toHaveBeenCalled()
+        expect(document.querySelector('.popover')).toBeNull()
         done()
       })