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:

plugin/plugin.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

Which we can define as:

plugin/plugin.test.ts
const network = [
{ id: "S1", type: "SupplySource" },
{
id: "V1", type: "Valve",
connections: ["S1", "T1"],
simulation: {
status: "OPEN"
}
},
{
id: "T1", type: "Tank"
},
{
id: "V2", type: "Valve",
connections: ["T1", "J1"],
simulation: {
status: "CLOSED"
}
},
{
id: "J1", type: "Junction"
}
];

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.

plugin/plugin.test.ts
import { mockSDK } from "@qatium/sdk-testing-library"
import { MyPlugin } from './plugin';
it("add heatmap layer on run", () => {
const sdk = mockSDK({ network })
// Mock the sdk used in the plugin
global.sdk = sdk;
const plugin = new MyPlugin();
plugin.run();
});

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

plugin/plugin.test.ts
import { mockSDK } from "@qatium/sdk-testing-library"
import { MyPlugin } from "./plugin"
it("add heatmap layer on run", () => {
const sdk = mockSDK({ network })
// Mock the sdk used in the plugin
global.sdk = sdk;
const plugin = new MyPlugin();
plugin.run();
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 to close valves. Any plugin event can be simulated on tests.

Ensure that valves are closed when the message is received:

plugin/plugin.test.ts
it("close valves on message", () => {
const sdk = mockSDK({ network })
// Mock the sdk used in the plugin
global.sdk = sdk;
const plugin = new MyPlugin();
plugin.onMessage({ 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");
});