-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need Clarification for use with componentDidMount #75
Comments
The problem is probably that:
I'll close this issue since it's not related to axios-mock-adapter. |
@ctimmerm would be really nice if a solution was provided instead of a list of problems. It's nice to check the response and all, but what about the component, and how it was affected after the state changed? What about testing the display of response-triggered elements? Don't we want to test "deeper" than just the response data? @Jack-Barry After a little tinkering, this is what worked for me: import React from 'react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { mount, shallow } from 'enzyme';
import Clients from './Clients';
describe('Clients', () => {
let clients, mock;
const clientData =
[{'_id': '123', 'name': 'CBS', 'address': '100 Broadway Ave, New York, NY 15395'},
{'_id': '456', 'name': 'NBC', 'address': '4688 19th Street, New York, NY 12536'}];
beforeEach(() => {
mock = new MockAdapter(axios);
mock.onGet('api/clients').reply(200, clientData);
clients = mount(<Clients />);
});
it('gets client data and updates the state', () => {
clients.update();
expect(clients.state()).toHaveProperty('clients', clientData);
expect(clients.find('.card-body').length).toEqual(1);
expect(clients.find('.empty-text').length).toEqual(0);
expect(clients.find('table').length).toEqual(2);
expect(clients.find('tr').length).toEqual(3);
});
}); 🍻 |
It was not a list of problems, but a sequence of events that explains the problem. It is not a problem that is related to axios-mock-adapter and the way you handle it depends on the testing framework and other libraries you're using, and how you structure your code. it('does something', function(done) {
// ... your test code ...
setImmediate(function() {
expect(/* ... */);
done();
});
}); |
@jrodl3r Thanks for the tips! I actually rethought my approach after the correct albeit blunt response from @ctimmerm and just forgot to come back and provide the solution I came up with. I understand that this isn't an issue with the functionality of Separation of ConcernsI ended up writing the methods that make use of Build a Method to Make the AJAX CallLet's say I write a method called Use the Predictable AJAX Call Method in
|
Maybe I am just a bit illiterate when it comes to testing, but I was hoping to get some clarification and/or help on writing tests for calls made by
axios
from within thecomponentDidMount
function for React components.Here is a simple
componentDidMount
function which usesaxios
:and here is the setup and spec for the functionality:
The problem I run into is that within the spec,
state
never changes.As far as I understand it, when the
AjaxDemo
component gets mounted, theaxios
call it makes will receive a response frommock
, and update state according to the response frommock
. This would mean thatstate
should have been set, because theaxios
promise resolved and assigned an array of values tostate.posts
. Is there something I'm missing here? I feel like it's going to be embarrassingly obvious, or maybe there is a better way to split this testing up - maybe it's leaning too much into the integration test arena.Any help on this would be much appreciated!
An extra note:
I tried running this code with
as well as
and the call to
update()
did nothing noticeable.The text was updated successfully, but these errors were encountered: