Factory, and stubbing an Astronomy method?


#1

This might be a bit advanced, but I’ve got this Astronomy model:

export const Insight = Class.create({
  name: 'Insight',
  collection: Insights,
  behaviors: ['timestamp', 'softremove'],
  secured: false,
  // ...
  methods: {
    getCoverImageUrl() {
      if (this.coverAttachment) {
        const attachment = Attachment.findOne(this.coverAttachment);

        return attachment ? attachment.metadata.url : '';
      }
    },
  // ...
};

// Factory for running tests
Factory.define('insight', Insights, {
  attachments: [],
  coverAttachment: '',
  description: 'This is some insight',
  getCoverImageUrl() {
    return 'http://placehold.it/600x400';
  },
  orgId: 'org1',
  ratings: [],
  related: [],
  tagIds: [],
  title: 'Test Insight',
});

And this is my test:

describe('<Insight />', function () {
  if (Meteor.isServer) return;

  let clickInsight, insight, getCoverImageUrl;

  beforeEach(function () {
    StubCollections.stub(Insights);
    clickInsight = sinon.spy();
    insight = Factory.create('insight');
    insight.getCoverImageUrl(); // just testing
  });

  afterEach(function () {
    StubCollections.restore();
  });

  it('does something', function () {
    const wrapper = shallow(
      <Insight insight={insight} onClickInsight={clickInsight} />
    );

    wrapper.find('.insight').simulate('click');
  });
});

But the test fails:

TypeError: insight.getCoverImageUrl is not a function
    at Context.<anonymous> (app/app.js?hash=091256c96cf78981b6ddcffdc3792d4fc6ca44ef:239:13)
    at callFn (packages/practicalmeteor_mocha.js?hash=489e07e2e98906b5ed07059bc93d8f53fe79c00b:3227:21)
    at Hook.Runnable.run (packages/practicalmeteor_mocha.js?hash=489e07e2e98906b5ed07059bc93d8f53fe79c00b:3220:7)
    at next (packages/practicalmeteor_mocha.js?hash=489e07e2e98906b5ed07059bc93d8f53fe79c00b:3564:10)
    at packages/practicalmeteor_mocha.js?hash=489e07e2e98906b5ed07059bc93d8f53fe79c00b:3586:5
    at timeslice (packages/practicalmeteor_mocha.js?hash=489e07e2e98906b5ed07059bc93d8f53fe79c00b:1962:29)

Apparently stubbing with sinon didn’t work for some reason. What am I missing?


#2

Sorry for bump, you probably already fixed this issue.
You have to create a new Insight object with the JSON you get from te factory as in
new Insight(Factory.create('insight'))