From 43512a469b81984b5fdfae34b87d034914ba8ad8 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 4 Apr 2022 15:21:34 -0400 Subject: [PATCH] Add tests for Registration component --- .../__tests__/registration.test.js | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 app/soapbox/features/verification/__tests__/registration.test.js diff --git a/app/soapbox/features/verification/__tests__/registration.test.js b/app/soapbox/features/verification/__tests__/registration.test.js new file mode 100644 index 000000000..ff5da7e97 --- /dev/null +++ b/app/soapbox/features/verification/__tests__/registration.test.js @@ -0,0 +1,67 @@ +import React from 'react'; + +import { __stub } from 'soapbox/api'; + +import { fireEvent, render, screen, waitFor } from '../../../jest/test-helpers'; +import Registration from '../registration'; + +describe('', () => { + it('renders', () => { + render(); + + expect(screen.getByRole('heading')).toHaveTextContent(/register your account/i); + }); + + describe('with valid data', () => { + beforeEach(() => { + __stub(mock => { + mock.onPost('/api/v1/pepe/accounts').reply(200, {}); + mock.onPost('/api/v1/apps').reply(200, {}); + mock.onPost('/oauth/token').reply(200, {}); + mock.onPost('/api/v1/accounts/verify_credentials').reply(200, {}); + mock.onGet('/api/v1/instance').reply(200, {}); + }); + }); + + it('handles successful submission', async() => { + render(); + + await waitFor(() => { + fireEvent.submit(screen.getByTestId('button'), { preventDefault: () => {} }); + }); + + expect(screen.getByTestId('toast')).toHaveTextContent(/welcome to/i); + expect(screen.queryAllByRole('heading')).toHaveLength(0); + }); + }); + + describe('with invalid data', () => { + it('handles 422 errors', async() => { + __stub(mock => { + mock.onPost('/api/v1/pepe/accounts').reply(422, {}); + }); + + render(); + + await waitFor(() => { + fireEvent.submit(screen.getByTestId('button'), { preventDefault: () => {} }); + }); + + expect(screen.getByTestId('toast')).toHaveTextContent(/this username has already been taken/i); + }); + + it('handles generic errors', async() => { + __stub(mock => { + mock.onPost('/api/v1/pepe/accounts').reply(500, {}); + }); + + render(); + + await waitFor(() => { + fireEvent.submit(screen.getByTestId('button'), { preventDefault: () => {} }); + }); + + expect(screen.getByTestId('toast')).toHaveTextContent(/failed to register your account/i); + }); + }); +});