Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Conversation

@sigfriedCub1990
Copy link
Member

@sigfriedCub1990 sigfriedCub1990 commented Dec 22, 2023

🎩 What?

Add defaultResponses option to wrapito-vitest.

🤔 Why?

There are cases when you need a set of responses to be common for all components, e.g Feature Flags(FF).
Historically, the way to do this is by defining an extension and then using it with every call to wrap.

// setupTests.js
import { render } from '@testing-library/react';
import { configure } from 'wrapito-vitest';

configure({
    mount: render,
    extend: {
        withDefaultResponses: ({ addResponses }, [otherResponses = []]) => {
          addResponses([
            {
              path: '/feature-flags',
              responseBody: {
                next_page: null,
                results: ['flag-1', 'flag-2'],
              },
            },
          ])
        },
    }
})

// app.test.js
import { wrap } from 'wrapito-vitest';
import { screen } from '@testing-library/react';

import App from './App'

test('should render app', () => {
    wrap(App).withDefaultResponses().mount();

    expect(await screen.findByText('Hello, world!')).toBeVisible();
})

We can do better if we could define a set of default responses that every component should use.

// setupTests.js
import { render } from '@testing-library/react';
import { configure } from 'wrapito-vitest';

configure({
    mount: render,
    defaultResponses: [
        {
          path: '/feature-flags',
          responseBody: {
            next_page: null,
            results: ['flag-1', 'flag-2'],
          },
        },
    ]
})

// app.test.js
import { wrap } from 'wrapito-vitest';
import { screen } from '@testing-library/react';

import App from './App'

test('should render app', () => {
    wrap(App).mount(); // Notice the absence of the extension

    expect(await screen.findByText('Hello, world!')).toBeVisible();
})

🧪 How has this been tested? / 💥 How will I know if this breaks?

Unit tested

Closes #10

@sigfriedCub1990 sigfriedCub1990 self-assigned this Dec 22, 2023
@sigfriedCub1990 sigfriedCub1990 requested a review from a team December 22, 2023 20:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to define default requests for every test

2 participants