Mockzilla logo

How-to: Mocking Infinite Scroll

Testing infinite scroll or pagination in a UI can be painful if you have to manually create dozens of JSON objects. Mockzilla’s Dynamic Data feature makes this effortless.

The Goal

Create an endpoint /api/posts that returns realistic generated posts. The schema controls the number of posts; a limit query parameter can be copied into response metadata, but does not change the generated array size.


Step 1: Define the Post Schema

Create a GET mock with endpoint /api/posts. In Mock Response, select Dynamic Response and paste this JSON Schema:

{
  "type": "array",
  "minItems": 10,
  "maxItems": 20,
  "items": {
    "type": "object",
    "properties": {
      "id": { "type": "string", "faker": "string.uuid" },
      "title": { "type": "string", "faker": "lorem.sentence" },
      "author": { "type": "string", "faker": "person.fullName" },
      "content": { "type": "string", "faker": "lorem.paragraphs" },
      "createdAt": { "type": "string", "format": "date-time", "faker": "date.past" }
    },
    "required": ["id", "title", "author"]
  }
}

Why this works:

  • maxItems sets the length: Mockzilla always generates exactly maxItems items, so this schema returns 20 posts on every request. To test a different list length, change maxItems. Always set it: an array without maxItems generates MOCKZILLA_MAX_ITEMS (default 1000) items.
  • format: date-time: Returns an ISO timestamp. Without it, date.past returns a JavaScript date string such as Thu Nov 27 2025 09:22:33 GMT….
  • Faker integration: Generates realistic sentences and names instead of "test 1", "test 2".

Step 2: Simulating Empty States

To test how your UI looks when there is no data, you don't need a new mock. Just temporarily change your schema:

{
  "type": "array",
  "minItems": 0,
  "maxItems": 0,
  "items": { "type": "object" }
}

Set both limits to 0 and keep items. Without items the schema fails and Mockzilla falls back to the static response; with only maxItems: 0, one item is still generated. Outcome: Your endpoint will now return [], allowing you to verify your "No posts found" UI state.


Step 3: Dynamic Pagination

Instead of creating multiple mocks, use a single mock that echoes back your page and limit query parameters using Request Context Interpolation. These values are metadata only; the schema's minItems and maxItems set the array size.

{
  "type": "object",
  "properties": {
    "items": { "type": "array", "minItems": 10, "maxItems": 10, "items": { "type": "object", "properties": { "id": { "type": "string", "faker": "string.uuid" } } } },
    "meta": {
      "type": "object",
      "properties": {
        "page": { "type": "integer", "const": "{{math (default $.query.page 1) \"+\" 0}}" },
        "limit": { "type": "integer", "const": "{{math (default $.query.limit 10) \"+\" 0}}" }
      }
    }
  }
}

When you call ?page=2&limit=50, meta returns {"page": 2, "limit": 50} as numbers. Without the query string it returns the defaults 1 and 10. The items array still contains the 10 entries configured in its schema. See missing values and types for how default and math work.


🤖 AI-First: Agent Instructions

Mockzilla is a developer and AI-first platform. Instead of writing JSON Schemas yourself, you should leverage an AI agent (like a compatible assistant via MCP) to do it for you based on your production spec.

AI Agent Command
Paste this command into your AI chat session to generate high-fidelity infinite scroll mocks.

"Use mockzilla-mock-maker to build a paginated API endpoint /api/posts for my my-api folder. It should use JSON Schema + Faker to return realistic content, supporting page and limit query parameters in the metadata."

// Tip: Provide a sample post object for 100% accuracy.


Summary

By using Dynamic Data, you can:

  1. Test Large Lists: Set maxItems to 100+. (Capped by MOCKZILLA_MAX_ITEMS env var, default 1000).
  2. Test Varied Content: Use Faker to generate long and short strings.
  3. Test Empty States: Set minItems and maxItems to 0.
  4. Echo Request Data: Use {{$.query.field}} to include query values in the response.

Next Step: See all configuration options in the Configuration Reference.