javascript - React/Flux + Jasmine - Expected spy to have been called fails -
i'm not sure why happens, have simple component , test, however, fails on ✖ should call getstate on teststore. getstatefromstores called, should called well, right? clueless atm.
import react, { proptypes } 'react' import teststore '../stores/teststore' export default class testcomponent extends react.component { static proptypes = { } static getstatefromstores() { return teststore.getstate() } constructor(props) { super(props) this.state = testcomponent.getstatefromstores() } render() { return ( <div> <img src='' alt=''/> </div> ) } } test:
var react = require('react') var testutils = require('react/lib/reacttestutils') var immutable = require('immutable') const mockteststoredata = immutable.fromjs({ one: { foo: 'bar' }, two: { bar: 'baz' } }) describe('testcomponent.jsx', () => { var teststore var testcomponent var testcomponentel var renderedrootelement var rendereddomnode beforeeach(() => { teststore = require('../../stores/teststore') spyon(teststore, 'getstate') // .and.returnvalue(mockteststoredata) testcomponent = require('../testcomponent.jsx') spyon(testcomponent, 'getstatefromstores') testcomponentel = react.createelement(testcomponent) renderedrootelement = testutils.renderintodocument(testcomponentel) rendereddomnode = react.finddomnode(renderedrootelement) }) it('should rendered within div', () => { expect(rendereddomnode.tagname.touppercase()).toequal('div') }) it('should have static getstatefromstores function', () => { expect(testcomponent.getstatefromstores).tobedefined() }) it('should call getstatefromstores on construction', () => { expect(testcomponent.getstatefromstores).tohavebeencalled() }) it('should call getstate on teststore', () => { expect(teststore.getstate).tohavebeencalled() }) })
teststore.getstate() supposed called testcomponent.getstatefromstores(), spying on testcomponent.getstatefromstores():
... spyon(testcomponent, 'getstatefromstores'); ... so actual implementation not being invoked during tests. spy on function , have called, change to:
spyon(testcomponent, 'getstatefromstores').and.callthrough(); that being said, testing whether or not methods called inside module testing perhaps implementation specific. it's better keep test more cause-and-effect oriented. suggested solution make following adjustments:
var react = require('react') var testutils = require('react/lib/reacttestutils') var immutable = require('immutable') const mockteststoredata = immutable.fromjs({ one: { foo: 'bar' }, two: { bar: 'baz' } }) describe('testcomponent.jsx', () => { var teststore var testcomponent var testcomponentel var renderedrootelement var rendereddomnode beforeeach(() => { teststore = require('../../stores/teststore') spyon(teststore, 'getstate').and.returnvalue(mockteststoredata); testcomponent = require('../testcomponent.jsx') // don't spyon(testcomponent, 'getstatefromstores') testcomponentel = react.createelement(testcomponent) renderedrootelement = testutils.renderintodocument(testcomponentel) rendereddomnode = react.finddomnode(renderedrootelement) }) it('should rendered within div', () => { expect(rendereddomnode.tagname.touppercase()).toequal('div') }) it('should have static getstatefromstores function', () => { expect(testcomponent.getstatefromstores).tobedefined() }) // don't //it('should call getstatefromstores on construction', () => { // expect(testcomponent.getstatefromstores).tohavebeencalled() //}) // don't //it('should call getstate on teststore', () => { // expect(teststore.getstate).tohavebeencalled() //}) // instead it( 'should initial state teststore', () => { expect(teststore.getstate).tohavebeencalled(); // i'd optional expect( testcomponent.state ).toequal( mockteststoredata ); }) }) now you're free change implementation , testing important: state of testcomponent after initialized.
Comments
Post a Comment