Skip to content

Commit 865e06b

Browse files
authored
Remove Vuetify from Forgot password page (#6334)
* Remove Vuetify from Forgot password page Signed-off-by: adibmbrk <adibmbrk@gmail.com> * Scope email validation assertions to the email field Signed-off-by: adibmbrk <adibmbrk@gmail.com> * Show email validation error on blur or submit Signed-off-by: adibmbrk <adibmbrk@gmail.com> * Fit message layout forms on small screens Signed-off-by: adibmbrk <adibmbrk@gmail.com> * Announce the forgot password error banner to screen readers Signed-off-by: adibmbrk <adibmbrk@gmail.com> * Use userEvent and createTranslator in the forgot password tests Signed-off-by: adibmbrk <adibmbrk@gmail.com> --------- Signed-off-by: adibmbrk <adibmbrk@gmail.com>
1 parent 91820fd commit 865e06b

3 files changed

Lines changed: 118 additions & 35 deletions

File tree

‎contentcuration/contentcuration/frontend/accounts/components/StudioMessageLayout.vue‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
}
101101
102102
.message-slot-container {
103+
width: 100%;
103104
max-width: 400px;
104105
margin: 24px auto 0;
105106
text-align: center;

‎contentcuration/contentcuration/frontend/accounts/pages/__tests__/forgotPassword.spec.js‎

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
import { render, screen, fireEvent, waitFor } from '@testing-library/vue';
1+
import { render, screen, waitFor } from '@testing-library/vue';
2+
import userEvent from '@testing-library/user-event';
23
import VueRouter from 'vue-router';
34
import ForgotPassword from '../resetPassword/ForgotPassword';
5+
import commonStrings from 'shared/translator';
6+
import { createTranslator } from 'shared/i18n';
7+
8+
const { fieldRequired$ } = commonStrings;
9+
const {
10+
forgotPasswordTitle$,
11+
emailLabel$,
12+
submitButton$,
13+
emailValidationMessage$,
14+
forgotPasswordFailed$,
15+
} = createTranslator(ForgotPassword.name, ForgotPassword.$trs);
16+
17+
const INVALID_EMAIL = 'invalid-email';
18+
const VALID_EMAIL = 'test@test.com';
419

520
const sendPasswordResetLinkMock = jest.fn(() => Promise.resolve());
621

@@ -31,38 +46,66 @@ const renderComponent = () => {
3146
};
3247

3348
describe('ForgotPassword', () => {
49+
let user;
50+
51+
const emailInput = () => screen.getByLabelText(emailLabel$());
52+
const submitButton = () => screen.getByRole('button', { name: submitButton$() });
53+
3454
beforeEach(() => {
3555
jest.clearAllMocks();
56+
user = userEvent.setup();
3657
});
3758

3859
it('should render the forgot password form', () => {
3960
renderComponent();
40-
expect(screen.getByText('Reset your password')).toBeInTheDocument();
41-
expect(screen.getByLabelText('Email')).toBeInTheDocument();
42-
expect(screen.getByRole('button', { name: 'Submit' })).toBeInTheDocument();
61+
expect(screen.getByText(forgotPasswordTitle$())).toBeInTheDocument();
62+
expect(emailInput()).toBeInTheDocument();
63+
expect(submitButton()).toBeInTheDocument();
64+
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
4365
});
4466

4567
it('should not submit form with empty email', async () => {
4668
renderComponent();
47-
await fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
69+
await user.click(submitButton());
70+
await waitFor(() => {
71+
expect(screen.getByTestId('email-field')).toHaveTextContent(fieldRequired$());
72+
});
4873
expect(sendPasswordResetLinkMock).not.toHaveBeenCalled();
4974
});
5075

5176
it('should not submit form with invalid email', async () => {
5277
renderComponent();
53-
await fireEvent.update(screen.getByLabelText(/email/i), 'invalid-email');
54-
await fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
78+
await user.type(emailInput(), INVALID_EMAIL);
79+
await user.click(submitButton());
80+
await waitFor(() => {
81+
expect(screen.getByTestId('email-field')).toHaveTextContent(emailValidationMessage$());
82+
});
5583
expect(sendPasswordResetLinkMock).not.toHaveBeenCalled();
5684
});
5785

86+
it('should not show validation error while typing', async () => {
87+
renderComponent();
88+
await user.type(emailInput(), INVALID_EMAIL);
89+
expect(screen.getByTestId('email-field')).not.toHaveTextContent(emailValidationMessage$());
90+
});
91+
92+
it('should show validation error on blur', async () => {
93+
renderComponent();
94+
await user.type(emailInput(), INVALID_EMAIL);
95+
await user.tab();
96+
await waitFor(() => {
97+
expect(screen.getByTestId('email-field')).toHaveTextContent(emailValidationMessage$());
98+
});
99+
});
100+
58101
it('should submit form with valid email and navigate to success page', async () => {
59102
sendPasswordResetLinkMock.mockResolvedValue({});
60103
const { router } = renderComponent();
61-
await fireEvent.update(screen.getByLabelText(/email/i), 'test@test.com');
62-
await fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
104+
await user.type(emailInput(), VALID_EMAIL);
105+
await user.click(submitButton());
63106
await waitFor(() => {
64107
expect(sendPasswordResetLinkMock).toHaveBeenCalledTimes(1);
65-
expect(sendPasswordResetLinkMock).toHaveBeenCalledWith(expect.anything(), 'test@test.com');
108+
expect(sendPasswordResetLinkMock).toHaveBeenCalledWith(expect.anything(), VALID_EMAIL);
66109
});
67110
await waitFor(() => {
68111
expect(router.currentRoute.name).toBe('PasswordInstructionsSent');
@@ -72,8 +115,8 @@ describe('ForgotPassword', () => {
72115
it('should show error banner when submission fails', async () => {
73116
sendPasswordResetLinkMock.mockRejectedValue(new Error('Failed'));
74117
renderComponent();
75-
await fireEvent.update(screen.getByLabelText(/email/i), 'test@test.com');
76-
await fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
77-
expect(await screen.findByTestId('error-banner')).toBeInTheDocument();
118+
await user.type(emailInput(), VALID_EMAIL);
119+
await user.click(submitButton());
120+
expect(await screen.findByRole('alert')).toHaveTextContent(forgotPasswordFailed$());
78121
});
79122
});

‎contentcuration/contentcuration/frontend/accounts/pages/resetPassword/ForgotPassword.vue‎

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,89 @@
11
<template>
22

3-
<MessageLayout
3+
<StudioMessageLayout
44
:header="$tr('forgotPasswordTitle')"
55
:text="$tr('forgotPasswordPrompt')"
66
>
7-
<VForm
8-
ref="form"
9-
lazy-validation
7+
<form
8+
class="forgot-password-form"
9+
novalidate
1010
@submit.prevent="submit"
1111
>
12-
<Banner
13-
:text="$tr('forgotPasswordFailed')"
14-
:value="error"
12+
<StudioBanner
13+
v-if="error"
14+
role="alert"
1515
error
16-
class="mb-4"
17-
data-testid="error-banner"
18-
/>
19-
<EmailField
16+
class="banner"
17+
>
18+
{{ $tr('forgotPasswordFailed') }}
19+
</StudioBanner>
20+
<StudioEmailField
2021
v-model="email"
2122
autofocus
23+
data-testid="email-field"
24+
:label="$tr('emailLabel')"
25+
:errorMessages="showEmailError && errors.email ? [emailErrorText] : []"
26+
@blur="showEmailError = true"
2227
/>
2328
<KButton
2429
primary
2530
class="w-100"
2631
:text="$tr('submitButton')"
2732
type="submit"
2833
/>
29-
</VForm>
30-
</MessageLayout>
34+
</form>
35+
</StudioMessageLayout>
3136

3237
</template>
3338

3439

3540
<script>
3641
3742
import { mapActions } from 'vuex';
38-
import MessageLayout from '../../components/MessageLayout';
39-
import EmailField from 'shared/views/form/EmailField';
40-
import Banner from 'shared/views/Banner';
43+
import StudioMessageLayout from '../../components/StudioMessageLayout';
44+
import StudioEmailField from '../../components/form/StudioEmailField';
45+
import StudioBanner from 'shared/views/StudioBanner';
46+
import commonStrings from 'shared/translator';
47+
import { generateFormMixin } from 'shared/mixins';
48+
49+
const formMixin = generateFormMixin({
50+
email: {
51+
required: true,
52+
validator: v => Boolean(v && v.trim()) && /.+@.+\..+/.test(v),
53+
},
54+
});
4155
4256
export default {
4357
name: 'ForgotPassword',
4458
components: {
45-
MessageLayout,
46-
EmailField,
47-
Banner,
59+
StudioMessageLayout,
60+
StudioEmailField,
61+
StudioBanner,
4862
},
63+
mixins: [formMixin],
4964
data() {
5065
return {
51-
email: '',
5266
error: false,
67+
showEmailError: false,
5368
};
5469
},
70+
computed: {
71+
emailErrorText() {
72+
if (!this.email || !this.email.trim()) {
73+
/* eslint-disable-next-line kolibri/vue-no-undefined-string-uses */
74+
return commonStrings.$tr('fieldRequired');
75+
}
76+
return this.$tr('emailValidationMessage');
77+
},
78+
},
5579
methods: {
5680
...mapActions('account', ['sendPasswordResetLink']),
5781
submit() {
5882
this.error = false;
59-
if (this.$refs.form.validate()) {
60-
this.sendPasswordResetLink(this.email)
83+
this.showEmailError = true;
84+
const formData = this.clean();
85+
if (this.validate(formData)) {
86+
this.sendPasswordResetLink(formData.email)
6187
.then(() => {
6288
this.$router
6389
.push({
@@ -77,6 +103,8 @@
77103
'Please enter your email address to receive instructions for resetting your password',
78104
submitButton: 'Submit',
79105
forgotPasswordFailed: 'Failed to send a password reset link. Please try again.',
106+
emailLabel: 'Email',
107+
emailValidationMessage: 'Please enter a valid email',
80108
},
81109
};
82110
@@ -85,6 +113,17 @@
85113

86114
<style lang="scss" scoped>
87115
116+
.forgot-password-form {
117+
width: 400px;
118+
max-width: 100%;
119+
text-align: left;
120+
}
121+
122+
.banner {
123+
width: 100%;
124+
margin-bottom: 16px;
125+
}
126+
88127
.w-100 {
89128
width: 100%;
90129
}

0 commit comments

Comments
 (0)