samedi 12 janvier 2019

Why does my Firebase injection fail to register and be available for acceptance tests?

I've imported the Firebase SDK into my Ember app. There is an initializer that injects Firebase into the entire app like so:

export function initialize(application) {

  const config = {
    apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    authDomain: "my-app-xxxxxx.firebaseapp.com",
    databaseURL: "https://my-app-xxxxxx.firebaseio.com",
    projectId: "my-app-xxxxxx",
    storageBucket: "my-app-xxxxxx.appspot.com",
    messagingSenderId: "xxxxxx"
  }

  if( typeof firebase !== 'undefined'  && firebase) {
    firebase.initializeApp(config);
    application.register('firebase:home', firebase, {instantiate: false});
    application.inject('route', 'firebase', 'firebase:home');
  }
}

export default {
  name: 'firebase',
  initialize
};

This is initializer properly injects firebase and makes it available in all my routes and templates as far as I can tell. I'm able to use authentication and read, write to the database correctly.

The problem happens when I try to write an acceptance test. For example a simple test where I enter the main route:

import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';

module('Acceptance | login', function(hooks) {

  setupApplicationTest(hooks);

  test('visiting /login', async function(assert) {

    await visit('/home');

    debugger;
    assert.equal(currentURL(), '/home');


  });
});

Hitting the home route instantly produces a window alert of ReferenceError: firebase is not defined. Shouldn't the initializer have handled this?

I don't understand why firebase is readily accessible and everything works when the app is running normally, yet it is undefined when used with an acceptance test.




Aucun commentaire:

Enregistrer un commentaire