Skip to content

Commit

Permalink
[#38] Add i18next mocks and use translation slugs in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
malparty committed Oct 26, 2021
1 parent 76721ec commit 9325597
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 32 deletions.
8 changes: 8 additions & 0 deletions src/__mocks__/react-i18next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const useMock = {
t: (k: string) => {
return k;
},
i18n: {changeLanguage: (language: string) => {}}
};

export const useTranslation = () => useMock;
14 changes: 7 additions & 7 deletions src/components/LoginForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export interface LoginFormValues {
password: string;
}

export const submitLoginForm = async (values: LoginFormValues, { setSubmitting }: FormikHelpers<LoginFormValues>) => {
await new Promise((resolve) => setTimeout(resolve, 2000)); // Will be replaced by API call in BackEnd Task

console.log(JSON.stringify(values, null, 2)); // Will be replaced by authentication & redirection in BackEnd Task
setSubmitting(false);
};

const LoginForm = () => {
const { t } = useTranslation();

Expand All @@ -20,13 +27,6 @@ const LoginForm = () => {
.required(t('login.errors.password_required'))
});

const submitLoginForm = async (values: LoginFormValues, { setSubmitting }: FormikHelpers<LoginFormValues>) => {
await new Promise((resolve) => setTimeout(resolve, 2000)); // Will be replaced by API call in BackEnd Task

console.log(JSON.stringify(values, null, 2)); // Will be replaced by authentication & redirection in BackEnd Task
setSubmitting(false);
};

return (
<div className="login-form">
<img className="login-form__header-image" src={nimbleLogo} alt={t('login.nimble_logo_alt')} />
Expand Down
4 changes: 1 addition & 3 deletions src/tests/Login/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ describe('LoginScreen', () => {
const screenClass = 'login-screen';

it('displays a sign in to Nimble message', () => {
const signInText = 'Sign in to Nimble';

const loginScreen = render(<LoginScreen />);

expect(loginScreen.getByText(signInText)).toBeVisible();
expect(loginScreen.getByText('login.title')).toBeVisible();
});

it('adds the screen class to body', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Object {
class="base-alert__icon"
>
<img
alt="Error icon"
alt="base_alert.error_icon_alt"
src="icon-error.svg"
/>
</div>
Expand All @@ -22,7 +22,7 @@ Object {
<h5
class="base-alert__content-title"
>
Error
base_alert.error_title
</h5>
<div
class="base-alert__content-message"
Expand All @@ -42,7 +42,7 @@ Object {
class="base-alert__icon"
>
<img
alt="Error icon"
alt="base_alert.error_icon_alt"
src="icon-error.svg"
/>
</div>
Expand All @@ -52,7 +52,7 @@ Object {
<h5
class="base-alert__content-title"
>
Error
base_alert.error_title
</h5>
<div
class="base-alert__content-message"
Expand Down
20 changes: 10 additions & 10 deletions src/tests/components/LoginForm/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Object {
class="login-form"
>
<img
alt="Nimble Logo"
alt="login.nimble_logo_alt"
class="login-form__header-image"
src="nimble-logo.png"
/>
<div
class="login-form__header-title"
>
Sign in to Nimble
login.title
</div>
<form
action="#"
Expand All @@ -28,7 +28,7 @@ Object {
class="login-form__label"
for="email"
>
Email
login.email
</label>
<input
class="login-form__input form-control"
Expand All @@ -45,7 +45,7 @@ Object {
class="login-form__label"
for="password"
>
Password
login.password
</label>
<input
class="login-form__input form-control"
Expand All @@ -62,7 +62,7 @@ Object {
class="btn btn-light"
type="submit"
>
Sign in
login.submit
</button>
</div>
</form>
Expand All @@ -74,14 +74,14 @@ Object {
class="login-form"
>
<img
alt="Nimble Logo"
alt="login.nimble_logo_alt"
class="login-form__header-image"
src="nimble-logo.png"
/>
<div
class="login-form__header-title"
>
Sign in to Nimble
login.title
</div>
<form
action="#"
Expand All @@ -93,7 +93,7 @@ Object {
class="login-form__label"
for="email"
>
Email
login.email
</label>
<input
class="login-form__input form-control"
Expand All @@ -110,7 +110,7 @@ Object {
class="login-form__label"
for="password"
>
Password
login.password
</label>
<input
class="login-form__input form-control"
Expand All @@ -127,7 +127,7 @@ Object {
class="btn btn-light"
type="submit"
>
Sign in
login.submit
</button>
</div>
</form>
Expand Down
12 changes: 6 additions & 6 deletions src/tests/components/LoginForm/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { render } from '@testing-library/react';
import LoginForm, { LoginFormValues } from '@src/components/LoginForm';
import LoginForm, { LoginFormValues, submitLoginForm } from '@src/components/LoginForm';
import { FormikHelpers } from 'formik';

describe('LoginForm', () => {
Expand All @@ -11,27 +11,27 @@ describe('LoginForm', () => {

it('displays an email field with its label', () => {
const loginForm = render(<LoginForm />);
expect(loginForm.getByLabelText('Email')).toBeVisible();
expect(loginForm.getByLabelText('login.email')).toBeVisible();
});

it('displays a password field with its label', () => {
const loginForm = render(<LoginForm />);

expect(loginForm.getByLabelText('Password')).toBeVisible();
expect(loginForm.getByLabelText('login.password')).toBeVisible();
});

it('displays a Sign In button', () => {
const loginForm = render(<LoginForm />);

expect(loginForm.getByText('Sign in')).toBeVisible();
expect(loginForm.getByText('login.title')).toBeVisible();
});

it('calls the setSubmitting callback after submitLoginForn is called', async () => {
const loginForm = new LoginForm({});
const values = { email: 'test@email.com', password: '012345678' };

const formikHelpers = { setSubmitting: jest.fn() } as unknown as FormikHelpers<LoginFormValues>;

await loginForm.submitLoginForm(values, formikHelpers);
await submitLoginForm(values, formikHelpers);

expect(formikHelpers.setSubmitting).toBeCalledWith(false);
});
Expand Down
9 changes: 7 additions & 2 deletions src/tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
import App from '@src/App';

Expand All @@ -7,6 +7,9 @@ import App from '@src/App';
// called with <App /> and HTML element with id = root
jest.mock('react-dom', () => ({ render: jest.fn() }));

// mock i18n import as we do not need translations setup for this test
jest.mock('@src/i18n', () => ({}));

test('renders with App and root div', () => {
// Create and append to document body
// an HTML element with id = root
Expand All @@ -21,7 +24,9 @@ test('renders with App and root div', () => {
// and HTML element with id = root
expect(ReactDOM.render).toHaveBeenCalledWith(
<React.StrictMode>
<App />
<Suspense fallback="loading">
<App />
</Suspense>
</React.StrictMode>,
root
);
Expand Down

1 comment on commit 9325597

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 93.48% 43/46
🟢 Branches 87.5% 7/8
🟢 Functions 90.48% 19/21
🟢 Lines 93.18% 41/44

Test suite run success

26 tests passing in 8 suites.

Report generated by 🧪jest coverage report action from 9325597

Please sign in to comment.