Skip to content

Commit

Permalink
fix(multidropzone): [EMU-5919] add label to multidropzone component (#…
Browse files Browse the repository at this point in the history
…259)

* fix(multidropzone): [EMU-5919] add label to multidropzone component

* fix(multidropzone): [EMU-5919] update test title

* fix(multidropzone): [EMU-5919] update input id

* refactor(multidropzone): [EMU-5919] update method to create unique id

* fix(multidropzone): [EMU-5919] update label pointer type
  • Loading branch information
a-maffei authored Sep 7, 2023
1 parent 877a2d2 commit 4606951
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 123 deletions.
182 changes: 101 additions & 81 deletions src/lib/components/multiDropzone/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ const mockOnFileSelect = jest.fn();
const mockOnRemoveFile = jest.fn();
const file = new File(['DummyFile'], 'dummy.png', { type: 'image/png' });

const inputTestId = "ds-drop-input";
const spinnerTestId = "ds-filecell-spinner";
const progressbarTestId = "ds-filecell-progressbar";
const inputTestId = 'ds-drop-input';
const spinnerTestId = 'ds-filecell-spinner';
const progressbarTestId = 'ds-filecell-progressbar';
const uploadedFilesMock = {
id: "123",
name: "File name",
progress: 100,
type: "jpg",
id: '123',
name: 'File name',
progress: 100,
type: 'jpg',
};

const setup = ({
uploadedFiles = [],
uploading = false,
...rest
uploadedFiles = [],
uploading = false,
...rest
}: Partial<MultiDropzoneProps>) => {
return render(
<MultiDropzone
Expand All @@ -34,7 +34,7 @@ const setup = ({
};

describe('MultiDropzone component', () => {
it("should call onFileSelect on files change", async () => {
it('should call onFileSelect on files change', async () => {
const { getByTestId, user } = setup({});
const files = [file, file];

Expand All @@ -44,32 +44,35 @@ describe('MultiDropzone component', () => {
});

describe('Error states', () => {
it("should show max files error message", () => {
it('should show max files error message', () => {
const screen = setup({
maxFiles: 1,
uploadedFiles: [uploadedFilesMock, {
...uploadedFilesMock,
id: "222"
}],
uploadedFiles: [
uploadedFilesMock,
{
...uploadedFilesMock,
id: '222',
},
],
});
expect(screen.getByText("Too many files.")).toBeVisible();

expect(screen.getByText('Too many files.')).toBeVisible();
});

it("should show max file size error message", async () => {
it('should show max file size error message', async () => {
const { getByTestId, getByText, user } = setup({ maxSize: 10 });
const bigFile = file;
Object.defineProperty(bigFile, 'size', { value: 1024 });

await user.upload(getByTestId(inputTestId), [bigFile]);

expect(
getByText("File is too large. It must be less than 10 Bytes.")
getByText('File is too large. It must be less than 10 Bytes.')
).toBeInTheDocument();
});

it("should show wrong filetype error message", async () => {
const { getByTestId, getByText } = setup({ accept: "document" });
it('should show wrong filetype error message', async () => {
const { getByTestId, getByText } = setup({ accept: 'document' });
const input = getByTestId(inputTestId);

await act(async () => {
Expand All @@ -79,12 +82,14 @@ describe('MultiDropzone component', () => {
});

expect(
getByText("File type must be one of DOC, DOCX, PDF")
getByText('File type must be one of DOC, DOCX, PDF')
).toBeInTheDocument();
});

it("should remove wrong filetype error message", async () => {
const { getByAltText, getByTestId, queryByText, user } = setup({ accept: "document" });
it('should remove wrong filetype error message', async () => {
const { getByAltText, getByTestId, queryByText, user } = setup({
accept: 'document',
});
const input = getByTestId(inputTestId);

await act(async () => {
Expand All @@ -93,135 +98,150 @@ describe('MultiDropzone component', () => {
fireEvent.change(input, { target: { files: [file] } });
});

await user.click(getByAltText("remove"));
await user.click(getByAltText('remove'));

expect(queryByText("File type must be one of DOC, DOCX, PDF")).not.toBeInTheDocument();
expect(
queryByText('File type must be one of DOC, DOCX, PDF')
).not.toBeInTheDocument();
});
});

describe('Copy text', () => {
it("should show uploader text", () => {
it('should show uploader text', () => {
const screen = setup({});

expect(screen.getByText("Choose file or drag & drop")).toBeInTheDocument();
expect(
screen.getByText('Choose file or drag & drop')
).toBeInTheDocument();
});

it("should show uploader text translated", () => {
const instructionsText = "Drag drop file";
it('should show uploader text translated', () => {
const instructionsText = 'Drag drop file';
const screen = setup({
textOverrides: { instructionsText }
textOverrides: { instructionsText },
});

expect(screen.getByText(instructionsText)).toBeInTheDocument();
});

it("should show image accept file type label", () => {
const screen = setup({ accept: "image" });
it('should show image accept file type label', () => {
const screen = setup({ accept: 'image' });

expect(
screen.getByText("Supports HEIC, BMP, JPEG, JPG, PNG")
screen.getByText('Supports HEIC, BMP, JPEG, JPG, PNG')
).toBeInTheDocument();
});

it("should show document accept file type label", () => {
const screen = setup({ accept: "document" });
it('should show document accept file type label', () => {
const screen = setup({ accept: 'document' });

expect(
screen.getByText("Supports DOC, DOCX, PDF")
).toBeInTheDocument();
expect(screen.getByText('Supports DOC, DOCX, PDF')).toBeInTheDocument();
});

it("should custom document accept file type label", () => {
const screen = setup({ accept: {
"application/pdf": [".pdf"],
"image/jpg": [".jpg"],
} });
it('should custom document accept file type label', () => {
const screen = setup({
accept: {
'application/pdf': ['.pdf'],
'image/jpg': ['.jpg'],
},
});

expect(
screen.getByText("Supports PDF, JPG")
).toBeInTheDocument();
expect(screen.getByText('Supports PDF, JPG')).toBeInTheDocument();
});

it("should show disabled text if is uploading", () => {
it('should show disabled text if is uploading', () => {
const screen = setup({ uploading: true });

expect(
screen.getByText("Please wait while uploading file...")
screen.getByText('Please wait while uploading file...')
).toBeInTheDocument();
});

it('should associate input with its label', () => {
const { getByLabelText } = setup({});
const input = getByLabelText('Choose file or drag & drop');

expect(input).toBeInTheDocument();
});
});

describe('Uploaded files', () => {
it("should show uploaded files", () => {
it('should show uploaded files', () => {
const screen = setup({
uploadedFiles: [uploadedFilesMock],
});

expect(
screen.getByText(uploadedFilesMock.name)
).toBeInTheDocument();
expect(screen.getByText(uploadedFilesMock.name)).toBeInTheDocument();
});

it("should call onRemoveFile with uploaded file id", () => {
it('should call onRemoveFile with uploaded file id', () => {
const screen = setup({
uploadedFiles: [uploadedFilesMock],
});

screen.getByAltText("remove").click();
screen.getByAltText('remove').click();

expect(mockOnRemoveFile).toBeCalledWith(uploadedFilesMock.id);
});

it("should show uploaded file with uploading label", () => {
it('should show uploaded file with uploading label', () => {
const screen = setup({
uploadedFiles: [{ ...uploadedFilesMock, progress: 50 }],
});

expect(screen.getByText("Uploading...")).toBeInTheDocument();
expect(screen.getByText('Uploading...')).toBeInTheDocument();
});

it("should show uploaded file with progress bar", () => {
it('should show uploaded file with progress bar', () => {
const screen = setup({
uploadedFiles: [{
...uploadedFilesMock,
progress: 50,
}],
uploadedFiles: [
{
...uploadedFilesMock,
progress: 50,
},
],
});

expect(screen.getByTestId(progressbarTestId)).toBeInTheDocument();
});

it("should show uploaded file with no progress bar", () => {
it('should show uploaded file with no progress bar', () => {
const screen = setup({
uploadedFiles: [{
...uploadedFilesMock,
progress: 50,
showProgressBar: false
}],
uploadedFiles: [
{
...uploadedFilesMock,
progress: 50,
showProgressBar: false,
},
],
});

expect(screen.queryByTestId(progressbarTestId)).not.toBeInTheDocument();
});

it("should show uploaded file with loading spinner", () => {
it('should show uploaded file with loading spinner', () => {
const screen = setup({
uploadedFiles: [{
...uploadedFilesMock,
progress: 50,
showLoadingSpinner: true
}],
uploadedFiles: [
{
...uploadedFilesMock,
progress: 50,
showLoadingSpinner: true,
},
],
});

expect(screen.getByTestId(spinnerTestId)).toBeInTheDocument();
});

it("should show uploaded file with no loading spinner", () => {
it('should show uploaded file with no loading spinner', () => {
const screen = setup({
uploadedFiles: [{
...uploadedFilesMock,
progress: 50,
showLoadingSpinner: false
}],
uploadedFiles: [
{
...uploadedFilesMock,
progress: 50,
showLoadingSpinner: false,
},
],
});

expect(screen.queryByTestId(spinnerTestId)).not.toBeInTheDocument();
Expand Down
Loading

0 comments on commit 4606951

Please sign in to comment.