forked from Nickersoft/push.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_spec.js
More file actions
174 lines (111 loc) · 4.21 KB
/
Copy pathpush_spec.js
File metadata and controls
174 lines (111 loc) · 4.21 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
describe('initialization', function () {
it('should create a new instance', function () {
expect(window.Push !== undefined).toBeTruthy();
});
it('isSupported should return a boolean', function () {
expect(typeof Push.isSupported).toBe('boolean');
});
});
describe('permission', function () {
var callback, // Empty callback
noop; // No operator (empty function)
beforeEach(function () {
callback = jasmine.createSpy('callback');
noop = function () {};
});
it('should have permission stored as a string constant', function () {
expect(typeof Push.Permission.get()).toBe('string');
});
it('should update permission value if permission is denied and execute callback', function (done) {
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.DENIED);
});
Push.Permission.request(noop, callback);
setTimeout(function () {
expect(Push.Permission.has()).toBe(false);
expect(callback).toHaveBeenCalled();
done();
}, 500);
});
it('should request permission if permission is not granted', function () {
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.DENIED);
});
Push.Permission.request();
Push.create('hello world!');
expect(window.Notification.requestPermission).toHaveBeenCalled();
});
it('should update permission value if permission is granted and execute callback', function (done) {
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.GRANTED);
});
Push.Permission.request(callback, noop);
setTimeout(function () {
expect(Push.Permission.has()).toBe(true);
expect(callback).toHaveBeenCalled();
done();
}, 500);
});
});
describe('creating notifications', function () {
var callback;
beforeAll(function () {
jasmine.clock().install();
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.GRANTED);
});
});
beforeEach(function () {
callback = jasmine.createSpy('callback');
});
it('should throw exception if no title is provided', function () {
expect(function() {
Push.create();
}).toThrow();
});
it('should return promise successfully', function () {
var wrapper = Push.create('hello world');
expect(wrapper.then).not.toBe(undefined);
});
it('should return the correct notification count', function () {
var count;
count = Push.count;
Push.create('hello world!');
expect(Push.count).toBe(count + 1);
});
});
describe('closing notifications', function () {
it('should close notifications if a timeout is specified', function () {
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
cb(Push.Permission.GRANTED);
});
spyOn(window.Notification.prototype, 'close');
Push.create('hello world!', {
timeout: 1000
});
expect(window.Notification.prototype.close).not.toHaveBeenCalled();
jasmine.clock().tick(1000);
expect(window.Notification.prototype.close).toHaveBeenCalled();
});
it('should close a notification given a tag', function () {
var count;
spyOn(window.Notification.prototype, 'close');
Push.create('hello world!', {
tag: 'foo'
});
count = Push.count;
Push.close('foo');
expect(window.Notification.prototype.close).toHaveBeenCalled();
expect(Push.count).toBe(count - 1);
});
it('should close all notifications when cleared', function () {
spyOn(window.Notification.prototype, 'close');
Push.create('hello world!', {
tag: 'foo'
});
expect(Push.count).toBeGreaterThan(0);
Push.clear();
expect(window.Notification.prototype.close).toHaveBeenCalled();
expect(Push.count).toBe(0);
});
});