summaryrefslogtreecommitdiff
path: root/plugins/phonegap-plugin-push/spec/index.spec.js
blob: f794fae5ec3a14550291f8fe57097e7311989901 (plain)
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
/*!
 * Module dependencies.
 */

var cordova = require('./helper/cordova'),
    PushNotification = require('../www/push'),
    execSpy,
    execWin,
    options;

/*!
 * Specification.
 */

describe('phonegap-plugin-push', function() {
    beforeEach(function() {
        options = { android: {}, ios: {}, windows: {} };
        execWin = jasmine.createSpy();
        execSpy = spyOn(cordova.required, 'cordova/exec').andCallFake(execWin);
    });

    describe('PushNotification', function() {
        it("should exist", function() {
            expect(PushNotification).toBeDefined();
            expect(typeof PushNotification == 'object').toBe(true);
        });

        it("should contain a init function", function() {
            expect(PushNotification.init).toBeDefined();
            expect(typeof PushNotification.init == 'function').toBe(true);
        });

        it("should contain a unregister function", function() {
            var push = PushNotification.init({});
            expect(push.unregister).toBeDefined();
            expect(typeof push.unregister == 'function').toBe(true);
        });

        it("should contain a setApplicationIconBadgeNumber function", function() {
            var push = PushNotification.init({});
            expect(push.setApplicationIconBadgeNumber).toBeDefined();
            expect(typeof push.setApplicationIconBadgeNumber == 'function').toBe(true);
        });
    });
    
    describe('PushNotification instance', function() {
        describe('cordova.exec', function() {
            it('should call cordova.exec on next process tick', function(done) {
                PushNotification.init(options);
                setTimeout(function() {
                    expect(execSpy).toHaveBeenCalledWith(
                        jasmine.any(Function),
                        jasmine.any(Function),
                        'PushNotification',
                        'init',
                        jasmine.any(Object)
                    );
                    done();
                }, 100);
            });
        });

        describe('on "registration" event', function() {
            it('should be emitted with an argument', function(done) {
                execSpy.andCallFake(function(win, fail, service, id, args) {
                    win({ 'registrationId': 1 });
                });
                var push = PushNotification.init(options);
                push.on('registration', function(data) {
                    expect(data.registrationId).toEqual(1);
                    done();
                });
            });
        });

        describe('on "notification" event', function() {
            beforeEach(function() {
                execSpy.andCallFake(function(win, fail, service, id, args) {
                    win({
                        message: 'Message',
                        title: 'Title',
                        count: 1,
                        sound: 'beep',
                        image: 'Image',
                        additionalData: {}
                    });
                });
            });

            it('should be emitted on success', function(done) {
                var push = PushNotification.init(options);
                push.on('notification', function(data) {
                    done();
                });
            });

            it('should provide the data.message argument', function(done) {
                var push = PushNotification.init(options);
                push.on('notification', function(data) {
                    expect(data.message).toEqual('Message');
                    done();
                });
            });

            it('should provide the data.title argument', function(done) {
                var push = PushNotification.init(options);
                push.on('notification', function(data) {
                    expect(data.title).toEqual('Title');
                    done();
                });
            });

            it('should provide the data.count argument', function(done) {
                var push = PushNotification.init(options);
                push.on('notification', function(data) {
                    expect(data.count).toEqual(1);
                    done();
                });
            });

            it('should provide the data.sound argument', function(done) {
                var push = PushNotification.init(options);
                push.on('notification', function(data) {
                    expect(data.sound).toEqual('beep');
                    done();
                });
            });

            it('should provide the data.image argument', function(done) {
                var push = PushNotification.init(options);
                push.on('notification', function(data) {
                    expect(data.image).toEqual('Image');
                    done();
                });
            });

            it('should provide the data.additionalData argument', function(done) {
                var push = PushNotification.init(options);
                push.on('notification', function(data) {
                    expect(data.additionalData).toEqual({});
                    done();
                });
            });
        });

        describe('on "error" event', function() {
            it('should be emitted with an Error', function(done) {
                execSpy.andCallFake(function(win, fail, service, id, args) {
                    fail('something went wrong');
                });
                var push = PushNotification.init(options);
                push.on('error', function(e) {
                    expect(e).toEqual(jasmine.any(Error));
                    expect(e.message).toEqual('something went wrong');
                    done();
                });
            });
        });
    });
});