3

I'm pretty new to using Karma and Jasmine and I'm trying to test an ionic/cordova based app which runs on iOS. The app is heavily using local sqlite database using cordova-sqlite-storage plugin.

And the application also has a code to use websql if cordova is undefined because one of my staff want to develop in on browsers to speed up. And it worked well when we use ionic serve command.

.factory('DB', [
'$q', '$cordovaSQLite', 'DB_CONFIG', '$http',
function($q, $cordovaSQLite, DB_CONFIG, $http) {
var self = this;
self.q = DB_CONFIG.q;
self.db = null;

self.init = function(name) {

    var deferred = $q.defer();
    if (window.cordova) {
        self.db = $cordovaSQLite.openDB({ name: name });
        deferred.resolve();
    }else{
        console.log ("non cordova");
        self.db = window.openDatabase(name, '1', 'database', 1024 * 1024 * 100);

        self.query("SELECT count(*) as cnt from sqlite_master WHERE type = 'table'").then(function(r){
                console.log (r.rows[0].cnt);
            if(r.rows[0].cnt > 1){
                deferred.resolve(true);
            }else{
                self.devinit(name).then(function(){
                    deferred.resolve(true);
                });
            }
        }, function (r) {
            console.log (r.toString());
        });
    }
    return deferred.promise;
};

So I tried to use the same approach on testing. But I'm experiencing weird symptom and couldn't find an information about it. When I start the test which use the above code, test didn't work well. The initialization code to be tested was not fully executed though it runs well on ionic.serve command

And when I try to run a sql command on a chrome, it showed the message

'Database has been closed'

Chrome screen shot

As you can see on the screenshot, it has 'sync.db'. So the database was created. But other queries to create table and put some data in it was not executed properly. And the database was closed.

If you have any information about the symptom, it would be really appreciated.

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73

0 Answers0