Skip to content

Commit

Permalink
test(ui): Add examples of overriding default contexts (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed May 22, 2024
1 parent 77049e7 commit 5932b5a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/docs/frontend/using-rtl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ As a part of this goal, we avoid testing implementation details so refactors (ch

We are generally in favor of Use Case Coverage over Code Coverage.

## Use our built-in contexts
By default, `render()` has some default contexts setup for you. The organization context is set to `OrganizationFixture()` and the router context has some default params like orgId and projectId. These can be overriden by passing a proptery to the 2nd argument of the `render()` function.

Example of overriding the default organization features and access:
```tsx
import {OrganizationFixture} from 'sentry-fixture/organization';
import {render, screen} from "sentry-test/reactTestingLibrary";

const organization = OrganizationFixture({access: ['org:admin'], features: ['my-feature-flag']});
// useOrganization will now get the above organization
render(<Example />, {organization});
```

Router context can be overriden in the same way.

```tsx
const {organization, router, routerContext} = initializeOrg({
organization: {features: ['global-views', 'open-membership']},
router: {
location: {
pathname: '/organizations/org-slug/issues/',
query: {environment: 'prod'},
},
params: {},
},
});

render(<Example />, {context: routerContext, organization});
await userEvent.click(something);
expect(router.push).toHaveBeenCalledTimes(1);
```

## Querying

- use `getBy...` as much as possible
Expand Down

0 comments on commit 5932b5a

Please sign in to comment.