123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- $(function () {
- 'use strict'
- if (typeof bootstrap !== 'undefined') {
- window.Toast = bootstrap.Toast
- }
- QUnit.module('toast plugin')
- QUnit.test('should be defined on jquery object', function (assert) {
- assert.expect(1)
- assert.ok($(document.body).toast, 'toast method is defined')
- })
- QUnit.module('toast', {
- beforeEach: function () {
- // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
- $.fn.bootstrapToast = $.fn.toast.noConflict()
- },
- afterEach: function () {
- $.fn.toast = $.fn.bootstrapToast
- delete $.fn.bootstrapToast
- $('#qunit-fixture').html('')
- }
- })
- QUnit.test('should provide no conflict', function (assert) {
- assert.expect(1)
- assert.strictEqual(typeof $.fn.toast, 'undefined', 'toast was set back to undefined (org value)')
- })
- QUnit.test('should return the current version', function (assert) {
- assert.expect(1)
- assert.strictEqual(typeof Toast.VERSION, 'string')
- })
- QUnit.test('should throw explicit error on undefined method', function (assert) {
- assert.expect(1)
- var $el = $('<div/>')
- $el.bootstrapToast()
- try {
- $el.bootstrapToast('noMethod')
- } catch (err) {
- assert.strictEqual(err.message, 'No method named "noMethod"')
- }
- })
- QUnit.test('should return jquery collection containing the element', function (assert) {
- assert.expect(2)
- var $el = $('<div/>')
- var $toast = $el.bootstrapToast()
- assert.ok($toast instanceof $, 'returns jquery collection')
- assert.strictEqual($toast[0], $el[0], 'collection contains element')
- })
- QUnit.test('should auto hide', function (assert) {
- assert.expect(1)
- var done = assert.async()
- var toastHtml =
- '<div class="toast" data-delay="1">' +
- '<div class="toast-body">' +
- 'a simple toast' +
- '</div>' +
- '</div>'
- var $toast = $(toastHtml)
- .bootstrapToast()
- .appendTo($('#qunit-fixture'))
- $toast.on('hidden.bs.toast', function () {
- assert.strictEqual($toast.hasClass('show'), false)
- done()
- })
- .bootstrapToast('show')
- })
- QUnit.test('should not add fade class', function (assert) {
- assert.expect(1)
- var done = assert.async()
- var toastHtml =
- '<div class="toast" data-delay="1" data-animation="false">' +
- '<div class="toast-body">' +
- 'a simple toast' +
- '</div>' +
- '</div>'
- var $toast = $(toastHtml)
- .bootstrapToast()
- .appendTo($('#qunit-fixture'))
- $toast.on('shown.bs.toast', function () {
- assert.strictEqual($toast.hasClass('fade'), false)
- done()
- })
- .bootstrapToast('show')
- })
- QUnit.test('should allow to hide toast manually', function (assert) {
- assert.expect(1)
- var done = assert.async()
- var toastHtml =
- '<div class="toast" data-delay="1" data-autohide="false">' +
- '<div class="toast-body">' +
- 'a simple toast' +
- '</div>' +
- '</div>'
- var $toast = $(toastHtml)
- .bootstrapToast()
- .appendTo($('#qunit-fixture'))
- $toast
- .on('shown.bs.toast', function () {
- $toast.bootstrapToast('hide')
- })
- .on('hidden.bs.toast', function () {
- assert.strictEqual($toast.hasClass('show'), false)
- done()
- })
- .bootstrapToast('show')
- })
- QUnit.test('should do nothing when we call hide on a non shown toast', function (assert) {
- assert.expect(1)
- var $toast = $('<div />')
- .bootstrapToast()
- .appendTo($('#qunit-fixture'))
- var spy = sinon.spy($toast[0].classList, 'contains')
- $toast.bootstrapToast('hide')
- assert.strictEqual(spy.called, true)
- })
- QUnit.test('should allow to destroy toast', function (assert) {
- assert.expect(2)
- var $toast = $('<div />')
- .bootstrapToast()
- .appendTo($('#qunit-fixture'))
- assert.ok(typeof $toast.data('bs.toast') !== 'undefined')
- $toast.bootstrapToast('dispose')
- assert.ok(typeof $toast.data('bs.toast') === 'undefined')
- })
- QUnit.test('should allow to destroy toast and hide it before that', function (assert) {
- assert.expect(4)
- var done = assert.async()
- var toastHtml =
- '<div class="toast" data-delay="0" data-autohide="false">' +
- '<div class="toast-body">' +
- 'a simple toast' +
- '</div>' +
- '</div>'
- var $toast = $(toastHtml)
- .bootstrapToast()
- .appendTo($('#qunit-fixture'))
- $toast.one('shown.bs.toast', function () {
- setTimeout(function () {
- assert.ok($toast.hasClass('show'))
- assert.ok(typeof $toast.data('bs.toast') !== 'undefined')
- $toast.bootstrapToast('dispose')
- assert.ok(typeof $toast.data('bs.toast') === 'undefined')
- assert.ok($toast.hasClass('show') === false)
- done()
- }, 1)
- })
- .bootstrapToast('show')
- })
- QUnit.test('should allow to config in js', function (assert) {
- assert.expect(1)
- var done = assert.async()
- var toastHtml =
- '<div class="toast">' +
- '<div class="toast-body">' +
- 'a simple toast' +
- '</div>' +
- '</div>'
- var $toast = $(toastHtml)
- .bootstrapToast({
- delay: 1
- })
- .appendTo($('#qunit-fixture'))
- $toast.on('shown.bs.toast', function () {
- assert.strictEqual($toast.hasClass('show'), true)
- done()
- })
- .bootstrapToast('show')
- })
- QUnit.test('should close toast when close element with data-dismiss attribute is set', function (assert) {
- assert.expect(2)
- var done = assert.async()
- var toastHtml =
- '<div class="toast" data-delay="1" data-autohide="false" data-animation="false">' +
- '<button type="button" class="ml-2 mb-1 close" data-dismiss="toast">' +
- 'close' +
- '</button>' +
- '</div>'
- var $toast = $(toastHtml)
- .bootstrapToast()
- .appendTo($('#qunit-fixture'))
- $toast
- .on('shown.bs.toast', function () {
- assert.strictEqual($toast.hasClass('show'), true)
- var button = $toast.find('.close')
- button.trigger('click')
- })
- .on('hidden.bs.toast', function () {
- assert.strictEqual($toast.hasClass('show'), false)
- done()
- })
- .bootstrapToast('show')
- })
- QUnit.test('should expose default setting to allow to override them', function (assert) {
- assert.expect(1)
- var defaultDelay = 1000
- Toast.Default.delay = defaultDelay
- var toastHtml =
- '<div class="toast" data-autohide="false" data-animation="false">' +
- '<button type="button" class="ml-2 mb-1 close" data-dismiss="toast">' +
- 'close' +
- '</button>' +
- '</div>'
- var $toast = $(toastHtml)
- .bootstrapToast()
- var toast = $toast.data('bs.toast')
- assert.strictEqual(toast._config.delay, defaultDelay)
- })
- })
|