From: Jeremy Davis Date: Wed, 30 Aug 2023 09:46:45 +0000 (+0200) Subject: SONAR-20254 Migrate ResetPassword test to RTL X-Git-Tag: 10.2.0.77647~29 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7696840922861e564d32a79803799dd06eff73f6;p=sonarqube.git SONAR-20254 Migrate ResetPassword test to RTL --- diff --git a/server/sonar-web/src/main/js/app/components/__tests__/ResetPassword-test.tsx b/server/sonar-web/src/main/js/app/components/__tests__/ResetPassword-test.tsx index fb844d95584..e11bea21558 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/ResetPassword-test.tsx +++ b/server/sonar-web/src/main/js/app/components/__tests__/ResetPassword-test.tsx @@ -17,16 +17,21 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { shallow } from 'enzyme'; +import userEvent from '@testing-library/user-event'; import * as React from 'react'; -import ResetPasswordForm from '../../../components/common/ResetPasswordForm'; import { mockLoggedInUser } from '../../../helpers/testMocks'; +import { renderComponent } from '../../../helpers/testReactTestingUtils'; +import { byLabelText, byRole } from '../../../helpers/testSelector'; import { ResetPassword, ResetPasswordProps } from '../ResetPassword'; jest.mock('../../../helpers/system', () => ({ getBaseUrl: jest.fn().mockReturnValue('/context'), })); +jest.mock('../../../api/users', () => ({ + changePassword: jest.fn().mockResolvedValue(true), +})); + const originalLocation = window.location; beforeAll(() => { @@ -47,22 +52,31 @@ afterAll(() => { }); }); -it('should render correctly', () => { - expect(shallowRender()).toMatchSnapshot(); -}); +/* + * Note: the form itself is also used in the context of the account page + * and is tested there as well (i.e. Account-it.tsx) + */ +it('should navigate to the homepage after submission', async () => { + const user = userEvent.setup(); + renderResetPassword(); -it('should navigate to the homepage after submission', () => { - const wrapper = shallowRender(); - const form = wrapper.find(ResetPasswordForm); - const { onPasswordChange } = form.props(); + // Make password strong + await user.type(ui.oldPasswordInput.get(), '1234'); + await user.type(ui.passwordInput.get(), 'strong'); + await user.type(ui.passwordConfirmationInput.get(), 'strong'); - if (onPasswordChange) { - onPasswordChange(); - } + await user.click(ui.submitButton.get()); expect(window.location.href).toBe('/context/'); }); -function shallowRender(props: Partial = {}) { - return shallow(); +function renderResetPassword(props: Partial = {}) { + return renderComponent(); } + +const ui = { + oldPasswordInput: byLabelText(/my_profile\.password\.old/), + passwordInput: byLabelText(/my_profile\.password\.new/), + passwordConfirmationInput: byLabelText(/my_profile\.password\.confirm/), + submitButton: byRole('button', { name: 'update_verb' }), +}; diff --git a/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/ResetPassword-test.tsx.snap b/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/ResetPassword-test.tsx.snap deleted file mode 100644 index 6c22619eecc..00000000000 --- a/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/ResetPassword-test.tsx.snap +++ /dev/null @@ -1,52 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly 1`] = ` -
- -
-

- my_account.reset_password -

-

- my_account.reset_password.explain -

-
-

- my_profile.password.title -

- -
-
-
-`;