react-icons/md#MdImage TypeScript Examples

The following examples show how to use react-icons/md#MdImage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InputFileField.test.tsx    From hub with Apache License 2.0 4 votes vote down vote up
describe('InputFileField', () => {
  afterEach(() => {
    jest.resetAllMocks();
  });

  it('creates snapshot', () => {
    const { asFragment } = render(<InputFileField {...defaultProps} />);
    expect(asFragment()).toMatchSnapshot();
  });

  it('renders proper content', () => {
    render(<InputFileField {...defaultProps} />);
    expect(screen.getByLabelText(defaultProps.label)).toBeInTheDocument();
    expect(screen.getByLabelText('message')).toBeInTheDocument();
    expect(screen.getByLabelText('message')).toHaveClass('d-none');
    expect(screen.getByRole('button', { name: 'Add image' })).toBeInTheDocument();
    expect(screen.getByTestId('defaultIcon')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Add image' })).toHaveProperty('type', 'button');
  });

  it('renders custom placeholder icon', () => {
    render(<InputFileField {...defaultProps} placeholderIcon={<MdImage />} />);
    expect(screen.queryByTestId('defaultIcon')).toBeNull();
  });

  it('calls saveImage to click saveOriginalBtn button', async () => {
    mocked(API).saveImage.mockResolvedValue({ imageId: '16782' });
    render(<InputFileField {...defaultProps} />);
    const input = screen.getByLabelText('message');
    const file = new File(['(image)'], 'testImage.png', { type: 'image/png' });
    await userEvent.upload(input, file);

    expect(await screen.findByRole('dialog')).toHaveClass('d-block');

    const saveBtn = await screen.findByRole('button', { name: 'Save original' });
    await userEvent.click(saveBtn);

    await waitFor(() => expect(API.saveImage).toHaveBeenCalledTimes(1));

    expect(onImageChangeMock).toHaveBeenCalledTimes(1);
    expect(onImageChangeMock).toHaveBeenCalledWith('16782');
  });

  it('calls alertDispatcher when an error occurred to save image', async () => {
    mocked(API).saveImage.mockRejectedValue({ kind: ErrorKind.Other });
    render(<InputFileField {...defaultProps} />);
    const input = screen.getByLabelText('message');
    const file = new File(['(image)'], 'testImage.png', { type: 'image/png' });
    await userEvent.upload(input, file);

    expect(await screen.findByRole('dialog')).toHaveClass('d-block');

    const saveBtn = await screen.findByRole('button', { name: 'Save original' });
    await userEvent.click(saveBtn);

    await waitFor(() => expect(API.saveImage).toHaveBeenCalledTimes(1));

    expect(alertDispatcher.postAlert).toHaveBeenCalledTimes(1);
    expect(alertDispatcher.postAlert).toHaveBeenCalledWith({
      type: 'danger',
      message: 'An error occurred saving the image, please try again later.',
    });
  });

  it('calls onAuthError when UnauthorizedError is returned', async () => {
    mocked(API).saveImage.mockRejectedValue({
      kind: ErrorKind.Unauthorized,
    });
    render(<InputFileField {...defaultProps} />);
    const input = screen.getByLabelText('message');
    const file = new File(['(image)'], 'testImage.png', { type: 'image/png' });
    await userEvent.upload(input, file);

    expect(await screen.findByRole('dialog')).toHaveClass('d-block');

    const saveBtn = await screen.findByRole('button', { name: 'Save original' });
    await userEvent.click(saveBtn);

    await waitFor(() => expect(API.saveImage).toHaveBeenCalledTimes(1));

    expect(onAuthErrorMock).toHaveBeenCalledTimes(1);
  });

  it('calls alertDispatcher when file is not an image', async () => {
    mocked(API).saveImage.mockResolvedValue({ imageId: '16782' });
    render(<InputFileField {...defaultProps} />);
    const input = screen.getByLabelText('message');
    const file = new File(['(text)'], 'text.txt', { type: 'text/text' });
    await userEvent.upload(input, file);

    await waitFor(() => expect(alertDispatcher.postAlert).toHaveBeenCalledTimes(1));
    expect(alertDispatcher.postAlert).toHaveBeenCalledWith({
      type: 'danger',
      message: 'Sorry, only images are accepted',
    });
  });
});