Skip to content

Commit 221aeb4

Browse files
committed
Add vitest for JavaScript unit testing
Set up vitest with jsdom environment for testing JavaScript modules. Add unit tests for Configuration class and EditorConfiguration. Update GitHub Actions and local CI to run JS tests.
1 parent 59fb650 commit 221aeb4

File tree

7 files changed

+914
-9
lines changed

7 files changed

+914
-9
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ jobs:
5555
ruby-version: ruby-3.3.7
5656
bundler-cache: true
5757

58-
- name: Run tests
58+
- name: Set up Node.js
59+
uses: actions/setup-node@v6
60+
with:
61+
node-version: '20'
62+
cache: 'yarn'
63+
64+
- name: Install JavaScript dependencies
65+
run: yarn install --frozen-lockfile
66+
67+
- name: Run JavaScript tests
68+
run: yarn test
69+
70+
- name: Run Rails tests
5971
env:
6072
RAILS_ENV: test
6173
# REDIS_URL: redis://localhost:6379/0

config/ci.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
step "Style: Ruby", "bin/rubocop -f github"
55
step "Style: JavaScript", "yarn lint"
66

7-
step "Tests: prepare", "env RAILS_ENV=test bin/rails db:test:prepare"
8-
step "Tests: run", "env RAILS_ENV=test bin/rails test:all -d"
7+
step "Tests: JavaScript", "yarn test"
8+
step "Tests: Rails (prepare)", "env RAILS_ENV=test bin/rails db:test:prepare"
9+
step "Tests: Rails (run)", "env RAILS_ENV=test bin/rails test:all -d"
910

1011
unless success?
1112
failure "Signoff: CI failed.", "Fix the issues and try again."

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
"@rollup/plugin-node-resolve": "^16.0.1",
1919
"@rollup/plugin-terser": "^0.4.4",
2020
"eslint": "^9.15.0",
21+
"jsdom": "^27.3.0",
2122
"rollup": "^4.44.1",
2223
"rollup-plugin-copy": "^3.5.0",
23-
"rollup-plugin-gzip": "^4.1.1"
24+
"rollup-plugin-gzip": "^4.1.1",
25+
"vitest": "^4.0.16"
2426
},
2527
"scripts": {
2628
"build": "rollup -c",
2729
"build:npm": "rollup -c rollup.config.npm.mjs",
2830
"watch": "rollup -wc --watch.onEnd=\"rails restart\"",
2931
"lint": "eslint",
32+
"test": "vitest --environment=jsdom",
3033
"prerelease": "yarn build:npm",
3134
"release": "yarn build:npm && yarn publish"
3235
},
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, test } from "vitest"
2+
import Configuration from "../../../src/config/configuration"
3+
4+
const config = new Configuration({
5+
default: { richText: true },
6+
})
7+
8+
test("gets path", () => {
9+
expect(config.get("default.richText")).toBe(true)
10+
})
11+
12+
test("returns undefined for missing option", () => {
13+
expect(config.get("default.missing")).toBe(undefined)
14+
})
15+
16+
test("overwrites bool with hash", () => {
17+
const config = new Configuration(
18+
{ toolbar: false },
19+
{ toolbar: { bold: true } }
20+
)
21+
expect(config.get("toolbar.bold")).toBe(true)
22+
})
23+
24+
test("ovewrites hash with bool", () => {
25+
const config = new Configuration(
26+
{ toolbar: { bold: true } },
27+
{ toolbar: false }
28+
)
29+
expect(config.get("toolbar")).toBe(false)
30+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect, test } from "vitest"
2+
import { createElement } from "../helpers/dom_helper"
3+
import EditorConfiguration from "../../../src/editor/configuration"
4+
import { configure } from "../../../src/index"
5+
6+
configure({
7+
default: {
8+
richText: true,
9+
attachments: true
10+
},
11+
simple: {
12+
richText: false
13+
}
14+
})
15+
16+
test("uses defaults", () => {
17+
const element = createElement("<lexxy-editor></lexxy-editor>")
18+
const config = new EditorConfiguration(element)
19+
expect(config.get("attachments")).toBe(true)
20+
})
21+
22+
test("uses preset", () => {
23+
const element = createElement("<lexxy-editor preset='simple'></lexxy-editor>")
24+
const config = new EditorConfiguration(element)
25+
expect(config.get("richText")).toBe(false)
26+
})
27+
28+
test("preset fallbacks to default", () => {
29+
const element = createElement("<lexxy-editor preset='simple'></lexxy-editor>")
30+
const config = new EditorConfiguration(element)
31+
expect(config.get("attachments")).toBe(true)
32+
})
33+
34+
test("overrides defaults", () => {
35+
const element = createElement("<lexxy-editor rich-text='false'></lexxy-editor>")
36+
const config = new EditorConfiguration(element)
37+
expect(config.get("richText")).toBe(false)
38+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function createElement(html) {
2+
const element = document.createElement("div")
3+
element.innerHTML = html
4+
return element.firstChild;
5+
}

0 commit comments

Comments
 (0)