-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathbrowser.test.ts
More file actions
101 lines (85 loc) · 3.6 KB
/
browser.test.ts
File metadata and controls
101 lines (85 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { describe, test, expect, beforeAll } from "vitest"
import { Herb, HerbBackend } from "../src"
describe("@herb-tools/browser", () => {
beforeAll(async () => {
await Herb.load()
})
test("loads wasm successfully", () => {
expect(Herb).toBeDefined()
})
test("Herb export is of instance HerbBackend", () => {
expect(Herb instanceof HerbBackend).toBeTruthy()
})
test("version() returns a string", async () => {
const version = Herb.version
expect(typeof version).toBe("string")
expect(version).toBe("@herb-tools/browser@0.9.1, @herb-tools/core@0.9.1, libprism@1.9.0, libherb@0.9.1 (WebAssembly)")
})
test("parse() can process a simple template", async () => {
const simpleHtml = '<div><%= "Hello World" %></div>'
const result = Herb.parse(simpleHtml)
expect(result).toBeDefined()
expect(result.value).toBeDefined()
expect(result.source).toBeDefined()
expect(result.errors).toHaveLength(0)
expect(result.warnings).toHaveLength(0)
})
test("extractRuby() extracts embedded Ruby code", async () => {
const simpleHtml = '<div><%= "Hello World" %></div>'
const ruby = Herb.extractRuby(simpleHtml)
expect(ruby).toBeDefined()
expect(ruby).toBe(' "Hello World" ; ')
})
test("extractHTML() extracts HTML content", async () => {
const simpleHtml = '<div><%= "Hello World" %></div>'
const html = Herb.extractHTML(simpleHtml)
expect(html).toBeDefined()
expect(html).toBe("<div> </div>")
})
test("parse and transform erb if node", async () => {
const erb = "<% if true %>true<% end %>"
const result = Herb.parse(erb)
expect(result).toBeDefined()
expect(result.value).toBeDefined()
expect(result.value.inspect()).toContain(
"@ ERBIfNode (location: (1:0)-(1:26))",
)
expect(result.value.inspect()).toContain(
"@ ERBEndNode (location: (1:17)-(1:26))",
)
})
test("parse() without track_whitespace option ignores whitespace", async () => {
const htmlWithWhitespace = '<div class="example">content</div>'
const result = Herb.parse(htmlWithWhitespace)
expect(result).toBeDefined()
expect(result.value).toBeDefined()
expect(result.errors).toHaveLength(0)
expect(result.value.inspect()).not.toContain("@ WhitespaceNode")
})
test("parse() with track_whitespace: false ignores whitespace", async () => {
const htmlWithWhitespace = '<div class="example">content</div>'
const result = Herb.parse(htmlWithWhitespace, { track_whitespace: false })
expect(result).toBeDefined()
expect(result.value).toBeDefined()
expect(result.errors).toHaveLength(0)
expect(result.value.inspect()).not.toContain("@ WhitespaceNode")
})
test("parse() with track_whitespace: true tracks whitespace", async () => {
const htmlWithWhitespace = '<div class="example">content</div>'
const result = Herb.parse(htmlWithWhitespace, { track_whitespace: true })
expect(result).toBeDefined()
expect(result.value).toBeDefined()
expect(result.errors).toHaveLength(0)
expect(result.value.inspect()).toContain("@ WhitespaceNode")
expect(result.value.inspect()).toContain('" "')
})
test("parse() with track_whitespace tracks whitespace in close tags", async () => {
const htmlWithWhitespace = '<div>content</div >'
const result = Herb.parse(htmlWithWhitespace, { track_whitespace: true })
expect(result).toBeDefined()
expect(result.value).toBeDefined()
expect(result.errors).toHaveLength(0)
expect(result.value.inspect()).toContain("@ WhitespaceNode")
expect(result.value.inspect()).toContain('" "')
})
})