This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathangular-notification.js
127 lines (101 loc) · 3.9 KB
/
angular-notification.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var expect = chai.expect;
describe('Notification provider', function () {
var $window, Notification;
beforeEach(module('notification'));
beforeEach(module(function (NotificationProvider) {
NotificationProvider.setOptions({ foo: 'bar' });
}));
beforeEach(inject(function ($injector) {
$window = $injector.get('$window');
Notification = $injector.get('Notification');
$window.Notification = sinon.spy();
$window.Notification.prototype.addEventListener = sinon.spy();
$window.Notification.prototype.close = sinon.spy();
$window.Notification.requestPermission = sinon.spy(requestPermission);
$window.Notification.respondPermission = respondPermission;
function requestPermission(callback) {
$window.Notification._permissionCb = callback;
}
function respondPermission(permission) {
$window.Notification._permissionCb(permission);
}
}));
describe('#setOptions', function () {
it('should define default options', function () {
new Notification('title');
$window.Notification.respondPermission('granted');
expect($window.Notification).to.be.calledWith('title', {
foo: 'bar'
});
});
});
it('should return an error if not supported', function () {
$window.Notification = null;
function createNotification() {
new Notification();
};
expect(createNotification)
.to.throw('This browser does not support desktop notification.');
});
describe('#requestPermission', function () {
it('should request if permission is "default"', function () {
$window.Notification.permission = 'default';
new Notification();
expect($window.Notification.requestPermission).to.be.called;
});
it('should request if permission is not defined', function () {
$window.Notification.permission = undefined;
new Notification();
expect($window.Notification.requestPermission).to.be.called;
});
it('should set permission', function () {
$window.Notification.permission = undefined;
new Notification();
$window.Notification.respondPermission('granted');
expect($window.Notification.requestPermission).to.be.called;
expect($window.Notification.permission).to.equal('granted');
});
});
describe('#$on', function () {
it('should register events when permission is "granted" at start', function () {
$window.Notification.permission = 'granted';
var notification = new Notification();
notification.$on('close', function () {});
expect(notification.baseNotification.addEventListener).to.be.calledWith('close');
});
it('should register events when permission is not "granted" at start', function () {
var notification = new Notification();
notification.$on('close', function () {});
$window.Notification.respondPermission('granted');
expect(notification.baseNotification.addEventListener).to.be.calledWith('close');
});
});
describe('#close', function () {
it('should do nothing if notification is not created', function () {
var notification = new Notification();
notification.close();
});
it('should close notification if created', function () {
$window.Notification.permission = 'granted';
var notification = new Notification();
notification.close();
expect(notification.baseNotification.close).to.be.called;
});
});
describe('#delay', function () {
var clock;
beforeEach(function () {
clock = sinon.useFakeTimers();
});
afterEach(function () {
clock.restore();
});
it('should be possible to set a delay', function () {
$window.Notification.permission = 'granted';
var notification = new Notification('title', { delay: 1000 });
expect(notification.baseNotification.close).to.not.be.called;
clock.tick(1001);
expect(notification.baseNotification.close).to.be.called;
});
});
});