Skip to content

Commit f0d85e0

Browse files
basic test for running and building react views (#32)
1 parent df129b5 commit f0d85e0

5 files changed

Lines changed: 152 additions & 1 deletion

File tree

‎.github/workflows/nodejs.yml‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This workflow installs saltcorn from npm and runs the plugin tests.
2+
3+
name: Node.js CI
4+
5+
on:
6+
push:
7+
branches: [main]
8+
pull_request:
9+
branches: [main]
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
timeout-minutes: 25
15+
services:
16+
# Label used to access the service container
17+
postgres:
18+
# Docker Hub image
19+
image: postgres
20+
# Provide the password for postgres
21+
env:
22+
POSTGRES_PASSWORD: postgres
23+
POSTGRES_DB: saltcorn_test
24+
# Set health checks to wait until postgres has started
25+
options: >-
26+
--health-cmd pg_isready
27+
--health-interval 10s
28+
--health-timeout 5s
29+
--health-retries 5
30+
ports:
31+
- 5432:5432
32+
steps:
33+
- uses: actions/checkout@v2
34+
- name: Use Node.js
35+
uses: actions/setup-node@v1
36+
with:
37+
node-version: "22.x"
38+
- run: npm install -g @saltcorn/cli@1.3.1-beta.11
39+
env:
40+
CI: true
41+
PGHOST: localhost
42+
PGUSER: postgres
43+
PGDATABASE: saltcorn_test
44+
PGPASSWORD: postgres
45+
- run: saltcorn info
46+
env:
47+
CI: true
48+
PGHOST: localhost
49+
PGUSER: postgres
50+
PGDATABASE: saltcorn_test
51+
PGPASSWORD: postgres
52+
- run: saltcorn add-schema -f
53+
env:
54+
CI: true
55+
PGHOST: localhost
56+
PGUSER: postgres
57+
PGDATABASE: saltcorn_test
58+
PGPASSWORD: postgres
59+
- run: saltcorn dev:plugin-test -d $PWD -f backup.zip
60+
env:
61+
CI: true
62+
PGHOST: localhost
63+
PGUSER: postgres
64+
PGDATABASE: saltcorn_test
65+
PGPASSWORD: postgres

‎package.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"build_view_dev": "webpack --mode development --config user-code/webpack.config.js",
99
"build_main": "webpack --mode production --config main-code/webpack.config.js",
1010
"build_main_dev": "webpack --mode development --config main-code/webpack.config.js",
11-
"eslint": "eslint ./app-code"
11+
"eslint": "eslint ./app-code",
12+
"test": "jest tests --runInBand"
1213
},
1314
"dependencies": {
1415
"@saltcorn/markup": "^0.2.0",
@@ -29,6 +30,9 @@
2930
"style-loader": "4.0.0",
3031
"css-loader": "7.1.2"
3132
},
33+
"devDependencies": {
34+
"jest": "^29.7.0"
35+
},
3236
"author": "Christian Hugo",
3337
"license": "MIT",
3438
"repository": "github:saltcorn/react",

‎tests/backup.zip‎

16.3 KB
Binary file not shown.

‎tests/build.test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const { getState } = require("@saltcorn/data/db/state");
2+
const View = require("@saltcorn/data/models/view");
3+
const { mockReqRes } = require("@saltcorn/data/tests/mocks");
4+
const { afterAll, beforeAll, describe, it, expect } = require("@jest/globals");
5+
6+
getState().registerPlugin("base", require("@saltcorn/data/base-plugin"));
7+
getState().registerPlugin("@saltcorn/react", require(".."));
8+
9+
afterAll(require("@saltcorn/data/db").close);
10+
beforeAll(async () => {
11+
await getState().refresh(true);
12+
});
13+
14+
describe("react view build tests", () => {
15+
it("run tableless view", async () => {
16+
const view = View.findOne({ name: "empty_react_view" });
17+
const body = {
18+
user_code: `
19+
import React from "react";
20+
export default function App({ viewName, query }) {
21+
return <h3>Please write your React code here</h3>;
22+
};`,
23+
build_mode: "development",
24+
timestamp: undefined,
25+
};
26+
await view.runRoute(
27+
"build_user_code",
28+
body,
29+
mockReqRes.res,
30+
{ req: { ...mockReqRes.req, body }, res: mockReqRes.res },
31+
false,
32+
);
33+
const stored = mockReqRes.getStored();
34+
expect(stored).toBeDefined();
35+
expect(stored).toEqual({ json: { notify_success: "Build successful" } });
36+
});
37+
});

‎tests/view.test.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const { getState } = require("@saltcorn/data/db/state");
2+
const View = require("@saltcorn/data/models/view");
3+
const { mockReqRes } = require("@saltcorn/data/tests/mocks");
4+
const { afterAll, beforeAll, describe, it, expect } = require("@jest/globals");
5+
6+
getState().registerPlugin("base", require("@saltcorn/data/base-plugin"));
7+
getState().registerPlugin("@saltcorn/react", require(".."));
8+
9+
afterAll(require("@saltcorn/data/db").close);
10+
beforeAll(async () => {
11+
await getState().refresh(true);
12+
});
13+
14+
describe("react view run tests", () => {
15+
it("run tableless view", async () => {
16+
const view = View.findOne({ name: "default_react_view" });
17+
const result = await view.run({}, mockReqRes);
18+
expect(result).toBeDefined();
19+
expect(result).toContain('<div class="_sc_react-view"');
20+
expect(result).toContain('view-name="default_react_view"');
21+
expect(result).toContain('query=\"%7B%7D\"');
22+
expect(result).toContain(
23+
'user=\"%7B%22id%22%3A1%2C%22role_id%22%3A1%2C%22attributes%22%3A%7B%7D%7D\"',
24+
);
25+
expect(result).toContain('timestamp="');
26+
});
27+
28+
it("run view with table", async () => {
29+
const view = View.findOne({ name: "react_view_with_data" });
30+
const result = await view.run({}, mockReqRes);
31+
expect(result).toBeDefined();
32+
expect(result).toContain('<div class="_sc_react-view"');
33+
expect(result).toContain('view-name="react_view_with_data"');
34+
expect(result).toContain('query=\"%7B%7D\"');
35+
expect(result).toContain('table-name="albums"');
36+
expect(result).toContain('state=\"%7B%7D\"');
37+
expect(result).toContain(
38+
'rows=\"%5B%7B%22id%22%3A1%2C%22name%22%3A%22album%20A%22%2C%22release_date%22%3A%222025-08-03T17%3A08%3A00.000Z%22%7D%2C%7B%22id%22%3A2%2C%22name%22%3A%22album%20B%22%2C%22release_date%22%3A%222025-08-27T17%3A08%3A00.000Z%22%7D%5D\"',
39+
);
40+
expect(result).toContain(
41+
'user=\"%7B%22id%22%3A1%2C%22role_id%22%3A1%2C%22attributes%22%3A%7B%7D%7D\"',
42+
);
43+
expect(result).toContain('timestamp="');
44+
});
45+
});

0 commit comments

Comments
 (0)