jeudi 7 avril 2016

How to unit test custom Ember.Inflector rules

I'm trying to create a test for the following initializer:

import Ember from 'ember';

const { Inflector } = Ember;

export function initialize() {
    Inflector.inflector.uncountable('settings');
    Inflector.inflector.uncountable('feedback');
}

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

Ember-cli generates the following test for an initializer:

import Ember from 'ember';
import InflectorInitializer from 'redirect/initializers/inflector';
import { module, test } from 'qunit';

let application;

module('Unit | Initializer | inflector', {
    beforeEach() {
      Ember.run(function() {
        application = Ember.Application.create();
        application.deferReadiness();
      });
    }
});

test('it works', function(assert) {
    InflectorInitializer.initialize(application);

    assert.ok(true);
});

As I understand it, a beforeEach() hook creates an application first, and then pauses it so the initializer test can run.

The test fails because Ember.Inflector in undefined when the initializer is called by the test.

How do I test custom inflector rules?

I'd like to have the following assertions:

assert.equal(inflector.singularize('settings'), 'settings'); 
assert.equal(inflector.pluralize('settings'), 'settings');
assert.equal(inflector.singularize('feedback'), 'feedback'); 
assert.equal(inflector.pluralize('feedback'), 'feedback');
assert.equal(inflector.rules.uncountable['settings'], true); 
assert.equal(inflector.rules.uncountable['feedback'], true); 

I'd be willing to configure inflector rules in a different way if this will make it testable.

I tried to create an application and remove deferReadiness to then see If I can put those assertions in the test. But even though Ember.Inflector is then defined, it doesn't contain my custom rules.




Aucun commentaire:

Enregistrer un commentaire