Skip to content
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

Add globalResponses configuration #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

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 join this conversation on GitHub. Already have an account? Sign in to comment
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