Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
}

.message-slot-container {
width: 100%;
max-width: 400px;
margin: 24px auto 0;
text-align: center;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/vue';
import { render, screen, waitFor } from '@testing-library/vue';
import userEvent from '@testing-library/user-event';
import VueRouter from 'vue-router';
import ForgotPassword from '../resetPassword/ForgotPassword';
import commonStrings from 'shared/translator';
import { createTranslator } from 'shared/i18n';

const { fieldRequired$ } = commonStrings;
const {
forgotPasswordTitle$,
emailLabel$,
submitButton$,
emailValidationMessage$,
forgotPasswordFailed$,
} = createTranslator(ForgotPassword.name, ForgotPassword.$trs);

const INVALID_EMAIL = 'invalid-email';
const VALID_EMAIL = 'test@test.com';

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

Expand Down Expand Up @@ -31,38 +46,66 @@ const renderComponent = () => {
};

describe('ForgotPassword', () => {
let user;

const emailInput = () => screen.getByLabelText(emailLabel$());
const submitButton = () => screen.getByRole('button', { name: submitButton$() });

beforeEach(() => {
jest.clearAllMocks();
user = userEvent.setup();
});

it('should render the forgot password form', () => {
renderComponent();
expect(screen.getByText('Reset your password')).toBeInTheDocument();
expect(screen.getByLabelText('Email')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Submit' })).toBeInTheDocument();
expect(screen.getByText(forgotPasswordTitle$())).toBeInTheDocument();
expect(emailInput()).toBeInTheDocument();
expect(submitButton()).toBeInTheDocument();
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
});

it('should not submit form with empty email', async () => {
renderComponent();
await fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await user.click(submitButton());
await waitFor(() => {
expect(screen.getByTestId('email-field')).toHaveTextContent(fieldRequired$());
});
expect(sendPasswordResetLinkMock).not.toHaveBeenCalled();
});

it('should not submit form with invalid email', async () => {
renderComponent();
await fireEvent.update(screen.getByLabelText(/email/i), 'invalid-email');
await fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await user.type(emailInput(), INVALID_EMAIL);
await user.click(submitButton());
await waitFor(() => {
expect(screen.getByTestId('email-field')).toHaveTextContent(emailValidationMessage$());
});
expect(sendPasswordResetLinkMock).not.toHaveBeenCalled();
});

it('should not show validation error while typing', async () => {
Comment thread
adibmbrk marked this conversation as resolved.
Resolved
renderComponent();
await user.type(emailInput(), INVALID_EMAIL);
expect(screen.getByTestId('email-field')).not.toHaveTextContent(emailValidationMessage$());
});

it('should show validation error on blur', async () => {
renderComponent();
await user.type(emailInput(), INVALID_EMAIL);
await user.tab();
await waitFor(() => {
expect(screen.getByTestId('email-field')).toHaveTextContent(emailValidationMessage$());
});
});

it('should submit form with valid email and navigate to success page', async () => {
sendPasswordResetLinkMock.mockResolvedValue({});
const { router } = renderComponent();
await fireEvent.update(screen.getByLabelText(/email/i), 'test@test.com');
await fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
await user.type(emailInput(), VALID_EMAIL);
await user.click(submitButton());
await waitFor(() => {
expect(sendPasswordResetLinkMock).toHaveBeenCalledTimes(1);
expect(sendPasswordResetLinkMock).toHaveBeenCalledWith(expect.anything(), 'test@test.com');
expect(sendPasswordResetLinkMock).toHaveBeenCalledWith(expect.anything(), VALID_EMAIL);
});
await waitFor(() => {
expect(router.currentRoute.name).toBe('PasswordInstructionsSent');
Expand All @@ -72,8 +115,8 @@ describe('ForgotPassword', () => {
it('should show error banner when submission fails', async () => {
sendPasswordResetLinkMock.mockRejectedValue(new Error('Failed'));
renderComponent();
await fireEvent.update(screen.getByLabelText(/email/i), 'test@test.com');
await fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
expect(await screen.findByTestId('error-banner')).toBeInTheDocument();
await user.type(emailInput(), VALID_EMAIL);
await user.click(submitButton());
expect(await screen.findByRole('alert')).toHaveTextContent(forgotPasswordFailed$());
});
});
Original file line number Diff line number Diff line change
@@ -1,63 +1,89 @@
<template>

<MessageLayout
<StudioMessageLayout
:header="$tr('forgotPasswordTitle')"
:text="$tr('forgotPasswordPrompt')"
>
<VForm
ref="form"
lazy-validation
<form
class="forgot-password-form"
novalidate
@submit.prevent="submit"
>
<Banner
:text="$tr('forgotPasswordFailed')"
:value="error"
<StudioBanner
Comment thread
adibmbrk marked this conversation as resolved.
Resolved
v-if="error"
role="alert"
error
class="mb-4"
data-testid="error-banner"
/>
<EmailField
class="banner"
>
{{ $tr('forgotPasswordFailed') }}
</StudioBanner>
<StudioEmailField
v-model="email"
autofocus
data-testid="email-field"
:label="$tr('emailLabel')"
:errorMessages="showEmailError && errors.email ? [emailErrorText] : []"
@blur="showEmailError = true"
/>
<KButton
primary
class="w-100"
:text="$tr('submitButton')"
type="submit"
/>
</VForm>
</MessageLayout>
</form>
</StudioMessageLayout>

</template>


<script>

import { mapActions } from 'vuex';
import MessageLayout from '../../components/MessageLayout';
import EmailField from 'shared/views/form/EmailField';
import Banner from 'shared/views/Banner';
import StudioMessageLayout from '../../components/StudioMessageLayout';
import StudioEmailField from '../../components/form/StudioEmailField';
import StudioBanner from 'shared/views/StudioBanner';
import commonStrings from 'shared/translator';
import { generateFormMixin } from 'shared/mixins';

const formMixin = generateFormMixin({
email: {
required: true,
validator: v => Boolean(v && v.trim()) && /.+@.+\..+/.test(v),
},
});

export default {
name: 'ForgotPassword',
components: {
MessageLayout,
EmailField,
Banner,
StudioMessageLayout,
StudioEmailField,
StudioBanner,
},
mixins: [formMixin],
data() {
return {
email: '',
error: false,
showEmailError: false,
};
},
computed: {
emailErrorText() {
if (!this.email || !this.email.trim()) {
/* eslint-disable-next-line kolibri/vue-no-undefined-string-uses */
return commonStrings.$tr('fieldRequired');
}
return this.$tr('emailValidationMessage');
},
},
methods: {
...mapActions('account', ['sendPasswordResetLink']),
submit() {
this.error = false;
if (this.$refs.form.validate()) {
this.sendPasswordResetLink(this.email)
this.showEmailError = true;
const formData = this.clean();
if (this.validate(formData)) {
this.sendPasswordResetLink(formData.email)
.then(() => {
this.$router
.push({
Expand All @@ -77,6 +103,8 @@
'Please enter your email address to receive instructions for resetting your password',
submitButton: 'Submit',
forgotPasswordFailed: 'Failed to send a password reset link. Please try again.',
emailLabel: 'Email',
emailValidationMessage: 'Please enter a valid email',
},
};

Expand All @@ -85,6 +113,17 @@

<style lang="scss" scoped>

.forgot-password-form {
width: 400px;
Comment thread
adibmbrk marked this conversation as resolved.
Resolved
max-width: 100%;
text-align: left;
}

.banner {
width: 100%;
margin-bottom: 16px;
}

.w-100 {
width: 100%;
}
Expand Down
Loading