Skip to content

Commit

Permalink
Chore: Allow notifications to persist (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Press authored Jan 26, 2018
1 parent d43094f commit e8992c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/lib/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class Notification {
* @public
* @param {string} message - Notification message
* @param {string} [buttonText] - Optional text to show in button
* @param {boolean} persist - Should the notification show until dismissal or respect the timeout
* @return {void}
*/
show(message, buttonText) {
show(message, buttonText, persist = false) {
this.messageEl.textContent = message;

if (buttonText) {
Expand All @@ -58,7 +59,7 @@ class Notification {
this.notificationEl.focus();

// Hide notification automatically after a delay
this.timeout = setTimeout(this.hide.bind(this), HIDE_TIMEOUT_MS);
this.timeout = persist ? null : setTimeout(this.hide.bind(this), HIDE_TIMEOUT_MS);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/lib/__tests__/Notification-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable no-unused-expressions */
import Notification from '../Notification';

const HIDE_TIMEOUT_MS = 5000; // 5s

let notif;
let clock;

const sandbox = sinon.sandbox.create();

Expand Down Expand Up @@ -41,6 +44,12 @@ describe('lib/Notification', () => {
describe('show()', () => {
beforeEach(() => {
sandbox.stub(window, 'setTimeout');
sandbox.stub(notif, 'hide');
clock = sinon.useFakeTimers();
});

afterEach(() => {
clock.restore();
});

it('should properly show the notification', () => {
Expand All @@ -61,6 +70,18 @@ describe('lib/Notification', () => {
assert.equal(notif.messageEl.textContent, 'test');
assert.equal(notif.buttonEl.textContent, __('notification_button_default_text'));
});

it('should hide after the timeout', () => {
notif.show('test', 'test');
clock.tick(HIDE_TIMEOUT_MS + 1);
expect(notif.hide).to.be.called;
});

it('should not hide after the timeout if the notification is set to persist', () => {
notif.show('test', 'test', true);
clock.tick(HIDE_TIMEOUT_MS + 1);
expect(notif.hide).to.not.be.called;
});
});

describe('hide()', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/_common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ $header-height: 48px;
visibility: hidden;
}

.bp-notifications-wrapper {
position: absolute; // Override _boxui.scss because Preview cannot assume taking up the full viewport
}

.bp-has-header .bp-notifications-wrapper {
top: 48px;
}
Expand Down

0 comments on commit e8992c7

Please sign in to comment.