Skip to content

Commit

Permalink
Remove loop
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jan 16, 2024
1 parent ced7dae commit 862acf8
Showing 1 changed file with 87 additions and 95 deletions.
182 changes: 87 additions & 95 deletions x-pack/plugins/cases/public/components/description/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,113 +36,111 @@ describe('Description', () => {
appMockRender = createAppMockRenderer();
});

for (let index = 0; index < 100; index++) {
it('renders description correctly', async () => {
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);
it('renders description correctly', async () => {
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);

expect(await screen.findByTestId('description')).toBeInTheDocument();
expect(await screen.findByText('Security banana Issue')).toBeInTheDocument();
});
expect(await screen.findByTestId('description')).toBeInTheDocument();
expect(await screen.findByText('Security banana Issue')).toBeInTheDocument();
});

it('hides and shows the description correctly when collapse button clicked', async () => {
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);
it('hides and shows the description correctly when collapse button clicked', async () => {
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);

userEvent.click(await screen.findByTestId('description-collapse-icon'));
userEvent.click(await screen.findByTestId('description-collapse-icon'));

await waitFor(() => {
expect(screen.queryByText('Security banana Issue')).not.toBeInTheDocument();
});
await waitFor(() => {
expect(screen.queryByText('Security banana Issue')).not.toBeInTheDocument();
});

userEvent.click(await screen.findByTestId('description-collapse-icon'));
userEvent.click(await screen.findByTestId('description-collapse-icon'));

expect(await screen.findByText('Security banana Issue')).toBeInTheDocument();
});
expect(await screen.findByText('Security banana Issue')).toBeInTheDocument();
});

it('shows textarea on edit click', async () => {
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);
it('shows textarea on edit click', async () => {
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);

userEvent.click(await screen.findByTestId('description-edit-icon'));
userEvent.click(await screen.findByTestId('description-edit-icon'));

expect(await screen.findByTestId('euiMarkdownEditorTextArea')).toBeInTheDocument();
});
expect(await screen.findByTestId('euiMarkdownEditorTextArea')).toBeInTheDocument();
});

it('edits the description correctly when saved', async () => {
const editedDescription = 'New updated description';
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);
it('edits the description correctly when saved', async () => {
const editedDescription = 'New updated description';
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);

userEvent.click(await screen.findByTestId('description-edit-icon'));
userEvent.click(await screen.findByTestId('description-edit-icon'));

userEvent.clear(await screen.findByTestId('euiMarkdownEditorTextArea'));
userEvent.paste(await screen.findByTestId('euiMarkdownEditorTextArea'), editedDescription);
userEvent.clear(await screen.findByTestId('euiMarkdownEditorTextArea'));
userEvent.paste(await screen.findByTestId('euiMarkdownEditorTextArea'), editedDescription);

userEvent.click(await screen.findByTestId('editable-save-markdown'));
userEvent.click(await screen.findByTestId('editable-save-markdown'));

await waitFor(() => {
expect(onUpdateField).toHaveBeenCalledWith({
key: 'description',
value: editedDescription,
});
await waitFor(() => {
expect(onUpdateField).toHaveBeenCalledWith({
key: 'description',
value: editedDescription,
});
});
});

it('keeps the old description correctly when canceled', async () => {
const editedDescription = 'New updated description';
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);

userEvent.click(await screen.findByTestId('description-edit-icon'));
it('keeps the old description correctly when canceled', async () => {
const editedDescription = 'New updated description';
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);

userEvent.clear(await screen.findByTestId('euiMarkdownEditorTextArea'));
userEvent.paste(await screen.findByTestId('euiMarkdownEditorTextArea'), editedDescription);
userEvent.click(await screen.findByTestId('description-edit-icon'));

expect(await screen.findByText(editedDescription)).toBeInTheDocument();
userEvent.clear(await screen.findByTestId('euiMarkdownEditorTextArea'));
userEvent.paste(await screen.findByTestId('euiMarkdownEditorTextArea'), editedDescription);

userEvent.click(await screen.findByTestId('editable-cancel-markdown'));
expect(await screen.findByText(editedDescription)).toBeInTheDocument();

await waitFor(() => {
expect(onUpdateField).not.toHaveBeenCalled();
});
userEvent.click(await screen.findByTestId('editable-cancel-markdown'));

expect(await screen.findByText('Security banana Issue')).toBeInTheDocument();
await waitFor(() => {
expect(onUpdateField).not.toHaveBeenCalled();
});

it('shows an error when description is too long', async () => {
const longDescription = 'a'.repeat(MAX_DESCRIPTION_LENGTH + 1);
expect(await screen.findByText('Security banana Issue')).toBeInTheDocument();
});

appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);
it('shows an error when description is too long', async () => {
const longDescription = 'a'.repeat(MAX_DESCRIPTION_LENGTH + 1);

userEvent.click(await screen.findByTestId('description-edit-icon'));
appMockRender.render(<Description {...defaultProps} onUpdateField={onUpdateField} />);

userEvent.clear(await screen.findByTestId('euiMarkdownEditorTextArea'));
userEvent.paste(await screen.findByTestId('euiMarkdownEditorTextArea'), longDescription);
userEvent.click(await screen.findByTestId('description-edit-icon'));

expect(
await screen.findByText(
'The length of the description is too long. The maximum length is 30000 characters.'
)
).toBeInTheDocument();
userEvent.clear(await screen.findByTestId('euiMarkdownEditorTextArea'));
userEvent.paste(await screen.findByTestId('euiMarkdownEditorTextArea'), longDescription);

expect(await screen.findByTestId('editable-save-markdown')).toHaveAttribute('disabled');
});
expect(
await screen.findByText(
'The length of the description is too long. The maximum length is 30000 characters.'
)
).toBeInTheDocument();

it('should hide the edit button when the user does not have update permissions', async () => {
appMockRender.render(
<TestProviders permissions={noUpdateCasesPermissions()}>
<Description {...defaultProps} onUpdateField={onUpdateField} />
</TestProviders>
);
expect(await screen.findByTestId('editable-save-markdown')).toHaveAttribute('disabled');
});

expect(await screen.findByText('Security banana Issue')).toBeInTheDocument();
expect(screen.queryByTestId('description-edit-icon')).not.toBeInTheDocument();
});
it('should hide the edit button when the user does not have update permissions', async () => {
appMockRender.render(
<TestProviders permissions={noUpdateCasesPermissions()}>
<Description {...defaultProps} onUpdateField={onUpdateField} />
</TestProviders>
);

it('should display description when case is loading', async () => {
appMockRender.render(
<Description {...defaultProps} onUpdateField={onUpdateField} isLoadingDescription={true} />
);
expect(await screen.findByText('Security banana Issue')).toBeInTheDocument();
expect(screen.queryByTestId('description-edit-icon')).not.toBeInTheDocument();
});

expect(await screen.findByTestId('description')).toBeInTheDocument();
});
}
it('should display description when case is loading', async () => {
appMockRender.render(
<Description {...defaultProps} onUpdateField={onUpdateField} isLoadingDescription={true} />
);

expect(await screen.findByTestId('description')).toBeInTheDocument();
});

describe('draft message', () => {
const draftStorageKey = `cases.testAppId.basic-case-id.description.markdownEditor`;
Expand All @@ -151,29 +149,23 @@ describe('Description', () => {
sessionStorage.setItem(draftStorageKey, 'value set in storage');
});

for (let index = 0; index < 100; index++) {
it('should not show unsaved draft message when loading', async () => {
appMockRender.render(
<Description
{...defaultProps}
onUpdateField={onUpdateField}
isLoadingDescription={true}
/>
);

expect(screen.queryByTestId('description-unsaved-draft')).not.toBeInTheDocument();
});
it('should not show unsaved draft message when loading', async () => {
appMockRender.render(
<Description {...defaultProps} onUpdateField={onUpdateField} isLoadingDescription={true} />
);

it('should not show unsaved draft message when description and storage value are same', async () => {
const props = {
...defaultProps,
caseData: { ...defaultProps.caseData, description: 'value set in storage' },
};
expect(screen.queryByTestId('description-unsaved-draft')).not.toBeInTheDocument();
});

appMockRender.render(<Description {...props} onUpdateField={onUpdateField} />);
it('should not show unsaved draft message when description and storage value are same', async () => {
const props = {
...defaultProps,
caseData: { ...defaultProps.caseData, description: 'value set in storage' },
};

expect(screen.queryByTestId('description-unsaved-draft')).not.toBeInTheDocument();
});
}
appMockRender.render(<Description {...props} onUpdateField={onUpdateField} />);

expect(screen.queryByTestId('description-unsaved-draft')).not.toBeInTheDocument();
});
});
});

0 comments on commit 862acf8

Please sign in to comment.