Skip to content
We're currently creating a lot of content. Sign up to get notified when it's ready.

Test your plugin

This guide will help you test your plugin effectively using Qatium’s SDK Testing Library. We will test the plugin created in the previous section.

By following these steps, you can ensure that your plugin works correctly within the Qatium ecosystem.

Step 1: Setting up your development environment

Install Dependencies

Start by installing Qatium SDK Testing Library as development dependency:

Terminal window
npm install -D @qatium/sdk-testing-library

Run Tests

Let’s add one dummy test, for example:

engine/engine.test.ts
it("first test", () => {
expect(true).toBe(true)
});

you can run tests with:

Terminal window
npm run test

or enter in watch mode:

Terminal window
npm run test:watch

Step 2: Adding tests

Now, let’s make sure your plugin behaves as expected by adding tests.

Create a Test network

Begin by defining a network to use in your tests, we will use:

Example network #1

You can utilize the helper functions provided by the testing library to create it:

engine/engine.test.ts
import { aJunction, aSupplySource, aTank, aValve } from "@qatium/sdk-testing-library"
const buildNetwork = () => [
aSupplySource({ id: "S1" }),
aValve({
id: "V1",
connections: ["S1", "T1"],
simulation: {
status: "OPEN"
}
}),
aTank({ id: "T1" }),
aValve({
id: "V2",
connections: ["T1", "J1"],
simulation: {
status: "CLOSED"
}
}),
aJunction({ id: "J1" }),
];

More information about building networks or using example networks is available in Building test networks.

Check Heatmap layer is added

When running, your plugin should add a Heatmap layer on top of closed valves, let’s verify it.

First, create an SDK mock using our test network, with it, we can instantiate the plugin and run it.

engine/engine.test.ts
import { mockSDK } from "@qatium/sdk-testing-library"
import { Engine } from './engine';
it("add heatmap layer on run", () => {
const sdk = mockSDK({ network: buildNetwork() })
const pluginEngine = new Engine();
pluginEngine.run(sdk);
});

Verify the plugin called sdk.map.addOverlay with the Heatmap layer.

engine/engine.test.ts
import { mockSDK } from "@qatium/sdk-testing-library"
it("add heatmap layer on run", () => {
const sdk = mockSDK({ network: buildNetwork() })
const pluginEngine = new Engine();
pluginEngine.run(sdk);
expect(sdk.map.addOverlay).toHaveBeenCalledWith([
expect.objectContaining({
data: [{
"geometry": {
"coordinates": [0, 0],
"type": "Point"
},
"properties": {},
"type": "Feature"
}],
type: "HeatmapLayer"
})
])
});

Later, you can learn more about mocking the SDK.

Check Valves are closed

A message is sent from the UI to the plugin engine to close valves. Any plugin event can be simulated on tests.

Ensure that valves are closed when the message is received:

engine/engine.test.ts
it("close valves on message", () => {
const sdk = mockSDK({ network: buildNetwork() })
const pluginEngine = new Engine();
pluginEngine.onMessage(sdk, { command: "closeValves", data: 1 });
// You can check the network state
const valve = sdk.network.getAsset("V1") as Valve
expect(valve.simulation?.status).toEqual("CLOSED")
// Or see which functions were called
expect(sdk.network.setStatus).toHaveBeenCalledTimes(1);
expect(sdk.network.setStatus).toHaveBeenCalledWith("V1", "CLOSED");
});