sitevision-mock
Jest mock for Sitevision Webapps / Restapps (Server side Javascript). Works with both Webapps 1 and 2.
Prerequisites
Only applies if you are building the applications in the old / legacy way
- Your Module Server Javascript code must be written in ES6-format in order to use this mock, see more here how to get started: https://github.com/OlofFredriksson/babel-plugin-transform-sitevision-apps
More information regarding the "bundled app" concept, please read on the following link: https://developer.sitevision.se/archives/developer-news/developer-news/2021-06-10-bundled-apps
Getting started
npm install --save-dev sitevision-mock
Load all (recommended)
In your jest.config.js
, add following:
{
"moduleNameMapper": {
...require("sitevision-mock")
}
}
Examples
See the tests in integration/ if you need some inspiration how to write test cases with this mock.
Transformers
The Sitevision API sometimes returns Java Objects, List & Calendar for example. This repo also contains transformers which could help you adding mock data.
Helpfull to use if you for example need to mock PropertyUtil values.
// Your Test file
import List from "sitevision-mock/dist/transformers/List";
...
const node = {
yourProperty: new List([1, 2, 3]),
};
Mock Sitevision Node
There is helper to generate a mocked Sitevision Node, with some of the properties and functions.
import { mockNode } from "sitevision-mock/dist/mockNode";
node = mockNode({
uuid: "myUUID",
});
node.getIdentifier(); // => myUUID
Typescript
sitevision-mock supports webapps written in Typescript. However, due to limitations with the Sitevision Typings file, you cant setMockData directly on the imported members. See workaround:
Working
import { setMockData } from "sitevision-mock/dist/setMockData";
setMockData("PortletContextUtil", {
getCurrentPage: { key2: "/URI" },
});
Not Working
import portletContextUtil from "PortletContextUtil";
portletContextUtil.__setMockData({
getCurrentPage: { key1: "/URI" },
});
If you use Javascript both ways work.
Why using Sitevision mock?
If you write unit tests with Jest, you should be able to focus on writing tests on the functions and logic in your code, not spend time on writing mock functions for the Sitevision API.
Imported classes will get wrapped in a jest object, so you can mock the return value or check if functions have been called.
References Sitevision API
- https://developer.sitevision.se/webdav/files/apidocs/overview-summary.html
- https://developer.sitevision.se/docs/webapps
Notice
This project is made during my spare time and since I dont have access to any Sitevision server its quite hard at the moment to show a good template project where this mock, and the babel-transform-plugin is used.
In a perfect world, maybe this mock is something that in the future should be generated by Sitevision themself? Much easier than doing reverse engineering. :-) (But less fun?)