docbrown/app/javascript/onboarding/components/__tests__/ProfileImage.test.jsx
Rajat Talesra 67ff8d4efb
Add Edit Profile Image option to Onboarding (#19589)
* Basic implementation

* Nit fix

* Using existing profile update api

* API call working

* Basic full implementation

* Nit fixes in design

* UI changes

* Image upload limit set

* Removed drag & drop

* Image size issue fix

* Minor naming fixes

* Removed native mobile code

* Nit fix

* Added few tests

* Added test

* Added test

* Added image upload error

* Added validateFileInput test failure case

* Updated code to test fix

* Revert db changes

* Minor design fixes

* Minor updated test

* Added validateFileInputs test temporarily

* Added processImageUpload test temporarily

* Aded files count test

* Minor warning fixes

* Updated placeholder text

* Added tests for TextInput and TextArea

* Removed un-required test file

* Removed un-required code

* Minor code cleaning
2023-06-23 16:08:27 +05:30

133 lines
3.8 KiB
JavaScript

import { h } from 'preact';
import { render, fireEvent, waitFor } from '@testing-library/preact';
import { axe } from 'jest-axe';
import fetch from 'jest-fetch-mock';
import '@testing-library/jest-dom';
import { ProfileImage } from '../ProfileForm/ProfileImage';
global.fetch = fetch;
describe('<ProfileImage />', () => {
it('should render correctly', () => {
const onMainImageUrlChangeMock = jest.fn();
const { getByTestId } = render(
<ProfileImage
onMainImageUrlChange={onMainImageUrlChangeMock}
mainImage="test.jpg"
userId="1"
name="Test User"
/>,
);
expect(getByTestId('profile-image-input')).toBeInTheDocument();
});
it('should have no a11y violations', async () => {
const { container } = render(
<ProfileImage
mainImage="/i/r5tvutqpl7th0qhzcw7f.png"
onMainImageUrlChange={jest.fn()}
userId="user1"
name="User 1"
/>,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it('displays an upload input when the image is not being uploaded', () => {
const { getByLabelText } = render(
<ProfileImage
mainImage=""
onMainImageUrlChange={jest.fn()}
userId="user1"
name="User 1"
/>,
);
const uploadInput = getByLabelText(/edit profile image/i);
expect(uploadInput.getAttribute('type')).toEqual('file');
});
it('shows the uploaded image', () => {
const { getByRole, queryByText } = render(
<ProfileImage
mainImage="/some-fake-image.jpg"
onMainImageUrlChange={jest.fn()}
userId="user1"
name="User 1"
/>,
);
const uploadInput = getByRole('img', { name: 'profile' });
expect(uploadInput.getAttribute('src')).toEqual('/some-fake-image.jpg');
expect(queryByText('Uploading...')).not.toBeInTheDocument();
});
it('shows the "Uploading..." message when an image is being uploaded', () => {
const { getByLabelText, getByText } = render(
<ProfileImage
mainImage=""
onMainImageUrlChange={jest.fn()}
userId="user1"
name="User 1"
/>,
);
const file = new File(['file content'], 'filename.png', {
type: 'image/png',
});
const uploadInput = getByLabelText(/edit profile image/i);
fireEvent.change(uploadInput, { target: { files: [file] } });
expect(getByText('Uploading...')).toBeInTheDocument();
});
it('displays an upload error when necessary', async () => {
const { getByLabelText, findByText, queryByText } = render(
<ProfileImage
onMainImageUrlChange={jest.fn()}
mainImage="test.png"
userId="1"
name="Test User"
/>,
);
const inputEl = getByLabelText('Edit profile image', { exact: false });
expect(inputEl.getAttribute('accept')).toEqual('image/*');
const file = new File(['(⌐□_□)'], 'chucknorris.png', {
type: 'image/png',
});
fetch.mockReject({
message: 'Some Fake Error',
});
fireEvent.change(inputEl, { target: { files: [file] } });
const fakeError = await findByText(/some fake error/i);
expect(fakeError).toBeInTheDocument();
expect(queryByText('Uploading...')).not.toBeInTheDocument();
});
it('should handle image upload correctly', async () => {
const onMainImageUrlChangeMock = jest.fn();
const { getByTestId } = render(
<ProfileImage
onMainImageUrlChange={onMainImageUrlChangeMock}
mainImage=""
userId="user1"
name="User 1"
/>,
);
const file = new File(['file content'], 'filename.png', {
type: 'image/png',
});
const uploadInput = getByTestId('profile-image-input');
fireEvent.change(uploadInput, { target: { files: [file] } });
await waitFor(() => {
expect(uploadInput.files).toHaveLength(1);
});
});
});