test('<FormSection>', () => { const { debug, getByTestId, queryByTestId, container, getByText } = render( <FormSection /> ); expect(getByTestId('page-title').textContent).toBe('New Form'); expect(queryByTestId('form')).toBeTruthy(); debug(); expect(container.firstChild).toMatchSnapshot(); });
describe('Modal', () => { it('should open and close modal', () => { const { container, getByText, queryByTestId, getByTestId } = render(<Modal />); const open = getByText('Open'); expect(queryByTestId('modal')).not.toBeInTheDocument(); fireEvent.click(open); expect(getByTestId('modal')).toBeInTheDocument(); expect(container).toMatchSnapshot(); }) })
it('does not render an error with a valid email', () => { const { queryByTestId, getByLabelText, getByText } = render(<SignInView />) const emailField = getByLabelText('Email') const submitButton = getByText('Submit') fireEvent.focus(emailField) fireEvent.change(emailField, { target: { value: 'test@test.com' } }) fireEvent.blur(emailField) expect(submitButton).toHaveAttribute('disabled') expect(queryByTestId('email-error')).toBeNull() })
test('<FormSection>', () => { const { debug, getByTestId, queryByTestId, container, getByText } = render( <Form submitForm={onSubmit} /> ); expect(queryByTestId('form')).toBeTruthy(); fireEvent.click(getByText('Submit')); expect(onSubmit).toHaveBeenCalledTimes(1); });
it('renders an error when authentication fails', async () => { const { getByText, getByTestId, queryByTestId, getByLabelText } = render( <SignInView />, ) const emailField = getByLabelText('Email') const passwordField = getByLabelText('Password') const submitButton = getByText('Submit') expect(submitButton).toHaveAttribute('disabled') fireEvent.focus(emailField) fireEvent.change(emailField, { target: { value: 'test@fail.com' } }) fireEvent.blur(emailField) fireEvent.focus(passwordField) fireEvent.change(passwordField, { target: { value: '1234567890' } }) fireEvent.blur(passwordField) expect(submitButton).not.toHaveAttribute('disabled') fireEvent.click(submitButton) await waitForElement(() => getByTestId('form-error')) expect(queryByTestId('form-message')).toBeNull() expect(getByTestId('form-error').textContent).toBe( 'There was a login failure.', ) })
expect(queryByTestId('data-item-product-C')).not.toBeInTheDocument() }) test('Cart has 1 item', () => { expect.assertions(2) expect(container.querySelector('.cart-wrapper')).toMatchSnapshot() expect(queryByTestId('data-item-product-C')).toBeInTheDocument() }) test('Cart has 2 item', () => { fireEvent.click(getByTestId('product-F')) expect(container.querySelector('.cart-wrapper')).toMatchSnapshot() expect(queryByTestId('data-item-product-F')).toBeInTheDocument() }) test('Cart lost 1 item', () => { fireEvent.click(getByTestId('data-item-product-F').querySelector('[data-subtract]')) expect(container.querySelector('.cart-wrapper')).toMatchSnapshot() expect(queryByTestId('data-item-product-F')).not.toBeInTheDocument() }) test('Cart is empty', (done) => {
fireEvent.click(getByTestId('image-1')); fireEvent.click(getByTestId('selected-image')); expect(queryByTestId('image-modal')).toBeNull(); });
it('does not render an error with a valid password', () => { const { queryByTestId, getByLabelText, getByText } = render(<SignInView />) const passwordField = getByLabelText('Password') const submitButton = getByText('Submit') fireEvent.focus(passwordField) fireEvent.change(passwordField, { target: { value: '1234123412341234' } }) fireEvent.blur(passwordField) expect(submitButton).toHaveAttribute('disabled') expect(queryByTestId('password-error')).toBeNull() })
it('renders an message when authentication succeeds', async () => { const { getByText, getByTestId, queryByTestId, getByLabelText } = render( <SignInView />, ) const emailField = getByLabelText('Email') const passwordField = getByLabelText('Password') const submitButton = getByText('Submit') expect(submitButton).toHaveAttribute('disabled') fireEvent.focus(emailField) fireEvent.change(emailField, { target: { value: 'test@succeed.com' } }) fireEvent.blur(emailField) fireEvent.focus(passwordField) fireEvent.change(passwordField, { target: { value: '1234567890' } }) fireEvent.blur(passwordField) expect(submitButton).not.toHaveAttribute('disabled') fireEvent.click(submitButton) await waitForElement(() => getByTestId('form-message')) expect(queryByTestId('form-error')).toBeNull() expect(getByTestId('form-message').textContent).toBe('You are logged in!') })