katowulf / mockfirebase

Firebase mock library for writing unit tests (experimental)
157 stars 40 forks source link

firebase sdk 3.0 support #109

Open ciekawy opened 8 years ago

ciekawy commented 8 years ago

what about new sdk 3.0 support? as there is an official release this would be an interested option

https://firebase.google.com/docs/web/setup

kpgarrod commented 7 years ago

I would also be interested in this or any alternative approach to testing Angular2 apps with AngularFire2. Any plans?

aaweb commented 7 years ago

+1

simonajones commented 7 years ago

+1

Nightbr commented 7 years ago

+1 and we are waiting for this here: https://github.com/firebase/firebase-util/issues/96#issuecomment-232568126 Thanks

soumak77 commented 7 years ago

@katowulf I wouldn't mind helping out to update this library to support firebase sdk 3.x.x if you can provide feedback on what needs to be changed

I added the following to MockFirebase.override so that it overrides the window.firebase reference. This allows using the new syntax of firebase.database().ref() in both source and tests.

window.firebase = {
  database: function() {
    return {
      ref: function(path) {
        return new window.mockfirebase.MockFirebase(path);
      }
    };
  }
};

I then updated MockFirebase.ref to be a property instead of a function so that it passes the $firebaseUtils.assertValidRef check. Those updates allow the following test to pass:

Source

export default class HomeController {
  constructor($firebaseArray) {
    var ref = firebase.database().ref().child('messages');
    this.messages = $firebaseArray(ref);
    return this;
  }

  submit(message) {
    return this.messages.$add(message);
  }
};

HomeController.$inject = ['$firebaseArray'];

Test

describe('HomeController', function() {
  window.MockFirebase.override();

  var controller;

  beforeEach(function() {
    module('application.home');

    inject(function($controller, $firebaseArray) {
      controller = $controller('HomeController', {
        $firebaseArray: $firebaseArray
      });
    });
  });

  it('should read data from firebase', function() {
    //save some data that our controller will read
    var message = 'message';
    controller.submit(message).then(function() {
      expect(controller.messages).toContain(message);
    });
  });
});

The test above works only because I have the submit function returning a promise and use that to wait for the write to be completed. I tried to make it work by just calling $add then using firebaseRef.flush() to flush the write, however, upon calling flush() I get the error:

Error: No deferred tasks to be flushed

EDIT Attempting to use firebaseRef.autoFlush() results in a test failure:

Expected [  ] to contain 'message'.
soumak77 commented 7 years ago

I was able to get basic support added for firebase sdk 3.x. See https://github.com/katowulf/mockfirebase/pull/112 for more info.

giltotherescue commented 7 years ago

Any update on this?

Grantlyk commented 7 years ago

I would also be interested in using mockFirebase with the 3.x.x SDK -- It seems that this project is dead now? I'd love to contribute to getting mockFirebase ready