Development4 min readSeptember 21, 2025

Frontend Developer: Testing a Document Upload Component

Frontend Developer: Testing a Document Upload Component

Scenario

Setting: Document management SaaS (composite)
Industry: Software as a Service

Challenge

Testing a React document upload component against size limits, formats and error states

This is a composite scenario based on common frontend development patterns. It does not describe a specific company, and it contains no measured results.

The Scenario

A frontend developer is building a drag-and-drop upload component for a document management app. The requirements are:

  • Accepted formats: PDF, DOCX, XLSX and TXT
  • Maximum size: 25 MB per file
  • Progress bar for uploads that take longer than a second
  • Clear errors for wrong formats and oversized files, shown before the upload starts

Unit tests can use tiny in-memory File objects, but some bugs only show up with real files: progress events, memory use, MIME types reported by the browser, and how the backend reacts to a 25 MB request.

Step 1: Map Requirements to Test Files

Each requirement gets at least one file that passes and one that fails. Using the Document tab in FileMock:

RequirementPassFail
PDF acceptedpdf-1mb.pdfn/a
DOCX accepteddocx-1mb.docxn/a
XLSX acceptedxlsx-1mb.xlsxn/a
TXT acceptedtxt-100kb.txtn/a
Other formatsn/artf-1mb.rtf, html-1mb.html
Size limitpdf-24.9mb.pdfpdf-25.1mb.pdf
Progress barpdf-20mb.pdfn/a

Step 2: Agree on What "25 MB" Means

This is the most common source of off-by-a-little bugs. FileMock uses binary megabytes, so a 25 MB file is 26,214,400 bytes. If the component checks file.size > 25 * 1000 * 1000, that file will be rejected; with 25 * 1024 * 1024 it will be accepted.

Whichever definition the product uses, the frontend and the backend must use the same one. Verify the exact byte counts of your boundary files:

wc -c < pdf-24.9mb.pdf
wc -c < pdf-25.1mb.pdf

To test the exact boundary, append bytes to a generated file:

# A file exactly 1 byte over 26,214,400 bytes, starting from a 25 MB PDF
cp pdf-25mb.pdf pdf-over-by-one.pdf
printf 'x' >> pdf-over-by-one.pdf

Step 3: Use Real Files in Component Tests

With Testing Library, real fixture files can be loaded from disk and passed to the input. This catches problems that hand-built File objects miss, such as the MIME type the component expects:

import fs from "fs";
import path from "path";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import UploadComponent from "./UploadComponent";

function fixture(name, type) {
  const buffer = fs.readFileSync(path.join(__dirname, "fixtures", name));
  return new File([buffer], name, { type });
}

test("rejects a PDF just over the size limit", async () => {
  render(<UploadComponent />);
  const input = screen.getByLabelText(/upload/i);

  await userEvent.upload(input, fixture("pdf-25.1mb.pdf", "application/pdf"));

  expect(screen.getByRole("alert")).toHaveTextContent(/25 MB/);
});

test("accepts a PDF just under the size limit", async () => {
  render(<UploadComponent />);
  const input = screen.getByLabelText(/upload/i);

  await userEvent.upload(input, fixture("pdf-24.9mb.pdf", "application/pdf"));

  expect(screen.queryByRole("alert")).not.toBeInTheDocument();
});

Keep large fixtures out of unit tests that run on every save; run them in a separate integration suite.

Step 4: Derive the Invalid Files

FileMock generates valid documents. The invalid ones come from simple commands:

# Empty file
: > empty.pdf

# Valid file with a misleading extension (DOCX content, .pdf name)
cp docx-1mb.docx looks-like-pdf.pdf

# Double extension
cp txt-100kb.txt report.pdf.txt

# No extension
cp pdf-1mb.pdf document

Expected behavior for each:

  • Empty file: rejected with a specific message, not a crash on reading the file
  • Misleading extension: the browser reports MIME type from the extension, so only a content check (on the server, or by reading the first bytes) catches it
  • Double extension: judged by the final extension
  • No extension: rejected, or detected by content if the product supports that

Step 5: Test Progress and Cancellation Manually

Automated tests rarely cover the experience of a slow upload. With the 20 MB PDF and the browser's network throttling (DevTools → Network → "Slow 4G"):

  • The progress bar appears and moves smoothly
  • Cancelling mid-upload stops the request and resets the component
  • Dropping a second file during an upload behaves as designed
  • The page stays responsive while the file is read

For more component test patterns, see Testing File Uploads in React Applications.

Checklist

  • One passing and one failing file per requirement
  • Byte-level boundary files, verified with wc -c
  • Same size definition in frontend and backend
  • Empty, renamed, double-extension and extensionless files
  • Progress bar and cancellation tested on a throttled connection
  • Error messages that say what is allowed, not only what failed

Generate the PDFs for this checklist with the FileMock PDF generator. DOCX, XLSX, TXT and other formats are in the same Document tab, created in your browser at the size you choose.

Related Case Studies

Ready to Achieve Similar Results?

Transform your testing workflow like Document management SaaS (composite). Generate comprehensive test files and build reliable applications with FileMock's powerful file generation capabilities.