Skip to content
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
41 changes: 41 additions & 0 deletions components/snackbar/toastSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,45 @@ describe('Toasts:', () => {
}, 20);
});
});

describe('Toast _container null guard', () => {
afterEach(() => {
// Reset shared static state so other specs are unaffected
M.Toast.dismissAll();
M.Toast._removeContainer();
});

it('should not throw when _removeContainer is called with a null container', () => {
M.Toast._container = null;
expect(() => M.Toast._removeContainer()).not.toThrow();
expect(M.Toast._container).toBeNull('because there was no container to remove');
});

it('should not throw when _removeContainer is called twice', () => {
// First call removes a freshly created container, second call hits the guard.
M.Toast._createContainer();
expect(M.Toast._container).not.toBeNull('because a container was just created');

expect(() => M.Toast._removeContainer()).not.toThrow();
expect(M.Toast._container).toBeNull('because the container was just removed');

expect(() => M.Toast._removeContainer()).not.toThrow('because the guard protects the second call');
expect(M.Toast._container).toBeNull();
});

it('should not throw building a toast while _container is null', () => {
// Create a first toast so the container exists and _toasts is non-empty,
// then null the container to force the inconsistent state the guard handles
// (a second toast skips _createContainer because _toasts.length !== 0).
const first = new M.Toast({ text: 'First', displayLength: 50, inDuration: 10, outDuration: 10 });
expect(first.el).toBeDefined();
M.Toast._container = null;

let instance;
expect(() => {
instance = new M.Toast({ text: 'No container', displayLength: 50, inDuration: 10, outDuration: 10 });
}).not.toThrow('because _createToast guards the null container');
expect(instance.el).toBeDefined('because the toast element is still created without a container');
});
});
});
12 changes: 8 additions & 4 deletions components/snackbar/toasts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Toast {
velocityX: number;

static _toasts: Toast[];
static _container: HTMLElement;
private static _container: HTMLElement | null;
static _draggedToast: Toast;

constructor(options: Partial<ToastOptions>) {
Expand Down Expand Up @@ -125,8 +125,10 @@ export class Toast {
static _removeContainer() {
document.removeEventListener('mousemove', Toast._onDragMove);
document.removeEventListener('mouseup', Toast._onDragEnd);
Toast._container.remove();
Toast._container = null;
if (Toast._container) {
Toast._container.remove();
Toast._container = null;
}
}

static _onDragStart(e: TouchEvent | MouseEvent) {
Expand Down Expand Up @@ -217,7 +219,9 @@ export class Toast {
toast.classList.add(...this.options.classes.split(' '));
}
if (this.message) toast.innerText = this.message;
Toast._container.appendChild(toast);
if (Toast._container) {
Toast._container.appendChild(toast);
}
return toast;
}

Expand Down
10 changes: 7 additions & 3 deletions dist/js/materialize.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6831,8 +6831,10 @@ class Toast {
static _removeContainer() {
document.removeEventListener('mousemove', Toast._onDragMove);
document.removeEventListener('mouseup', Toast._onDragEnd);
Toast._container.remove();
Toast._container = null;
if (Toast._container) {
Toast._container.remove();
Toast._container = null;
}
}
static _onDragStart(e) {
if (e.target && e.target.closest('.toast')) {
Expand Down Expand Up @@ -6916,7 +6918,9 @@ class Toast {
}
if (this.message)
toast.innerText = this.message;
Toast._container.appendChild(toast);
if (Toast._container) {
Toast._container.appendChild(toast);
}
return toast;
}
_animateIn() {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/materialize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ declare class Toast {
deltaX: number;
velocityX: number;
static _toasts: Toast[];
static _container: HTMLElement;
private static _container;
static _draggedToast: Toast;
constructor(options: Partial<ToastOptions>);
static get defaults(): ToastOptions;
Expand Down
10 changes: 7 additions & 3 deletions dist/js/materialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6832,8 +6832,10 @@ var M = (function (exports) {
static _removeContainer() {
document.removeEventListener('mousemove', Toast._onDragMove);
document.removeEventListener('mouseup', Toast._onDragEnd);
Toast._container.remove();
Toast._container = null;
if (Toast._container) {
Toast._container.remove();
Toast._container = null;
}
}
static _onDragStart(e) {
if (e.target && e.target.closest('.toast')) {
Expand Down Expand Up @@ -6917,7 +6919,9 @@ var M = (function (exports) {
}
if (this.message)
toast.innerText = this.message;
Toast._container.appendChild(toast);
if (Toast._container) {
Toast._container.appendChild(toast);
}
return toast;
}
_animateIn() {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/materialize.min.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions dist/js/materialize.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6829,8 +6829,10 @@ class Toast {
static _removeContainer() {
document.removeEventListener('mousemove', Toast._onDragMove);
document.removeEventListener('mouseup', Toast._onDragEnd);
Toast._container.remove();
Toast._container = null;
if (Toast._container) {
Toast._container.remove();
Toast._container = null;
}
}
static _onDragStart(e) {
if (e.target && e.target.closest('.toast')) {
Expand Down Expand Up @@ -6914,7 +6916,9 @@ class Toast {
}
if (this.message)
toast.innerText = this.message;
Toast._container.appendChild(toast);
if (Toast._container) {
Toast._container.appendChild(toast);
}
return toast;
}
_animateIn() {
Expand Down