Commit 7a78960c authored by Simon MacDonald's avatar Simon MacDonald
Browse files

[spec] Add tests

parent 0293676a
Loading
Loading
Loading
Loading
+116 −0
Changes for spec/index.spec.js: 116 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ var cordova = require('./helper/cordova'),

describe('phonegap-plugin-push', function() {
    beforeEach(function() {
        options = { android: {}, ios: {}, windows: {} };
        execWin = jasmine.createSpy();
        execSpy = spyOn(cordova.required, 'cordova/exec').andCallFake(execWin);
    });
@@ -41,4 +42,119 @@ describe('phonegap-plugin-push', function() {
            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();
                });
            });
        });
    });
});