Skip to content

Commit af42edb

Browse files
committed
more tests!
1 parent 1fc5c8c commit af42edb

File tree

5 files changed

+584
-2
lines changed

5 files changed

+584
-2
lines changed
+357
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
import type { NeovimClient, Buffer } from "neovim";
2+
import { extractMountTree, NeovimTestHelper } from "../../test/preamble.js";
3+
import { d, mountView } from "./view.js";
4+
import * as assert from "assert";
5+
import { test } from "node:test";
6+
7+
await test.describe("tea/render.spec.ts", async () => {
8+
let helper: NeovimTestHelper;
9+
let nvim: NeovimClient;
10+
let buffer: Buffer;
11+
12+
test.before(() => {
13+
helper = new NeovimTestHelper();
14+
});
15+
16+
test.beforeEach(async () => {
17+
nvim = await helper.startNvim();
18+
buffer = (await nvim.createBuffer(false, true)) as Buffer;
19+
await buffer.setOption("modifiable", false);
20+
});
21+
22+
test.afterEach(() => {
23+
helper.stopNvim();
24+
});
25+
26+
await test("rendering empty string", async () => {
27+
const view = () => d`1${""}2`;
28+
const mountedView = await mountView({
29+
view,
30+
props: {},
31+
mount: {
32+
nvim,
33+
buffer,
34+
startPos: { row: 0, col: 0 },
35+
endPos: { row: 0, col: 0 },
36+
},
37+
});
38+
39+
const lines = await buffer.getLines({
40+
start: 0,
41+
end: 1,
42+
strictIndexing: false,
43+
});
44+
45+
assert.equal(lines[0], "12");
46+
47+
assert.deepStrictEqual(
48+
await extractMountTree(mountedView._getMountedNode()),
49+
{
50+
type: "node",
51+
startPos: {
52+
row: 0,
53+
col: 0,
54+
},
55+
endPos: {
56+
row: 0,
57+
col: 2,
58+
},
59+
children: [
60+
{
61+
content: "1",
62+
startPos: {
63+
col: 0,
64+
row: 0,
65+
},
66+
endPos: {
67+
col: 1,
68+
row: 0,
69+
},
70+
type: "string",
71+
},
72+
{
73+
content: "",
74+
startPos: {
75+
col: 1,
76+
row: 0,
77+
},
78+
endPos: {
79+
col: 1,
80+
row: 0,
81+
},
82+
type: "string",
83+
},
84+
{
85+
content: "2",
86+
startPos: {
87+
col: 1,
88+
row: 0,
89+
},
90+
endPos: {
91+
col: 2,
92+
row: 0,
93+
},
94+
type: "string",
95+
},
96+
],
97+
},
98+
);
99+
});
100+
101+
await test("rendering multi-line interpolation", async () => {
102+
const multiLineValue = `first line
103+
second line
104+
third line`;
105+
const view = () => d`before${multiLineValue}after`;
106+
const mountedView = await mountView({
107+
view,
108+
props: {},
109+
mount: {
110+
nvim,
111+
buffer,
112+
startPos: { row: 0, col: 0 },
113+
endPos: { row: 0, col: 0 },
114+
},
115+
});
116+
117+
const lines = await buffer.getLines({
118+
start: 0,
119+
end: 4,
120+
strictIndexing: false,
121+
});
122+
123+
assert.deepStrictEqual(lines, [
124+
"beforefirst line",
125+
"second line",
126+
"third lineafter",
127+
]);
128+
129+
assert.deepStrictEqual(
130+
await extractMountTree(mountedView._getMountedNode()),
131+
{
132+
type: "node",
133+
startPos: {
134+
row: 0,
135+
col: 0,
136+
},
137+
endPos: {
138+
row: 2,
139+
col: 15,
140+
},
141+
children: [
142+
{
143+
content: "before",
144+
startPos: {
145+
col: 0,
146+
row: 0,
147+
},
148+
endPos: {
149+
col: 6,
150+
row: 0,
151+
},
152+
type: "string",
153+
},
154+
{
155+
content: "first line\nsecond line\nthird line",
156+
startPos: {
157+
row: 0,
158+
col: 6,
159+
},
160+
endPos: {
161+
row: 2,
162+
col: 10,
163+
},
164+
type: "string",
165+
},
166+
{
167+
content: "after",
168+
startPos: {
169+
row: 2,
170+
col: 10,
171+
},
172+
endPos: {
173+
row: 2,
174+
col: 15,
175+
},
176+
type: "string",
177+
},
178+
],
179+
},
180+
);
181+
});
182+
183+
await test("rendering multi-line template with interpolation", async () => {
184+
const name = "world";
185+
const view = () => d`
186+
Hello
187+
${name}
188+
Goodbye
189+
`;
190+
const mountedView = await mountView({
191+
view,
192+
props: {},
193+
mount: {
194+
nvim,
195+
buffer,
196+
startPos: { row: 0, col: 0 },
197+
endPos: { row: 0, col: 0 },
198+
},
199+
});
200+
201+
const lines = await buffer.getLines({
202+
start: 0,
203+
end: 5,
204+
strictIndexing: false,
205+
});
206+
207+
assert.deepStrictEqual(lines, [
208+
"",
209+
" Hello",
210+
" world",
211+
" Goodbye",
212+
" ",
213+
]);
214+
215+
assert.deepStrictEqual(
216+
await extractMountTree(mountedView._getMountedNode()),
217+
{
218+
type: "node",
219+
startPos: {
220+
row: 0,
221+
col: 0,
222+
},
223+
endPos: {
224+
row: 4,
225+
col: 4,
226+
},
227+
children: [
228+
{
229+
content: "\n Hello\n ",
230+
startPos: {
231+
col: 0,
232+
row: 0,
233+
},
234+
endPos: {
235+
col: 8,
236+
row: 2,
237+
},
238+
type: "string",
239+
},
240+
{
241+
content: "world",
242+
startPos: {
243+
col: 8,
244+
row: 2,
245+
},
246+
endPos: {
247+
col: 13,
248+
row: 2,
249+
},
250+
type: "string",
251+
},
252+
{
253+
content: "\n Goodbye\n ",
254+
startPos: {
255+
col: 13,
256+
row: 2,
257+
},
258+
endPos: {
259+
col: 4,
260+
row: 4,
261+
},
262+
type: "string",
263+
},
264+
],
265+
},
266+
);
267+
});
268+
269+
await test("rendering nested interpolation", async () => {
270+
const inner = d`(inner)`;
271+
const view = () => d`outer${inner}end`;
272+
const mountedView = await mountView({
273+
view,
274+
props: {},
275+
mount: {
276+
nvim,
277+
buffer,
278+
startPos: { row: 0, col: 0 },
279+
endPos: { row: 0, col: 0 },
280+
},
281+
});
282+
283+
const lines = await buffer.getLines({
284+
start: 0,
285+
end: 1,
286+
strictIndexing: false,
287+
});
288+
289+
assert.equal(lines[0], "outer(inner)end");
290+
291+
assert.deepStrictEqual(
292+
await extractMountTree(mountedView._getMountedNode()),
293+
{
294+
type: "node",
295+
startPos: {
296+
row: 0,
297+
col: 0,
298+
},
299+
endPos: {
300+
row: 0,
301+
col: 15,
302+
},
303+
children: [
304+
{
305+
content: "outer",
306+
startPos: {
307+
row: 0,
308+
col: 0,
309+
},
310+
endPos: {
311+
row: 0,
312+
col: 5,
313+
},
314+
type: "string",
315+
},
316+
{
317+
type: "node",
318+
startPos: {
319+
row: 0,
320+
col: 5,
321+
},
322+
endPos: {
323+
row: 0,
324+
col: 12,
325+
},
326+
children: [
327+
{
328+
content: "(inner)",
329+
startPos: {
330+
row: 0,
331+
col: 5,
332+
},
333+
endPos: {
334+
row: 0,
335+
col: 12,
336+
},
337+
type: "string",
338+
},
339+
],
340+
},
341+
{
342+
content: "end",
343+
startPos: {
344+
col: 12,
345+
row: 0,
346+
},
347+
endPos: {
348+
col: 15,
349+
row: 0,
350+
},
351+
type: "string",
352+
},
353+
],
354+
},
355+
);
356+
});
357+
});

0 commit comments

Comments
 (0)