enzyme

How to test mapStateToProps using React Redux and Jest?

孤街醉人 提交于 2021-02-11 06:07:52
问题 When I create a test for my connected React component where I want to test the mapStateToProps logic I run into a problem which I'm not sure how to solve. Error message Expected: 1 Received: undefined 24 | it('should show previously rolled value', () => { 25 | // test that the state values were correctly passed as props > 26 | expect(wrapper.props().lastRolledNumber).toBe(1); When I check the wrapper.props() it only returns the store object and no properties. To make sure it's not my code I

How to test mapStateToProps using React Redux and Jest?

眉间皱痕 提交于 2021-02-11 06:07:21
问题 When I create a test for my connected React component where I want to test the mapStateToProps logic I run into a problem which I'm not sure how to solve. Error message Expected: 1 Received: undefined 24 | it('should show previously rolled value', () => { 25 | // test that the state values were correctly passed as props > 26 | expect(wrapper.props().lastRolledNumber).toBe(1); When I check the wrapper.props() it only returns the store object and no properties. To make sure it's not my code I

React Jest/Enzyme Testing: useHistory Hook Breaks Test

烂漫一生 提交于 2021-02-10 17:45:48
问题 I'm fairly new to React, so please forgive my ignorance. I have a component: const Login: FunctionComponent = () => { const history = useHistory(); //extra logic that probably not necessary at the moment return ( <div> <form action=""> ...form stuff </form> </div> ) } When attempting to write jest/enzyme test, the one test case I've written is failing with the following error ` › encountered a declaration exception TypeError: Cannot read property 'history' of undefined` I've tried to use jest

how to test async function making use of moxios?

限于喜欢 提交于 2021-02-10 17:29:49
问题 Consider the following react code. sendFormData = async formData => { try { const image = await axios.post("/api/image-upload", formData); this.props.onFileNameChange(image.data); this.setState({ uploading: false }); } catch (error) { console.log("This errors is coming from ImageUpload.js"); console.log(error); } }; Here is the test it("should set uploading back to false on file successful upload", () => { const data = "dummydata"; moxios.stubRequest("/api/image-upload", { status: 200,

how to test async function making use of moxios?

℡╲_俬逩灬. 提交于 2021-02-10 17:29:20
问题 Consider the following react code. sendFormData = async formData => { try { const image = await axios.post("/api/image-upload", formData); this.props.onFileNameChange(image.data); this.setState({ uploading: false }); } catch (error) { console.log("This errors is coming from ImageUpload.js"); console.log(error); } }; Here is the test it("should set uploading back to false on file successful upload", () => { const data = "dummydata"; moxios.stubRequest("/api/image-upload", { status: 200,

React - how do I unit test an API call in Jest?

安稳与你 提交于 2021-02-10 17:26:42
问题 I have a bunch of API calls that I would like to unit test. As far as I know, unit testing API calls doesn't involve actually making those API calls. As far as I know you would simulate responses of those API calls and then test on the DOM changes however I'm currently struggling to do this. I have the following code: App.js function App() { const [text, setText] = useState(""); function getApiData() { fetch('/api') .then(res => res.json()) .then((result) => { console.log(JSON.stringify

React - how do I unit test an API call in Jest?

人盡茶涼 提交于 2021-02-10 17:23:11
问题 I have a bunch of API calls that I would like to unit test. As far as I know, unit testing API calls doesn't involve actually making those API calls. As far as I know you would simulate responses of those API calls and then test on the DOM changes however I'm currently struggling to do this. I have the following code: App.js function App() { const [text, setText] = useState(""); function getApiData() { fetch('/api') .then(res => res.json()) .then((result) => { console.log(JSON.stringify

React - how do I unit test an API call in Jest?

那年仲夏 提交于 2021-02-10 17:19:50
问题 I have a bunch of API calls that I would like to unit test. As far as I know, unit testing API calls doesn't involve actually making those API calls. As far as I know you would simulate responses of those API calls and then test on the DOM changes however I'm currently struggling to do this. I have the following code: App.js function App() { const [text, setText] = useState(""); function getApiData() { fetch('/api') .then(res => res.json()) .then((result) => { console.log(JSON.stringify

SyntaxError: Invalid or unexpected token @import

怎甘沉沦 提交于 2021-02-09 02:45:51
问题 I am trying to add Jest and Enzyme to a React webpack project. Everything is working, until I add tests to a component, with a style sheet importing google fonts. The error I get is: ● Test suite failed to run /Users/dev/Code/Git/react-redux-webpack/src/App.sass:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){@import url('https://fonts.googleapis.com/css?family=Barlow+Condensed') ^ SyntaxError: Invalid or unexpected token The sass file looks like:

SyntaxError: Invalid or unexpected token @import

旧时模样 提交于 2021-02-09 02:45:33
问题 I am trying to add Jest and Enzyme to a React webpack project. Everything is working, until I add tests to a component, with a style sheet importing google fonts. The error I get is: ● Test suite failed to run /Users/dev/Code/Git/react-redux-webpack/src/App.sass:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){@import url('https://fonts.googleapis.com/css?family=Barlow+Condensed') ^ SyntaxError: Invalid or unexpected token The sass file looks like: