Skip to content

Commit d04e812

Browse files
author
Takeharu.Oshida
authored
Merge pull request #61 from mobilusoss/feature/60
Enable to wait user interaction to close
2 parents 2bd2343 + d67866c commit d04e812

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Change Log
22

3+
### Ver 0.7.0
4+
* Enable to wait user interaction to close #60
5+
36
### Ver 0.6.1
47
* Update dependencies with `ncu -u`
58
* Add codebeat

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Notification.propTypes = {
6161

6262
* `onError(e, tag)` : Called when Desktop notification happen error.
6363

64-
* `timeout` : milli sec to close notification automatically.(Default 5000)
64+
* `timeout` : milli sec to close notification automatically. Ignored if `0` or less. (Default `5000`)
6565

6666
* `title` : Notification title.
6767

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-web-notification",
3-
"version": "0.6.1",
3+
"version": "0.7.0",
44
"description": "React component with HTML5 Web Notification API",
55
"main": "./lib/components/Notification.js",
66
"scripts": {

src/components/Notification.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ class Notification extends React.Component {
108108
const n = new window.Notification(this.props.title, opt);
109109
n.onshow = e => {
110110
this.props.onShow(e, opt.tag);
111-
setTimeout(() => {
112-
this.close(n);
113-
}, this.props.timeout);
111+
if (this.props.timeout > 0) {
112+
setTimeout(() => {
113+
this.close(n);
114+
}, this.props.timeout);
115+
}
114116
};
115117
n.onclick = e => { this.props.onClick(e, opt.tag); };
116118
n.onclose = e => { this.props.onClose(e, opt.tag); };

test/components/Notification_spec.js

+46-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,52 @@ describe('Test of Notification', () => {
288288
expect(args[1]).to.be.eql('mytag');
289289
});
290290
});
291-
291+
describe('test of autoClose timer', () => {
292+
const MY_TITLE = 'mytitle';
293+
const MY_OPTIONS = {
294+
tag: 'mytag',
295+
body: 'mybody',
296+
icon: 'myicon',
297+
lang: 'en',
298+
dir: 'ltr'
299+
};
300+
describe('when `props.timeout` is less than eql 0', () => {
301+
let n;
302+
before(() => {
303+
component = ReactTestUtils.renderIntoDocument(<Notification title={MY_TITLE} options={MY_OPTIONS} ignore={false} timeout={0}/>);
304+
n = component._getNotificationInstance('mytag');
305+
sinon.stub(n, 'close');
306+
n.onshow('showEvent');
307+
});
308+
after(() => {
309+
n.close.restore();
310+
})
311+
it('will not trigger close automatically', (done) => {
312+
setTimeout(() => {
313+
expect(n.close.called).to.be.eql(false);
314+
done();
315+
}, 200);
316+
})
317+
})
318+
describe('when `props.timeout` is greater than 0', () => {
319+
let n;
320+
before(() => {
321+
component = ReactTestUtils.renderIntoDocument(<Notification title={MY_TITLE} options={MY_OPTIONS} ignore={false} timeout={50}/>);
322+
n = component._getNotificationInstance('mytag');
323+
sinon.stub(n, 'close');
324+
n.onshow('showEvent');
325+
});
326+
after(() => {
327+
n.close.restore();
328+
});
329+
it('will trigger close automatically', (done) => {
330+
setTimeout(() => {
331+
expect(n.close.called).to.be.eql(true);
332+
done();
333+
}, 200);
334+
})
335+
})
336+
})
292337
describe('when swRegistration prop is defined', () => {
293338
const swRegistrationMock = { showNotification: sinon.stub().resolves({ notification: ee }) }
294339
const MY_TITLE = 'mytitle';

0 commit comments

Comments
 (0)