Skip to content

Instantly share code, notes, and snippets.

@aaronmcadam
Created June 2, 2020 16:57
Show Gist options
  • Save aaronmcadam/711609f6c9068c0572e614409314cb2e to your computer and use it in GitHub Desktop.
Save aaronmcadam/711609f6c9068c0572e614409314cb2e to your computer and use it in GitHub Desktop.
import React from 'react';
import {
act,
fireEvent,
render,
screen,
userEvent,
} from '../../../../testUtils';
import { CreateNewKeyForm } from './CreateNewKeyForm';
test('renders a form to create a new key', async () => {
render(<CreateNewKeyForm />);
const keyNameInput = screen.getByLabelText('Key name');
const copyInput = screen.getByLabelText('Copy');
const submitButton = screen.getByRole('button', { name: 'Create key' });
expect(keyNameInput).toBeInTheDocument();
expect(copyInput).toBeInTheDocument();
expect(submitButton).toBeInTheDocument();
});
test('renders error messages when validations fail', async () => {
render(<CreateNewKeyForm />);
const form = screen.getByTestId('create-key-form');
await act(() => {
fireEvent.submit(form);
return Promise.resolve();
});
const keyNameErrorMessage = screen.getByText(
'Please enter a name for the key',
);
expect(keyNameErrorMessage).toBeInTheDocument();
const copyErrorMessage = screen.getByText('Please enter some copy');
expect(copyErrorMessage).toBeInTheDocument();
});
test('shows an error message when the key name already exists', async () => {
render(<CreateNewKeyForm existingKeys={['test_key']} />);
const form = screen.getByTestId('create-key-form');
const keyNameInput = screen.getByLabelText('Key name');
const copyInput = screen.getByLabelText('Copy');
await userEvent.type(keyNameInput, 'test_key');
await userEvent.type(copyInput, 'test copy');
await act(() => {
fireEvent.submit(form);
return Promise.resolve();
});
const keyNameErrorMessage = screen.getByText('This key name already exists');
expect(keyNameErrorMessage).toBeInTheDocument();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment