|
@@ -1,5 +1,5 @@
|
|
|
import React from 'react';
|
|
|
-import { screen, within } from '@testing-library/react';
|
|
|
+import { screen, waitFor, within } from '@testing-library/react';
|
|
|
import { render } from 'lib/testHelpers';
|
|
|
import CustomParams, {
|
|
|
CustomParamsProps,
|
|
@@ -8,6 +8,30 @@ import { FormProvider, useForm } from 'react-hook-form';
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
import { TOPIC_CUSTOM_PARAMS } from 'lib/constants';
|
|
|
|
|
|
+const selectOption = async (listbox: HTMLElement, option: string) => {
|
|
|
+ await waitFor(() => userEvent.click(listbox));
|
|
|
+ await waitFor(() => userEvent.click(screen.getByText(option)));
|
|
|
+};
|
|
|
+
|
|
|
+const expectOptionIsSelected = (listbox: HTMLElement, option: string) => {
|
|
|
+ const selectedOption = within(listbox).getAllByRole('option');
|
|
|
+ expect(selectedOption.length).toEqual(1);
|
|
|
+ expect(selectedOption[0]).toHaveTextContent(option);
|
|
|
+};
|
|
|
+
|
|
|
+const expectOptionIsDisabled = async (
|
|
|
+ listbox: HTMLElement,
|
|
|
+ option: string,
|
|
|
+ disabled: boolean
|
|
|
+) => {
|
|
|
+ await waitFor(() => userEvent.click(listbox));
|
|
|
+ const selectedOption = within(listbox).getAllByText(option);
|
|
|
+ expect(selectedOption[1]).toHaveStyleRule(
|
|
|
+ 'cursor',
|
|
|
+ disabled ? 'not-allowed' : 'pointer'
|
|
|
+ );
|
|
|
+};
|
|
|
+
|
|
|
describe('CustomParams', () => {
|
|
|
const setupComponent = (props: CustomParamsProps) => {
|
|
|
const Wrapper: React.FC = ({ children }) => {
|
|
@@ -33,9 +57,9 @@ describe('CustomParams', () => {
|
|
|
});
|
|
|
|
|
|
describe('works with user inputs correctly', () => {
|
|
|
- it('button click creates custom param fieldset', () => {
|
|
|
+ it('button click creates custom param fieldset', async () => {
|
|
|
const addParamButton = screen.getByRole('button');
|
|
|
- userEvent.click(addParamButton);
|
|
|
+ await waitFor(() => userEvent.click(addParamButton));
|
|
|
|
|
|
const listbox = screen.getByRole('listbox');
|
|
|
expect(listbox).toBeInTheDocument();
|
|
@@ -44,51 +68,39 @@ describe('CustomParams', () => {
|
|
|
expect(textbox).toBeInTheDocument();
|
|
|
});
|
|
|
|
|
|
- it('can select option', () => {
|
|
|
+ it('can select option', async () => {
|
|
|
const addParamButton = screen.getByRole('button');
|
|
|
- userEvent.click(addParamButton);
|
|
|
+ await waitFor(() => userEvent.click(addParamButton));
|
|
|
|
|
|
const listbox = screen.getByRole('listbox');
|
|
|
|
|
|
- userEvent.selectOptions(listbox, ['compression.type']);
|
|
|
-
|
|
|
- const option = screen.getByRole('option', {
|
|
|
- selected: true,
|
|
|
- });
|
|
|
- expect(option).toHaveValue('compression.type');
|
|
|
- expect(option).toBeDisabled();
|
|
|
+ await selectOption(listbox, 'compression.type');
|
|
|
+ expectOptionIsSelected(listbox, 'compression.type');
|
|
|
+ expectOptionIsDisabled(listbox, 'compression.type', true);
|
|
|
|
|
|
const textbox = screen.getByRole('textbox');
|
|
|
expect(textbox).toHaveValue(TOPIC_CUSTOM_PARAMS['compression.type']);
|
|
|
});
|
|
|
|
|
|
- it('when selected option changes disabled options update correctly', () => {
|
|
|
+ it('when selected option changes disabled options update correctly', async () => {
|
|
|
const addParamButton = screen.getByRole('button');
|
|
|
- userEvent.click(addParamButton);
|
|
|
+ await waitFor(() => userEvent.click(addParamButton));
|
|
|
|
|
|
const listbox = screen.getByRole('listbox');
|
|
|
|
|
|
- userEvent.selectOptions(listbox, ['compression.type']);
|
|
|
-
|
|
|
- const option = screen.getByRole('option', {
|
|
|
- name: 'compression.type',
|
|
|
- });
|
|
|
- expect(option).toBeDisabled();
|
|
|
+ await selectOption(listbox, 'compression.type');
|
|
|
+ expectOptionIsDisabled(listbox, 'compression.type', true);
|
|
|
|
|
|
- userEvent.selectOptions(listbox, ['delete.retention.ms']);
|
|
|
- const newOption = screen.getByRole('option', {
|
|
|
- name: 'delete.retention.ms',
|
|
|
- });
|
|
|
- expect(newOption).toBeDisabled();
|
|
|
-
|
|
|
- expect(option).toBeEnabled();
|
|
|
+ await selectOption(listbox, 'delete.retention.ms');
|
|
|
+ expectOptionIsDisabled(listbox, 'delete.retention.ms', true);
|
|
|
+ expectOptionIsDisabled(listbox, 'compression.type', false);
|
|
|
});
|
|
|
|
|
|
- it('multiple button clicks create multiple fieldsets', () => {
|
|
|
+ it('multiple button clicks create multiple fieldsets', async () => {
|
|
|
const addParamButton = screen.getByRole('button');
|
|
|
- userEvent.click(addParamButton);
|
|
|
- userEvent.click(addParamButton);
|
|
|
- userEvent.click(addParamButton);
|
|
|
+ await waitFor(() => userEvent.click(addParamButton));
|
|
|
+ await waitFor(() => userEvent.click(addParamButton));
|
|
|
+ await waitFor(() => userEvent.click(addParamButton));
|
|
|
|
|
|
const listboxes = screen.getAllByRole('listbox');
|
|
|
expect(listboxes.length).toBe(3);
|
|
@@ -97,7 +109,7 @@ describe('CustomParams', () => {
|
|
|
expect(textboxes.length).toBe(3);
|
|
|
});
|
|
|
|
|
|
- it("can't select already selected option", () => {
|
|
|
+ it("can't select already selected option", async () => {
|
|
|
const addParamButton = screen.getByRole('button');
|
|
|
userEvent.click(addParamButton);
|
|
|
userEvent.click(addParamButton);
|
|
@@ -105,18 +117,11 @@ describe('CustomParams', () => {
|
|
|
const listboxes = screen.getAllByRole('listbox');
|
|
|
|
|
|
const firstListbox = listboxes[0];
|
|
|
- userEvent.selectOptions(firstListbox, ['compression.type']);
|
|
|
-
|
|
|
- const firstListboxOption = within(firstListbox).getByRole('option', {
|
|
|
- selected: true,
|
|
|
- });
|
|
|
- expect(firstListboxOption).toBeDisabled();
|
|
|
+ await selectOption(firstListbox, 'compression.type');
|
|
|
+ expectOptionIsDisabled(firstListbox, 'compression.type', true);
|
|
|
|
|
|
const secondListbox = listboxes[1];
|
|
|
- const secondListboxOption = within(secondListbox).getByRole('option', {
|
|
|
- name: 'compression.type',
|
|
|
- });
|
|
|
- expect(secondListboxOption).toBeDisabled();
|
|
|
+ expectOptionIsDisabled(secondListbox, 'compression.type', true);
|
|
|
});
|
|
|
|
|
|
it('when fieldset with selected custom property type is deleted disabled options update correctly', async () => {
|
|
@@ -128,26 +133,16 @@ describe('CustomParams', () => {
|
|
|
const listboxes = screen.getAllByRole('listbox');
|
|
|
|
|
|
const firstListbox = listboxes[0];
|
|
|
- userEvent.selectOptions(firstListbox, ['compression.type']);
|
|
|
-
|
|
|
- const firstListboxOption = within(firstListbox).getByRole('option', {
|
|
|
- selected: true,
|
|
|
- });
|
|
|
- expect(firstListboxOption).toBeDisabled();
|
|
|
+ await selectOption(firstListbox, 'compression.type');
|
|
|
+ expectOptionIsDisabled(firstListbox, 'compression.type', true);
|
|
|
|
|
|
const secondListbox = listboxes[1];
|
|
|
- userEvent.selectOptions(secondListbox, ['delete.retention.ms']);
|
|
|
- const secondListboxOption = within(secondListbox).getByRole('option', {
|
|
|
- selected: true,
|
|
|
- });
|
|
|
- expect(secondListboxOption).toBeDisabled();
|
|
|
+ await selectOption(secondListbox, 'delete.retention.ms');
|
|
|
+ expectOptionIsDisabled(secondListbox, 'delete.retention.ms', true);
|
|
|
|
|
|
const thirdListbox = listboxes[2];
|
|
|
- userEvent.selectOptions(thirdListbox, ['file.delete.delay.ms']);
|
|
|
- const thirdListboxOption = within(thirdListbox).getByRole('option', {
|
|
|
- selected: true,
|
|
|
- });
|
|
|
- expect(thirdListboxOption).toBeDisabled();
|
|
|
+ await selectOption(thirdListbox, 'file.delete.delay.ms');
|
|
|
+ expectOptionIsDisabled(thirdListbox, 'file.delete.delay.ms', true);
|
|
|
|
|
|
const deleteSecondFieldsetButton = screen.getByTitle(
|
|
|
'Delete customParam field 1'
|
|
@@ -155,17 +150,8 @@ describe('CustomParams', () => {
|
|
|
userEvent.click(deleteSecondFieldsetButton);
|
|
|
expect(secondListbox).not.toBeInTheDocument();
|
|
|
|
|
|
- expect(
|
|
|
- within(firstListbox).getByRole('option', {
|
|
|
- name: 'delete.retention.ms',
|
|
|
- })
|
|
|
- ).toBeEnabled();
|
|
|
-
|
|
|
- expect(
|
|
|
- within(thirdListbox).getByRole('option', {
|
|
|
- name: 'delete.retention.ms',
|
|
|
- })
|
|
|
- ).toBeEnabled();
|
|
|
+ expectOptionIsDisabled(firstListbox, 'delete.retention.ms', false);
|
|
|
+ expectOptionIsDisabled(thirdListbox, 'delete.retention.ms', false);
|
|
|
});
|
|
|
});
|
|
|
});
|