Skip to Content
Octopus Energy Packages

Create Test Data

Use mock factories to test components that consume measurement primitives

The @octopus-energy/measurements package ships a dedicated testing entry point with mock factories for every core type. These factories produce realistic default values and accept partial overrides, making it straightforward to set up unit and integration tests for components that consume measurement data.

Each factory returns one piece of the measurements puzzle: timeframe state, preferences, chart data, or raw measurements. Combine them to build any test scenario without needing a live API or complex setup.

Testing entry point is test-only

Never import from the testing entry point in production code. It pulls in mock data and test-only utilities that will bloat your bundle.

Import from the testing entry point

All mock factories live under a single entry point. Import only what your test needs:

import {
  createMockTimeframe,
  createMockTimeframeActions,
  createMockPreferences,
  createMockChartData,
  createMockSummary,
  createMockMeasurements,
  createMockGraphQLClient,
  createMockStorageAdapter,
} from "@octopus-energy/measurements/testing";

Create mocks for your test

Each factory produces a typed object with sensible defaults. Pass partial overrides to customize for your scenario:

import {
  createMockChartData,
  createMockSummary,
  createMockTimeframe,
  createMockTimeframeActions,
  createMockPreferences,
} from "@octopus-energy/measurements/testing";

const timeframe = createMockTimeframe({ range: "week" });
const timeframeActions = createMockTimeframeActions();
const [preferences, preferencesActions] = createMockPreferences();
const transformed = createMockChartData({ count: 7, range: "week" });
const summary = createMockSummary();

Render and assert

Pass the mocks to your component under test. The factories return the same types your production code consumes, so props match exactly:

import { render, screen } from "@testing-library/react";

test("renders energy chart with mock data", () => {
  const timeframe = createMockTimeframe({ range: "week" });
  const timeframeActions = createMockTimeframeActions();
  const [preferences, preferencesActions] = createMockPreferences();
  const transformed = createMockChartData({ range: "week" });

  render(
    <EnergyChart
      transformed={transformed}
      timeframe={timeframe}
      timeframeActions={timeframeActions}
      preferences={preferences}
      preferencesActions={preferencesActions}
    />,
  );

  expect(screen.getByRole("figure")).toBeInTheDocument();
});

Testing components that accept a StorageAdapter

Some components accept a StorageAdapter prop to persist preferences or timeframe state. Pass createMockStorageAdapter when you want fully isolated in-memory storage without touching localStorage:

// @vitest-environment happy-dom
import { render, screen } from "@testing-library/react";
import {
  createMockMeasurements,
  createMockStorageAdapter,
  createMockTimeframe,
} from "@octopus-energy/measurements/testing";
import { MarketSupplyPointCard } from "./market-supply-point-card";

test("renders electricity card", () => {
  render(
    <MarketSupplyPointCard
      getPreferenceStorage={() => createMockStorageAdapter("measurements:preferences")}
      marketSupplyPointId="msp-1"
      measurements={createMockMeasurements(48, { interval: "30m" })}
      timeframe={createMockTimeframe({ range: "week" })}
      utility="electricity"
    />,
  );

  expect(screen.getByText(/electricity/i)).toBeInTheDocument();
});

Because each createMockStorageAdapter call returns a fresh isolated instance, tests never share state and no localStorage.clear() is required.

Next steps

  • Testing API: full documentation of every factory function, parameters, and return types
  • Timeframe: the production types that mock factories replicate
  • Overview: the chart data structures that createMockChartData produces

On this page