Back to Repositories

Testing Upload Runner Data Preview Generation in Insomnia

This test suite validates the table preview data generation functionality in the Insomnia upload runner data modal. It ensures proper handling of JSON data conversion for display, including validation of both valid and invalid input scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the genPreviewTableData function, examining:

  • Normal JSON data processing with expected key-value pairs
  • Complex JSON handling with mixed valid and invalid elements
  • Invalid JSON input validation and error handling
  • Edge cases including undefined, null, and non-object array elements

Implementation Analysis

The testing approach utilizes Vitest for TypeScript-based unit testing, implementing discrete test cases for different data scenarios. The implementation leverages TypeScript’s type checking capabilities while deliberately testing type violations using @ts-expect-error annotations for invalid input validation.

Technical Details

  • Testing Framework: Vitest
  • Language: TypeScript
  • Test Structure: describe/it blocks
  • Assertion Methods: expect().toEqual()
  • Type Checking: TypeScript with intentional error testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test cases, clear test descriptions, and comprehensive edge case coverage. It demonstrates proper error handling validation and type checking, while maintaining clear separation of concerns between different test scenarios.

kong/insomnia

packages/insomnia/src/ui/components/modals/__tests__/upload-runner-data-modal.test.ts

            
import { describe, expect, it } from 'vitest';

import { genPreviewTableData } from '../upload-runner-data-modal';

describe('test generate table preview data ', () => {

  it('test normal json input', () => {
    const uploadData = [
      {
        'position': 0,
        'value': 'value0',
      },
      {
        'position': 1,
        'value': 'value1',
      },
      {
        'position': 2,
        'valeu': 'value-typo',
      },
    ];
    const { data, headers } = genPreviewTableData(uploadData);
    expect(headers).toEqual(['position', 'value', 'valeu']);
    expect(data).toEqual([
      {
        'position': 0,
        'value': 'value0',
      },
      {
        'position': 1,
        'value': 'value1',
      },
      {
        'position': 2,
        'valeu': 'value-typo',
      },
    ]);
  });

  it('test complex json input', () => {
    const uploadData = [
      {
        'position': 0,
        'value': 'value0',
      },
      'invalid',
      undefined,
      [1, 2, 3],
      null,
    ];
    // @ts-expect-error test inalid input
    const { data, headers } = genPreviewTableData(uploadData);
    expect(headers).toEqual(['position', 'value']);
    expect(data).toEqual([
      {
        'position': 0,
        'value': 'value0',
      },
    ]);
  });

  it('test invalid json input', () => {
    const uploadData = [
      'invalid',
      [1, 2, 3],
      undefined,
      null,
    ];
    // @ts-expect-error test inalid input
    const { data, headers } = genPreviewTableData(uploadData);
    expect(headers.length).toBe(0);
    expect(data.length).toBe(0);
  });
});