Skip to content

Commit 457b716

Browse files
author
Takeharu.Oshida
authored
Merge pull request #62 from mobilusoss/develop
Bump version from v0.6.1 to v0.7.0
2 parents 2bd2343 + a113a53 commit 457b716

File tree

7 files changed

+87
-33
lines changed

7 files changed

+87
-33
lines changed

.codebeatignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
lib/*
2-
example/*
1+
lib/**
2+
example/**
33
karma.conf.js

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

lib/components/Notification.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ function (_React$Component) {
160160
n.onshow = function (e) {
161161
_this3.props.onShow(e, opt.tag);
162162

163-
setTimeout(function () {
164-
_this3.close(n);
165-
}, _this3.props.timeout);
163+
if (_this3.props.timeout > 0) {
164+
setTimeout(function () {
165+
_this3.close(n);
166+
}, _this3.props.timeout);
167+
}
166168
};
167169

168170
n.onclick = function (e) {

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

+29-25
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,40 @@ class Notification extends React.Component {
8989
}
9090
}
9191

92+
doNotification() {
93+
let opt = this.props.options;
94+
if (typeof opt.tag !== 'string') {
95+
opt.tag = 'web-notification-' + seq();
96+
}
97+
if (this.notifications[opt.tag]) {
98+
return;
99+
}
92100

101+
if (this.props.swRegistration && this.props.swRegistration.showNotification) {
102+
this.props.swRegistration.showNotification(this.props.title, opt)
103+
this.notifications[opt.tag] = {};
104+
} else {
105+
const n = new window.Notification(this.props.title, opt);
106+
n.onshow = e => {
107+
this.props.onShow(e, opt.tag);
108+
if (this.props.timeout > 0) {
109+
setTimeout(() => {
110+
this.close(n);
111+
}, this.props.timeout);
112+
}
113+
};
114+
n.onclick = e => { this.props.onClick(e, opt.tag); };
115+
n.onclose = e => { this.props.onClose(e, opt.tag); };
116+
n.onerror = e => { this.props.onError(e, opt.tag); };
117+
118+
this.notifications[opt.tag] = n;
119+
}
120+
}
93121

94122
render() {
95123
let doNotShowOnActiveWindow = this.props.disableActiveWindow && this.windowFocus;
96124
if (!this.props.ignore && this.props.title && this.state.supported && this.state.granted && !doNotShowOnActiveWindow) {
97-
98-
let opt = this.props.options;
99-
if (typeof opt.tag !== 'string') {
100-
opt.tag = 'web-notification-' + seq();
101-
}
102-
103-
if (!this.notifications[opt.tag]) {
104-
if (this.props.swRegistration && this.props.swRegistration.showNotification) {
105-
this.props.swRegistration.showNotification(this.props.title, opt)
106-
this.notifications[opt.tag] = {};
107-
} else {
108-
const n = new window.Notification(this.props.title, opt);
109-
n.onshow = e => {
110-
this.props.onShow(e, opt.tag);
111-
setTimeout(() => {
112-
this.close(n);
113-
}, this.props.timeout);
114-
};
115-
n.onclick = e => { this.props.onClick(e, opt.tag); };
116-
n.onclose = e => { this.props.onClose(e, opt.tag); };
117-
n.onerror = e => { this.props.onError(e, opt.tag); };
118-
119-
this.notifications[opt.tag] = n;
120-
}
121-
}
125+
this.doNotification();
122126
}
123127

124128
// return null cause

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)