Skip to content

Commit a37e840

Browse files
feat: add tests for rendering table views
1 parent 3c1537e commit a37e840

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

jest.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
module.exports = {
22
transform: {
33
"^.+\\.ts?$": "ts-jest",
4+
"^.+\\markdown-table.js?$": "ts-jest",
5+
},
6+
globals: {
7+
'ts-jest': {
8+
isolatedModules: true
9+
}
410
},
511
testEnvironment: "node",
612
testRegex: "./src/.*\\.(test|spec)?\\.(ts|ts)$",

src/DatabaseViewRenderer.spec.ts

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { DatabaseViewRenderer } from "./DatabaseViewRenderer";
2+
import { RenderDatabaseEntryTask } from "./RenderDatabaseEntryTask";
3+
4+
describe("DatabaseViewRenderer", () => {
5+
test("renders with grouping", async () => {
6+
const linkRenderer = {};
7+
const sut = new DatabaseViewRenderer(linkRenderer as any);
8+
9+
const entries: RenderDatabaseEntryTask[] = [
10+
{
11+
properties: {
12+
meta: { id: "a", title: " A", url: "http://a" },
13+
properties: new Map([
14+
["Name", "A"],
15+
["Foo", "Bar"],
16+
]),
17+
},
18+
},
19+
{
20+
properties: {
21+
meta: { id: "b", title: " B", url: "http://b" },
22+
properties: new Map([
23+
["Name", "B"],
24+
["Foo", "Baz"],
25+
]),
26+
},
27+
},
28+
];
29+
30+
const result = sut.renderViews(entries, {
31+
entries: { emitToIndex: false },
32+
renderAs: "table",
33+
views: [
34+
{
35+
title: "By Foo",
36+
properties: {
37+
groupBy: "Foo",
38+
},
39+
},
40+
],
41+
});
42+
43+
const expected = `## By Foo - Bar
44+
45+
| Name | Foo |
46+
| ---- | --- |
47+
| A | Bar |
48+
49+
## By Foo - Baz
50+
51+
| Name | Foo |
52+
| ---- | --- |
53+
| B | Baz |`;
54+
expect(result).toEqual(expected);
55+
});
56+
57+
test("filters columns", async () => {
58+
const linkRenderer = {};
59+
const sut = new DatabaseViewRenderer(linkRenderer as any);
60+
61+
const entries: RenderDatabaseEntryTask[] = [
62+
{
63+
properties: {
64+
meta: { id: "a", title: " A", url: "http://a" },
65+
properties: new Map([
66+
["Name", "A"],
67+
["Foo", "Bar"],
68+
["Alice", "Bob"],
69+
]),
70+
},
71+
},
72+
];
73+
74+
const result = sut.renderViews(entries, {
75+
entries: { emitToIndex: false },
76+
renderAs: "table",
77+
views: [
78+
{
79+
title: "By Foo",
80+
properties: {
81+
include: ["Name", "Foo"],
82+
},
83+
},
84+
],
85+
});
86+
87+
const expected = `## By Foo
88+
89+
| Name | Foo |
90+
| ---- | --- |
91+
| A | Bar |`;
92+
expect(result).toEqual(expected);
93+
});
94+
});

src/DatabaseViewRenderer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export class DatabaseViewRenderer {
6868

6969
const tableMd = markdownTable.markdownTable(table);
7070
if (view.title) {
71-
return `## ${view.title} - ${titleAppendix}\n\n` + tableMd;
71+
const formattedTitle = [view.title, titleAppendix].filter(x => !!x).join(" - ");
72+
return `## ${formattedTitle}\n\n` + tableMd;
7273
} else {
7374
return tableMd;
7475
}

0 commit comments

Comments
 (0)