Forked from arackaf/unit-test-react-component-with-mobx.js
Created
September 11, 2021 02:12
-
-
Save chase2981/633c2b1eb961bb55f7dcb11c030e642c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component, createElement } from "react"; | |
import Enzyme, { shallow, render, mount } from "enzyme"; | |
import Adapter from "enzyme-adapter-react-16"; | |
import { action, observable, computed } from "mobx"; | |
import { observer } from "mobx-react"; | |
Enzyme.configure({ adapter: new Adapter() }); | |
class Store { | |
@observable val = 0; | |
} | |
class Mock { | |
called = 0; | |
doIt = () => this.called++; | |
} | |
const clientObject = new Mock(); | |
@observer | |
class Wrapper extends Component { | |
render() { | |
return <Comp1 val={this.props.store.val} />; | |
} | |
} | |
class Comp1 extends Component { | |
componentDidUpdate(prevProps, prevState) { | |
if (this.props.val % 2 === 1) { | |
//hard coded - in real life the client library will be put here by my | |
//library, which I'm testing | |
clientObject.doIt(); | |
} | |
} | |
render() { | |
return null; | |
} | |
} | |
test("test1", () => { | |
let store = new Store(); | |
mount(<Wrapper store={store} />); | |
expect(clientObject.called).toBe(0); | |
store.val++; | |
expect(clientObject.called).toBe(1); | |
store.val++; | |
expect(clientObject.called).toBe(1); | |
store.val++; | |
expect(clientObject.called).toBe(2); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment