jeudi 14 avril 2022

How to modify/change the ember mirage response in my tests file?

The application uses ember-cli-mirage to mock the data and uses specific endpoint to get the specific response. Mocking data and showing the content to the templates works fine.

Problem: I can't modify the response of this endpoint GET /foos in my test file.

/mirage/config.js

export default function () {
  this.namespace = '/api';

  let foos = {
    foos: [
      {
        id: 1,
        name: 'foo-2',
        description: 'foo description'
      },
    ],
  };

  this.get('/foos', function () {
    return foos;
  });
}

tests/acceptance/foo-test.js

import { module, test } from 'qunit';
import { visit, currentURL, click, find, findAll } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

module('Acceptance | foo', function (hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);

  test('visiting /foo', async function (assert) {
    this.server.get('/foos', () => {
      return new Response(
        200,
        {
          foos: [
            {
              id: 20,
              name: 'foo-3'
            }
          ]
        },
      );
    })

    await visit('/foo');
    assert.equal(currentURL(), '/foo');
  });
});

Question: How to modify the response of this endpoint GET /foos inside my test file? I want my test file to have a different response




Aucun commentaire:

Enregistrer un commentaire