Get started
Reference

Testing

Testing

Three packages publish a /testing entry with test doubles. None depends on a test runner: pass vi.fn or jest.fn where a spy is needed.

Pick an entry point

You are testingReach forFrom
A hook that drives a MapClientcreateMapClientStub, createMapClientSpy@mountaya/elements/testing
Code that uses MountayaMapcreateMountayaMapDouble@mountaya/elements/testing
Code that takes a StudioToolContextfakeViewer, fakeStudioCache, studioToolBus@mountaya/studio/testing
A component that renders recordsmakeItinerary, makeLineSegment, makeCollection@mountaya/studio-react/testing
A component that reads the viewerWithViewer, WithStudioHostRegistry@mountaya/studio-react/testing
A component that reads the APIWithMountayaClient@mountaya/studio-react/testing

Double the map client

A test with no WebGL cannot mount a real map. createMapClientStub is a complete MapClient that does nothing:

import { createMapClientStub } from "@mountaya/elements/testing";

const map = createMapClientStub({
  getViewport: () => ({ center: [6.8712, 45.9231], zoom: 13 }),
});

createMapClientSpy wraps every method with your spy, so you can assert which commands ran:

import { createMapClientSpy } from "@mountaya/elements/testing";
import { expect, test, vi } from "vitest";

test("frames the itinerary", () => {
  const map = createMapClientSpy(vi.fn);

  map.fitBounds([5.9, 45.8, 7.1, 46.5]);

  expect(map.fitBounds).toHaveBeenCalledWith([5.9, 45.8, 7.1, 46.5]);
});

Arrange a tool context

A viewer is a session, a cache is the host's query layer, and a bus is the thing under test:

import { fakeStudioCache, fakeViewer, studioToolBus } from "@mountaya/studio/testing";

const bus = studioToolBus();
const cache = fakeStudioCache();
const viewer = fakeViewer();

viewer.setEntitlements("video-export");

Build records shaped like the API's

The builders return complete records, so a test names only the fields it cares about:

import { makeItinerary, makeItineraryFixture, makeLineSegment } from "@mountaya/studio-react/testing";

const itinerary = makeItinerary({ id: "itn-1", name: "Tour du Mont-Blanc" });

const segment = makeLineSegment({
  color: "#e8590c",
  coordinates: [
    [6.8712, 45.9231, 1035],
    [6.9271, 45.9822, 1250],
  ],
  id: "seg-a",
  name: "Chamonix to Argentière",
});

// A whole itinerary, segments and waypoints included.
const detailed = makeItineraryFixture();

Render a component without a studio

useStudioViewer and useStudioHostRegistry throw outside a studio host, so a component test supplies the context:

import { viewerWith, WithViewer } from "@mountaya/studio-react/testing";
import { render } from "@testing-library/react";

render(
  <WithViewer viewer={viewerWith({ entitlements: ["video-export"] })}>
    <YourComponent />
  </WithViewer>,
);

Answer a component's reads

A component that reads the API throws outside a MountayaProvider. WithMountayaClient answers its requests from a table you pass, so no request leaves the machine:

import { WithMountayaClient } from "@mountaya/studio-react/testing";
import { render } from "@testing-library/react";

render(
  <WithMountayaClient
    answers={{
      "GET /v1/collections": {
        data: [],
        extensions: { metadata: { pagination: { limit: 50, next_cursor: null } } },
      },
      "PATCH /v1/collections/col-1": { body: { message: "Not yours." }, status: 403 },
    }}
    userId="user-1"
  >
    <YourComponent />
  </WithMountayaClient>,
);

Next steps

Last updated