2
votes

I have default params that are added to the search request from a route. I would like to test these in ember-cli-mirage but am stuck on how to capture the request or requestBody so that I can assert against it.

Was looking for something similar to what I found on this SO post, but need access to the actual request and not the DOM. I am able to access the search params entered by the user (the 'text' param in my example) using currentUrl(), but the default params included in the request sent to the server, but not the url.

Is there a way to capture and assert against the request itself using ember-cli-mirage?

Something like

test('it appends default params to request'), function(assert) {
  let searchUrl = '/my/route/url';

  server.get(searchUrl, (db, request) => {
    assert.equal(request.requestBody, "text=abc&all=true");
  }
});

EDIT

I was able to get the test to pass using Qunit's async helper, like so:

test('it appends default params to athlete request', function(assert) {
  assert.expect(2);
  let done = assert.async();

  server.get('/athletes', (db, request) => {
    let params = request.queryParams;
    assert.equal(params["page"], "1");
    assert.equal(params["per"], "50");
    done();
  });

  server.create('athlete', {first_name: 'John'});
  visit('/athletes');
});

Still getting an error in the console for this test related to the json:api serialization:

normalizeResponse must return a valid JSON API document:
        * meta must be an object

Going to open another question related to this failure elsewhere and link it in the comments.

1

1 Answers

4
votes

The request param passed to your route handlers is the PretenderJS request object, which has some useful keys:

  • request.params, the dynamic segments of your route
  • request.queryParams, deserialized query request params
  • request.requestBody, the text body, You can use JSON.parse(request.requestBody) to turn this into an object.

So, if you wanted to assert against the query params, use request.queryParms.