Back to Repositories

Testing Plugin Theme Validation and Template Processing in Insomnia

This test suite validates theme-related functionality and Nunjucks template processing in the Insomnia plugin system. It covers theme validation, name formatting, and template detection through comprehensive unit tests.

Test Coverage Overview

The test suite provides thorough coverage of three main components:
  • Nunjucks template detection in strings
  • Theme validation including rawCSS and nested theme blocks
  • Theme name validation and normalization
Edge cases include various template syntax patterns and complex nested theme structures. Key integration points focus on plugin theme validation and console error handling.

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks for organized test grouping and clear test case isolation. Implementation leverages vi.spyOn for console mocking and expect assertions for validation. The tests demonstrate effective use of TypeScript interfaces (PluginTheme) and mock data structures.

Technical Details

Testing tools and configuration:
  • Vitest/Jest as the testing framework
  • TypeScript for type safety
  • Mock implementations for console.error
  • Interface-based test data structures
  • Nested describe blocks for logical grouping

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated test cases, meaningful test descriptions, and comprehensive edge case coverage. Notable practices include:
  • Consistent test structure and naming
  • Proper mock data setup
  • Focused test assertions
  • Clear error message validation

kong/insomnia

packages/insomnia/src/plugins/misc.test.ts

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

import { containsNunjucks, PluginTheme, validateTheme, validateThemeName } from './misc';

describe('containsNunjucks', () => {
  it('will return true if the value contains nunjucks without', () => {
    expect(containsNunjucks('{{asdf}}')).toBeTruthy();
  });

  it('will return true if the value contains nunjucks with spaces', () => {
    expect(containsNunjucks('{{ asdf }}')).toBeTruthy();
  });

  it('will return false if the value contains nunjucks', () => {
    expect(containsNunjucks('#rgb(1,2,3)')).toBeFalsy();
  });
});

describe('validateTheme', () => {
  const nunjucksValue = '{{ nunjucks.4.lyfe }}';
  const name = 'mock-plugin';
  const displayName = 'Mock Plugin';
  const mockMessage = (path: string[]) => `[plugin] Nunjucks values in plugin themes are no longer valid. The plugin ${displayName} (${name}) has an invalid value, "${nunjucksValue}" at the path $.theme.${path.join('.')}`;

  vi.spyOn(console, 'error').mockImplementation(() => { });

  it('will validate rawCSS in the plugin theme', () => {
    const pluginTheme: PluginTheme = {
      name,
      displayName,
      theme: {
        rawCss: nunjucksValue,
      },
    };

    validateTheme(pluginTheme);

    const message = mockMessage(['rawCss']);
    expect(console.error).toHaveBeenLastCalledWith(message);
  });

  it('will validate top-level theme blocks in the plugin theme', () => {
    const pluginTheme: PluginTheme = {
      name,
      displayName,
      theme: {
        background: {
          default: nunjucksValue,
          info: '#abcdef',
        },
      },
    };

    validateTheme(pluginTheme);

    const message = mockMessage(['background', 'default']);
    expect(console.error).toHaveBeenLastCalledWith(message);
  });

  it('will validate styles sub-theme blocks in the plugin theme', () => {
    const pluginTheme: PluginTheme = {
      name,
      displayName,
      theme: {
        styles: {
          appHeader: {
            foreground: {
              default: nunjucksValue,
              info: '#abcdef',
            },
          },
        },
      },
    };

    validateTheme(pluginTheme);

    const message = mockMessage(['styles', 'appHeader', 'foreground', 'default']);
    expect(console.error).toHaveBeenLastCalledWith(message);
  });
});

describe('validateThemeName', () => {
  it('will return valid names as-is', () => {
    const name = 'default-dark';
    const validName = validateThemeName(name);
    expect(name).toEqual(validName);
  });

  it('will lowercase', () => {
    const name = 'Default-dark';
    const validName = validateThemeName(name);
    expect(name).not.toEqual(validName);
    expect(validName).toEqual('default-dark');
  });

  it('will replace spaces', () => {
    const name = 'default dark';
    const validName = validateThemeName(name);
    expect(name).not.toEqual(validName);
    expect(validName).toEqual('default-dark');
  });
});