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 custom queries to within #6737

Merged
merged 5 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion src/test/rtl/custom_render.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { render, screen } from './custom_render';
import { render, screen, within } from './custom_render';

describe('render returns the ByTestSubject custom queries', () => {
test('queryByTestSubject', () => {
Expand Down Expand Up @@ -84,3 +84,77 @@ describe('screen has the ByTestSubject custom queries', () => {
await expect(screen.findAllByTestSubject('test')).resolves.toHaveLength(1);
});
});

describe('within has the ByTestSubject custom queries', () => {
test('queryByTestSubject', () => {
render(
<div data-testid="outer">
<div data-test-subj="inner" />
</div>
);

expect(
within(screen.getByTestId('outer')).queryByTestSubject('inner')
).not.toBeNull();
});

test('queryAllByTestSubject', () => {
render(
<div data-testid="outer">
<div data-test-subj="inner" />
</div>
);

expect(
within(screen.getByTestId('outer')).queryAllByTestSubject('inner')
).toHaveLength(1);
});

test('getByTestSubject', () => {
render(
<div data-testid="outer">
<div data-test-subj="inner" />
</div>
);

expect(
within(screen.getByTestId('outer')).getByTestSubject('inner')
).toBeTruthy();
});

test('getAllByTestSubject', () => {
render(
<div data-testid="outer">
<div data-test-subj="inner" />
</div>
);

expect(
within(screen.getByTestId('outer')).getAllByTestSubject('inner')
).toHaveLength(1);
});

test('findByTestSubject', async () => {
render(
<div data-testid="outer">
<div data-test-subj="inner" />
</div>
);

await expect(
within(screen.getByTestId('outer')).findByTestSubject('inner')
).resolves.toBeTruthy();
});

test('findAllByTestSubject', async () => {
render(
<div data-testid="outer">
<div data-test-subj="inner" />
</div>
);

await expect(
within(screen.getByTestId('outer')).findAllByTestSubject('inner')
).resolves.toHaveLength(1);
});
});
13 changes: 13 additions & 0 deletions src/test/rtl/custom_render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@ const customScreen: Screen<typeof queries & typeof dataTestSubjQueries> = {
};

export { customScreen as screen };

/**
* Custom within util with EUI query helpers
*
* @see https://testing-library.com/docs/dom-testing-library/api-within/
*/
const customWithin = (element: HTMLElement) =>
within<typeof queries & typeof dataTestSubjQueries>(element, {
...queries,
...dataTestSubjQueries,
});

export { customWithin as within };
2 changes: 1 addition & 1 deletion src/test/rtl/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './component_helpers';
export { queryByTestSubject, queryAllByTestSubject, getByTestSubject, getAllByTestSubject, findAllByTestSubject, findByTestSubject, } from './data_test_subj_queries';
export { render, screen } from './custom_render';
export { render, screen, within } from './custom_render';
2 changes: 1 addition & 1 deletion src/test/rtl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export {
findAllByTestSubject,
findByTestSubject,
} from './data_test_subj_queries';
export { render, screen } from './custom_render';
export { render, screen, within } from './custom_render';
1 change: 1 addition & 0 deletions upcoming_changelogs/6737.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added React Testing Library `*ByTestSubject` custom commands to `within()`. RTL utilities can be imported from `@elastic/eui/lib/test/rtl`.