Back to blog
1/10/2024
8 min read
Testing

How to Use Placeholder JSON for UI Testing: A Complete Guide

Learn how to effectively use mock data and placeholder JSON APIs to test your user interfaces. Real-world examples and best practices included.

By MockDog Team

UI testing is a critical part of frontend development, and placeholder JSON APIs are your secret weapon for creating robust, reliable tests. Let's dive deep into how to use them effectively.

What is Placeholder JSON?

JSON data structure visualization

JSON data structure visualization

Placeholder JSON is fake data that mimics real API responses. It's perfect for testing because it:

  • Provides consistent, predictable data
  • Eliminates backend dependencies
  • Speeds up development cycles
  • Reduces testing costs

Setting Up Your Testing Environment

1. Choose the Right Mock API

For dog-related applications, our MockDog API is perfect:

const API_URL = 'https://mockdog.dev/api/dogs';

2. Create Test Data Utilities

// test-utils.js export const fetchMockDogs = async (count = 10) => { const response = await fetch(`$\{API_URL}?count=$\{count}`); return response.json(); }; export const createMockDog = (overrides = {}) => ({ id: 'test-id-123', name: 'Test Dog', age: 5, gender: 'male', description: 'A test dog for UI testing', imageUrl: 'https://placedog.net/400/300?id=1', ...overrides });

Testing Different UI States

1. Loading States

test('shows loading spinner while fetching dogs', async () => { render(<DogList />); expect(screen.getByTestId('loading-spinner')).toBeInTheDocument(); await waitFor(() => { expect(screen.queryByTestId('loading-spinner')).not.toBeInTheDocument(); }); });

Best Practices

Testing workflow diagram

Testing workflow diagram

  1. Use Consistent Data: Always use the same mock data structure
  2. Test Edge Cases: Include empty arrays, null values, and error responses
  3. Mock Network Delays: Test loading states with realistic timing
  4. Clean Up: Reset mocks between tests
  5. Document Your Mocks: Keep mock data structures documented

Conclusion

Placeholder JSON APIs are essential for modern frontend development. They enable faster development, better testing, and more reliable applications. Start with simple mock data and gradually build more sophisticated testing scenarios.

Remember: Good testing leads to better code, and better code leads to happier users!