moxios

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,

Wrapping async moxios call in act callback

ⅰ亾dé卋堺 提交于 2020-01-25 09:16:07
问题 I am trying to test a react functional component using hooks. The useEffect hook makes a call to a third part API which then calls setState on return. I have the test working but keep getting a warning that an update to the component was not wrapped in act. The problem I have is that the expectation is inside a moxios.wait promise and therefore I cannot wrap that in an act function and then assert on the result of that. The test passes but I know not wrapping code that updates state in an act

Matching axios POST request with moxios

╄→尐↘猪︶ㄣ 提交于 2020-01-03 10:44:53
问题 Is it possible to use moxios to mock a reply to a POST request that will match not just by URL but also by the POST body? Inspecting the body afterwards would work for me too. This is what I am doing now. As far as I know there are no method specific stubbing methods: describe('createCode', function () { it('should create new code', function () { moxios.stubRequest(process.env.API_URL + '/games/GM01/codes', { status: 200 }) }) }) 回答1: There is a way to inspect the last axios request using

Matching axios POST request with moxios

扶醉桌前 提交于 2020-01-03 10:44:17
问题 Is it possible to use moxios to mock a reply to a POST request that will match not just by URL but also by the POST body? Inspecting the body afterwards would work for me too. This is what I am doing now. As far as I know there are no method specific stubbing methods: describe('createCode', function () { it('should create new code', function () { moxios.stubRequest(process.env.API_URL + '/games/GM01/codes', { status: 200 }) }) }) 回答1: There is a way to inspect the last axios request using