apla / me.apla.cordova.app-preferences

App preferences plugin for cordova
Apache License 2.0
201 stars 210 forks source link

How to write jasmine test cases for plugin #125

Open maruthi-menlo opened 7 years ago

maruthi-menlo commented 7 years ago

Issue:


I need to write test cases for this plugin in my project. Below is the way I tried in my project but I'm getting the error.

ts file: save(){
if (this.platform.is('cordova')){ var prefs = window['plugins'].appPreferences; prefs.store (null, this.onPrefStoreError, 'serverUrl', this._settings.serverUrl); prefs.store (null, this.onPrefStoreError, 'useProxy', this._settings.useProxy); }
localStorage['settings'] = JSON.stringify(this._settings); console.log('saved settings');
}

Spec file: it('call save', inject([SettingsService,Platform], (settingsService: SettingsService, platform: Platform) => {
settingsService.save(); }); });

FYI.. I mock the platform as true in my mock file. Here I'm getting the platform as cordova. I'm getting below error when I excute the above test case. TypeError: undefined is not an object (evaluating 'window['plugins'].appPreferences') in config/karma-shim.js (line 29367)

Can anyone please let me know how to mock this plugin.

My mock file: export class MockPlatformWithCordova { public ready(): any { return new Promise((resolve: Function) => { resolve(); }); }

public is(name: string){
    if (name === 'cordova')
        return true;
    return true;
}

}

Please specify your environment

Plugin version:

Toolchain:

Platforms affected:

What the scope of your problem: