reactjs - How do I use Jest to test a React view wired to store? -


i have react view communicates store. i've tested views , stores separately, not in combination.

i followed structure documented here received typeerror. looks jest trying register store component if use dontmock.

using jest cli v0.2.0  pass  __tests__/unit/spec/stores/viewstore-spec.js (0.248s)  fail  __tests__/unit/spec/components/view-spec.react.js (0.942s) undefined ● view › defaults 0 unread   - typeerror: /users/matnorri/dev/projects/matnorri/web-client/src/app/scripts/components/view.react.js: /users/matnorri/dev/projects/matnorri/web-client/src/app/scripts/stores/viewstore.js: cannot call method 'register' of undefined         @ /users/matnorri/dev/projects/matnorri/web-client/src/app/scripts/stores/viewstore.js:43:31         @ object.runcontentwithlocalbindings (/users/matnorri/dev/projects/matnorri/web-client/node_modules/jest-cli/src/lib/utils.js:357:17)         ... 

i've included believe relevant code below, can provide in entirety if necessary.

view jest test

jest.dontmock('../../../../src/app/scripts/components/view.react'); jest.dontmock('../../../../src/app/scripts/stores/viewstore');  describe('view', function() {    var react;   var testutils;   var viewcomponent;   var view;    beforeeach(function() {     react = require('react/addons');     testutils = react.addons.testutils;     viewcomponent =       // tried both lines same effect.       // require('../../../../src/app/scripts/components/view.react.js');       require('../../../../src/app/scripts/components/view.react');     view = testutils.renderintodocument(<viewcomponent />);   });    it('defaults 0 unread', function() {     view._tostring = jest.genmockfunction();     view._onchange();     expect(view.state.unread).tobe(0);   }); }); 

thank you!

answer question

i ran issue , not mocking object-assign solved issue me. add jest.dontmock('object-assign'); spec file. solve problem.

// view jest test jest.dontmock('../../../../src/app/scripts/components/view.react'); jest.dontmock('../../../../src/app/scripts/stores/viewstore'); 

to mock or not mock

some more information when "mock or not mock" can found on jest documentation website.

jest modifies default behavior of "require" method , implements it's own require method. way able mock every function inside file. if want work (like store) should not mock file.

in case you're not mocking viewstore every function on store called. because didn't not mock object-assign object assign function doesn't work. way viewstore isn't created right way (it returns undefined) , test throws error cannot call method 'register' of undefined assign function didn't work, because mocked.

again; if read through documentation on jest website, you'll find detailed description when "mock or not mock".


extra info

i recommend add "object-assign" , other commonly used modules (like lodash/underscore) unmockedmodulepathpatterns in test config. more information this: https://facebook.github.io/jest/docs/api.html#config-unmockedmodulepathpatterns-array-string


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -