mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-13 17:14:04 +02:00
Create toast JS plugin, add unit tests.
This commit is contained in:
@@ -120,6 +120,7 @@
|
||||
<script src="../dist/tab.js"></script>
|
||||
<script src="../dist/tooltip.js"></script>
|
||||
<script src="../dist/popover.js"></script>
|
||||
<script src="../dist/toast.js"></script>
|
||||
|
||||
<!-- Unit Tests -->
|
||||
<script src="unit/alert.js"></script>
|
||||
@@ -133,6 +134,7 @@
|
||||
<script src="unit/tooltip.js"></script>
|
||||
<script src="unit/popover.js"></script>
|
||||
<script src="unit/util.js"></script>
|
||||
<script src="unit/toast.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="qunit-container">
|
||||
|
@@ -11,7 +11,8 @@
|
||||
"Alert": false,
|
||||
"Button": false,
|
||||
"Carousel": false,
|
||||
"Simulator": false
|
||||
"Simulator": false,
|
||||
"Toast": false
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 5,
|
||||
|
235
js/tests/unit/toast.js
Normal file
235
js/tests/unit/toast.js
Normal file
@@ -0,0 +1,235 @@
|
||||
$(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 pass delay object in html', function (assert) {
|
||||
assert.expect(1)
|
||||
var done = assert.async()
|
||||
|
||||
var toastHtml =
|
||||
'<div class="toast" data-delay=\'{"show": 0, "hide": 1}\'>' +
|
||||
'<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('show'), true)
|
||||
done()
|
||||
})
|
||||
.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: {
|
||||
show: 0,
|
||||
hide: 1
|
||||
}
|
||||
})
|
||||
.appendTo($('#qunit-fixture'))
|
||||
|
||||
$toast.on('shown.bs.toast', function () {
|
||||
assert.strictEqual($toast.hasClass('show'), true)
|
||||
done()
|
||||
})
|
||||
.bootstrapToast('show')
|
||||
})
|
||||
})
|
69
js/tests/visual/toast.html
Normal file
69
js/tests/visual/toast.html
Normal file
@@ -0,0 +1,69 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<link rel="stylesheet" href="../../../dist/css/bootstrap.min.css">
|
||||
<title>Toast</title>
|
||||
<style>
|
||||
.notifications {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
right: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Toast <small>Bootstrap Visual Test</small></h1>
|
||||
|
||||
<div class="row mt-3">
|
||||
<div class="col-md-12">
|
||||
<button id="btnShowToast" class="btn btn-primary">Show toast</button>
|
||||
<button id="btnHideToast" class="btn btn-primary">Hide toast</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="notifications">
|
||||
<div class="toast" data-delay='{"show": 0, "hide": 2000}'>
|
||||
<div class="toast-header">
|
||||
<img class="rounded mr-2" data-src="holder.js/20x20?size=1&text=.&bg=#007aff" alt="">
|
||||
<strong class="mr-auto">Bootstrap</strong>
|
||||
<small>11 mins ago</small>
|
||||
</div>
|
||||
<div class="toast-body">
|
||||
Hello, world! This is a toast message with <strong>autohide</strong> in 2 seconds
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="toast" data-autohide="false">
|
||||
<div class="toast-header">
|
||||
<img class="rounded mr-2" data-src="holder.js/20x20?size=1&text=.&bg=#007aff" alt="">
|
||||
<strong class="mr-auto">Bootstrap</strong>
|
||||
<small class="text-muted">2 seconds ago</small>
|
||||
</div>
|
||||
<div class="toast-body">
|
||||
Heads up, toasts will stack automatically
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="../../../site/docs/4.1/assets/js/vendor/jquery-slim.min.js"></script>
|
||||
<script src="../../dist/util.js"></script>
|
||||
<script src="../../dist/toast.js"></script>
|
||||
<script>
|
||||
$(function () {
|
||||
$('.toast').toast()
|
||||
|
||||
$('#btnShowToast').on('click', function () {
|
||||
$('.toast').toast('show')
|
||||
})
|
||||
|
||||
$('#btnHideToast').on('click', function () {
|
||||
$('.toast').toast('hide')
|
||||
})
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user