13

I'm testing a React/Reflux application using Jest. I have the following function in a store:

onLoad: function() {
  console.log("ORIGINAL LOAD");
  // http request here
}

I'm trying to mock it out so that it just does what it needs to do without doing the actual network stuff:

beforeEach(function() {

  // mock out onLoad so instead of making API call the store gets test data
  PostStore.onLoad = jest.genMockFunction().mockImplementation(function () {
    var p1 = new Post(
      "54da7df5119025513400000a",                    // id
      "Test Post",                                   // title
      "Kji6ftLjUqhElgnqOBqMUKxYONpU7nK/cu6jTA==\n",  // owner anonId
      "Test Course 1",                               // course name
      "This is a test!",                             // content
      6,                                             // upvotes
      2,                                             // downvotes
      ["Kji6ftLjUqhElgnqOBqMUKxYONpU7nK/cu6jTA==\n"] // voter anonIds
    );

    this.posts = [p1];
    console.log("mocked function");
  });

  // component initialized here
});

However, it seems like the mocked function never even gets created. When I run the tests, the console still logs ORIGINAL LOAD.

What's the correct way to override the object's method so that instead of setting the posts array in PostStore by doing an ajax call it just sets it with test data?

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
Hugo
  • 2,186
  • 8
  • 28
  • 44
  • Did you ever get this resolved? I'm having a very similar issue, but I'm testing with Jasmine and injecting dependencies using Rewire. I've logged an issue on the Reflux GitHub repo: https://github.com/spoike/refluxjs/issues/300 – Simon Adcock Mar 23 '15 at 08:46
  • I never resolved it, but for Jasmine I recommend using Sinon to mock things out. – Hugo Mar 23 '15 at 12:14
  • thanks, it turned out to be a limitation of Rewire, injecting the dependency after Reflux's store mixin was loaded. I ended up going with [inject-loader](https://github.com/plasticine/inject-loader) for webpack – Simon Adcock Mar 28 '15 at 20:55
  • Are you doing var PostStore = require('PostStore') before you actually use PostStore in your test? I believe you'll have to define your mockImplementation after the require statement to ensure that the jest auto mocking doesn't overwrite your mockImplementation. – pherris Apr 13 '15 at 21:06
  • I'm not sure if I was. I rewrote it using just rails instead. – Hugo Apr 13 '15 at 22:05

2 Answers2

6

I have found jest mock instance function It's here

eg:

import expect from 'expect';

jest.mock('../libs/utils/Master');
import Master from '../libs/utils/Master';

Master.mockImplementation(() => {
  return {
    returnOne: jest.fn().mockReturnValueOnce(1)
  }
})
describe('test Master', function() {
  it('test search', function() {
    let master = new Master();
    expect(master.returnOne()).toEqual(1);
  });
});
adriantoine
  • 2,160
  • 1
  • 15
  • 14
Yi zhang
  • 127
  • 1
  • 6
5

Almost there, all you need to do is

const onLoad = jest.fn().mockImplementation(function () {
    var p1 = new Post();//Add your stuff here
    this.posts = [p1];
    console.log("mocked function");
  });
PostStore.onLoad = onLoad.bind(PostStore); //PostStore is your object.
mdsAyubi
  • 1,225
  • 9
  • 9