How to test a component which needs to access its parentview?


#1

My component looks like this:

export default Ember.Component.extend({

  register: function () {

    this.get( 'parentView' ).addItem( this );

  }.on( 'init' )

});

How do I init the parentView so that the following test works?

test('it renders', function() {
  expect(2);

  // creates the component instance
  var component = this.subject();
  equal(component._state, 'preRender');

  // appends the component to the page
  this.append();
  equal(component._state, 'inDOM');
});

#2

I am new to Ember.js, but was messing around with something similar and did this:

test('it can be constructed', function() {
    var component = this.subject({
        ...
        parentView: Ember.Object.create({
            ....
        })
    });
});

My approach was to mock the parentView. This worked and allowed me to write test that I wanted to.

I have no idea how good or bad this technique is, but it worked for me.