mardi 1 décembre 2015

How test Service working with IndexedDB in AngularJS

I have app in AngularJS, where I make services, which stores data to IndexedDb. It's look like this:

var IndexedDbServices = angular.module('IndexedDbServices', []);

IndexedDbServices.factory('IndexedDb', ['$window', '$q', function($window, $q){
    var indexedDB = $window.indexedDB;
    var db=null;

    var open = function() {
        var deferred = $q.defer();
        var version = 3;
        var request = indexedDB.open("lCApp", version);

        request.onupgradeneeded = function(e) {
            db = e.target.result;    
            e.target.transaction.onerror = indexedDB.onerror;

            if(db.objectStoreNames.contains("collections")) {
                db.deleteObjectStore("collections");
            }

            var store = db.createObjectStore("collections", {
                keyPath: "id", autoIncrement : true
            });
            store.createIndex("name", "name", { unique: true });
            store.createIndex("description", "description", { unique: false });

            store.transaction.oncomplete = function(event) {
                var customerObjectStore = db.transaction("collections", "readwrite").objectStore("collections");
                customerObjectStore.add({name: "coll1", description: "desc for coll1"});
                customerObjectStore.add({name: "coll2", description: "desc for coll2"});
            };    
        };

        request.onsuccess = function(e) {
            db = e.target.result;
            deferred.resolve();
        };

        request.onerror = function(){
            deferred.reject();
        };

        return deferred.promise;
    };


    var findAll = function() {
        var deferred = $q.defer();
        if(db === null || db === undefined) {
            deferred.reject("IndexDB is not opened yet!");
        } else{
            var trans = db.transaction(["collections"], "readwrite");
            var store = trans.objectStore("collections");
            var data = [];

            // Get everything in the store;
            var keyRange = IDBKeyRange.lowerBound(0);
            var cursorRequest = store.openCursor(keyRange);

            cursorRequest.onsuccess = function(e) {
                var result = e.target.result;
                if(result === null || result === undefined)
                {
                    deferred.resolve(data);
                }
                else{
                    data.push(result.value);
                    result.continue();
                }
            };

            cursorRequest.onerror = function(e){
                console.log(e.value);
                deferred.reject("Something went wrong!!!");
            };
        }

        return deferred.promise;
    };

    return {
        open: open,
        add: add,
        findAll: findAll
    };
}]);

And I want test this code using unit test (jasmine). So i wrote this:

describe('IndexedDB service', function() {
    beforeEach(function() {
      module('IndexedDbServices');
    });

    it('should open db and find 2 collections', inject(function(IndexedDb) {
      IndexedDb.open().then(function(e){
        console.log(e);
      }, function(err){
        $window.alert(err);
      });

      console.log(IndexedDb.findAll());
  }));
});

Would you advise me how test if function open in IndexedDb service create IndexedDb and add rows. And next I function open in IndexedDB service return all that records?

I don't know why, but in this test db is not created

console.log(IndexedDb.findAll());
print: Promise{$$state: Object{status: 2, value: 'IndexDB is not opened yet!'}} So Db is not created. And next how use expect(..).toEqual() or something this in returned promises?

Aucun commentaire:

Enregistrer un commentaire