]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
test jquery interface for our alert plugin
authorJohann-S <johann.servoire@gmail.com>
Wed, 27 Mar 2019 08:47:57 +0000 (09:47 +0100)
committerJohann-S <johann.servoire@gmail.com>
Tue, 23 Jul 2019 12:23:50 +0000 (14:23 +0200)
js/src/alert/alert.spec.js

index cb7b57b7f3a1b9444dc1c9bb2ce4e8b0fa7290e7..5cf314d2224cd66d90f8792cb94cb9813b01aa27 100644 (file)
@@ -2,7 +2,7 @@ import Alert from './alert'
 import { makeArray, getTransitionDurationFromElement } from '../util/index'
 
 /** Test helpers */
-import { getFixture, clearFixture } from '../../tests/helpers/fixture'
+import { getFixture, clearFixture, jQueryMock } from '../../tests/helpers/fixture'
 
 describe('Alert', () => {
   let fixtureEl
@@ -124,4 +124,50 @@ describe('Alert', () => {
       expect(Alert._getInstance(alertEl)).toBeNull()
     })
   })
+
+  describe('_jQueryInterface', () => {
+    it('should handle config passed and toggle existing alert', () => {
+      fixtureEl.innerHTML = '<div class="alert"></div>'
+
+      const alertEl = fixtureEl.querySelector('.alert')
+      const alert = new Alert(alertEl)
+
+      spyOn(alert, 'close')
+
+      jQueryMock.fn.alert = Alert._jQueryInterface
+      jQueryMock.elements = [alertEl]
+
+      jQueryMock.fn.alert.call(jQueryMock, 'close')
+
+      expect(alert.close).toHaveBeenCalled()
+    })
+
+    it('should create new alert instance and call close', () => {
+      fixtureEl.innerHTML = '<div class="alert"></div>'
+
+      const alertEl = fixtureEl.querySelector('.alert')
+
+      jQueryMock.fn.alert = Alert._jQueryInterface
+      jQueryMock.elements = [alertEl]
+
+      jQueryMock.fn.alert.call(jQueryMock, 'close')
+
+      expect(Alert._getInstance(alertEl)).toBeDefined()
+      expect(fixtureEl.querySelector('.alert')).toBeNull()
+    })
+
+    it('should just create an alert instance without calling close', () => {
+      fixtureEl.innerHTML = '<div class="alert"></div>'
+
+      const alertEl = fixtureEl.querySelector('.alert')
+
+      jQueryMock.fn.alert = Alert._jQueryInterface
+      jQueryMock.elements = [alertEl]
+
+      jQueryMock.fn.alert.call(jQueryMock)
+
+      expect(Alert._getInstance(alertEl)).toBeDefined()
+      expect(fixtureEl.querySelector('.alert')).not.toBeNull()
+    })
+  })
 })