Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

be able to force remove visible toasts #495

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ function startTests(singleRun, done) {

////////////////

function karmaCompleted() {
done();
function karmaCompleted(err) {
done(err);
}
}

Expand Down
99 changes: 99 additions & 0 deletions tests/unit/toastr-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,105 @@
//Teardown
resetContainer();
});
module('remove');
asyncTest('remove - show 3 toasts, remove the 2nd', 1, function () {
//Arrange
var $toast = [];
$toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
$toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
$toast[2] = toastr.info(sampleMsg, sampleTitle + '-3');
var $container = toastr.getContainer();
//Act
toastr.remove($toast[1], { force: true });
//Assert
setTimeout(function () {
ok($container && $container.children().length === 2);
//Teardown
resetContainer();
start();
}, 1000);
});
asyncTest('remove - show 3 toasts, remove all 3, 0 left', 1, function () {
//Arrange
var $toast = [];
$toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
$toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
$toast[2] = toastr.info(sampleMsg, sampleTitle + '-3');
var $container = toastr.getContainer();
//Act
toastr.remove();
//Assert
setTimeout(function () {
ok($container && $container.children().length === 0);
//Teardown
resetContainer();
start();
}, delay);
});
asyncTest('remove and show - show 2 toasts, remove both, then show 1 more', 2, function () {
//Arrange
var $toast = [];
$toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
$toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
var $container = toastr.getContainer();
toastr.remove();
//Act
setTimeout(function () {
$toast[2] = toastr.info(sampleMsg, sampleTitle + '-3-Visible');
//Assert
equal($toast[2].find('div.toast-title').html(), sampleTitle + '-3-Visible', 'Finds toast after a remove');
ok($toast[2].is(':visible'), 'Toast after a remove is visible');
//Teardown
resetContainer();
start();
}, delay);
});
asyncTest('remove and show - remove removes toast container', 2, function () {
//Arrange
var $toast = [];
$toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
$toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
var $container = toastr.getContainer();
toastr.remove();
//Act
setTimeout(function () {
//Assert
equal($(selectors.container).length, 0, 'Toast container does not exist');
ok(!$toast[1].is(':visible'), 'Toast after a remove is visible');
//Teardown
resetContainer();
start();
}, delay);
});
asyncTest('remove and show - after remove new toast creates container', 1, function () {
//Arrange
var $toast = [];
$toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
$toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
var $container = toastr.getContainer();
toastr.remove();
//Act
setTimeout(function () {
$toast[2] = toastr.info(sampleMsg, sampleTitle + '-3-Visible');
//Assert
equal($(selectors.container).find('div.toast-title').html(), sampleTitle + '-3-Visible', 'Finds toast after a remove'); //Teardown
resetContainer();
start();
}, delay);
});
test('remove and show - after remove all toasts new toast still appears', 1, function () {
//Arrange
var $toast = [];
//Act
$toast[0] = toastr.info(sampleMsg, sampleTitle + '-1');
$toast[1] = toastr.info(sampleMsg, sampleTitle + '-2');
toastr.remove();
$toast[2] = toastr.info(sampleMsg, sampleTitle + '-3-Visible');
//Assert
ok($toast[2].is(':visible'), 'Toast after a remove is visible');
//Teardown
resetContainer();
});
module('info');
test('info - pass title and message', 3, function () {
//Arrange
Expand Down
11 changes: 8 additions & 3 deletions toastr.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@
}
}

function remove($toastElement) {
function remove($toastElement, removeOptions) {
var options = getOptions();
if (!$container) { getContainer(options); }
if ($toastElement && removeOptions && removeOptions.force) {
$toastElement.hide();
}
if ($toastElement && $(':focus', $toastElement).length === 0) {
removeToast($toastElement);
return;
Expand Down Expand Up @@ -322,8 +325,10 @@

function displayToast() {
$toastElement.hide();

options.onCreate();

if (typeof options.onCreate === 'function') {
options.onCreate();
}

$toastElement[options.showMethod](
{duration: options.showDuration, easing: options.showEasing, complete: options.onShown}
Expand Down