Currently, I am in the process of writing tests for a component within my Ember application. This particular component is responsible for executing an ajax POST request to communicate with my servers API and retrieve a file location string as a response.
To simulate this interaction, I have included a route in the mirage/config.js
file that should handle the ajax request.
this.post('/foobar', () => {
return "test";
});
Upon running my tests, I noticed in both the network tab and console that the ajax request is attempting to connect to localhost but encounters a Connection Refused error:
POST http://localhost:8000/foobar net::ERR_CONNECTION_REFUSED
.
Even after placing a debugger in the mirage/config.js
file, it appears that the route configuration is being overlooked during testing, despite having all the necessary URL and namespace settings configured.
When launching my application on localhost and accessing the endpoint, Mirage correctly handles the request. However, this behavior does not extend to testing scenarios.
The snippet below demonstrates the code responsible for initiating the ajax request:
ajax.request(
uri.toString(),
{
headers: ajax.get('headers'),
method: 'POST',
data: data
});
This ajax call is triggered by a button click event, which I emulate through my integration test suite.
In light of these observations, I seek guidance on how to conduct further testing and address the discrepancy preventing the requests from flowing through Mirage effectively.