Skip to content

Commit

Permalink
fix: erro nos testes e criado uploadimage stories
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavogularte committed Sep 10, 2024
1 parent 61a57d1 commit 776b181
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/components/UploadImage/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { render, screen, userEvent } from '@testing-library/vanilla';
import { render, screen, userEvent, waitFor } from '@testing-library/vanilla';
import UploadImage from '.';
import plusIcon from './img/plus-icon.svg';
import photoIcon from './img/photo-icon.svg';
Expand All @@ -10,8 +10,12 @@ describe('UploadImage', () => {
it('renders correctly', () => {
renderUploadImage();
const uploadInput = screen.getByLabelText('Carregar imagem');
const buttonIcon = screen.getByAltText('Botão com ícone');
const imagePreview = screen.getByLabelText('Carregar imagem');

expect(uploadInput).toBeInTheDocument();
expect(buttonIcon).toBeInTheDocument();
expect(imagePreview).toBeInTheDocument();
});

describe('Upload input', () => {
Expand Down Expand Up @@ -47,29 +51,32 @@ describe('UploadImage', () => {

await user.upload(uploadInput, file);

expect(buttonIcon).toHaveAttribute('src', photoIcon);
await waitFor(() => {
expect(buttonIcon).toHaveAttribute('src', photoIcon);
});
});
});

describe('Image preview', () => {
it('does not display preview if no file is selected', () => {
renderUploadImage();
const Imagepreview = screen.getByAltText('Imagem carregada');
const imagePreview = screen.getByAltText('Imagem carregada');

expect(Imagepreview).toHaveClass('hidden');
expect(imagePreview).toHaveClass('hidden');
});

it('display preview if file is selected', async () => {
renderUploadImage();
const user = userEvent.setup();

const Imagepreview = screen.getByAltText('Imagem carregada');
const imagePreview = screen.getByAltText('Imagem carregada');
const uploadInput = screen.getByLabelText('Carregar imagem');
const file = new File(['hello'], 'hello.png', { type: 'image/png' });

await user.upload(uploadInput, file);

expect(Imagepreview).not.toHaveClass('hidden');
await waitFor(() => {
expect(imagePreview).toBeInTheDocument();
});
});
});
});
14 changes: 14 additions & 0 deletions src/stories/UploadImage.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import UploadImage from '../components/UploadImage';

export default {
title: 'Components/UploadImage',

render: () => {
const $container = document.createElement('div');
const uploadImage = new UploadImage();
uploadImage.mount($container);
return $container;
},
};

export const Default = {};

0 comments on commit 776b181

Please sign in to comment.