-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRichText.test.js
136 lines (123 loc) · 4.03 KB
/
RichText.test.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const { RichTextList } = require("./RichText");
const MOCK_MENTION_RICH_TEXT_LIST = [
{
type: "mention",
mention: {
type: "page",
page: {
id: "68cec333-ac2a-4044-b720-9cd742f8fbd0",
},
},
annotations: {
bold: false,
italic: false,
strikethrough: false,
underline: false,
code: false,
color: "default",
},
plain_text: "How notioncms.dev is built with Eleventy and Notion CMS",
href: "https://www.notion.so/68cec333ac2a4044b7209cd742f8fbd0",
},
{
type: "mention",
mention: {
type: "database",
database: {
id: "10b45c7b-7eac-44bb-8d7f-d8bb12deb998",
},
},
annotations: {
bold: false,
italic: false,
strikethrough: false,
underline: false,
code: false,
color: "default",
},
plain_text: "Guides",
href: "https://www.notion.so/10b45c7b7eac44bb8d7fd8bb12deb998",
},
{
type: "mention",
mention: {
type: "user",
user: {
object: "user",
id: "309c630b-2c5a-48fa-9a2f-9540f32499a2",
name: "Lance Chen",
avatar_url:
"https://lh6.googleusercontent.com/-KRqNUOaK6vk/AAAAAAAAAAI/AAAAAAAAAPM/Lv5TcqwJY3o/photo.jpg?sz=50",
type: "person",
person: {
email: "[email protected]",
},
},
},
annotations: {
bold: false,
italic: false,
strikethrough: false,
underline: false,
code: false,
color: "default",
},
plain_text: "@Lance Chen",
href: null,
},
];
test("should trigger mentionResolver callback", async () => {
const mentionResolverCallback = jest.fn();
const mentionResolver = require("./MentionResolver.js");
mentionResolver.setResolver(mentionResolverCallback);
const result = new RichTextList(MOCK_MENTION_RICH_TEXT_LIST).toHTML();
expect(mentionResolverCallback).toHaveBeenCalledTimes(3);
expect(result).toEqual(
"How notioncms.dev is built with Eleventy and Notion CMSGuides@Lance Chen"
);
});
test("should resolve to <a> tag", async () => {
const mentionResolverCallback = jest.fn((richText) => {
if (
richText.type === "mention" &&
richText.mention.type === "page" &&
richText.mention.page.id === "68cec333-ac2a-4044-b720-9cd742f8fbd0"
) {
return {
type: "url",
url: "https://notioncms.dev/guides/how-notioncms.dev-is-built-with-eleventy-and-notion-cms/",
};
}
});
const mentionResolver = require("./MentionResolver.js");
mentionResolver.setResolver(mentionResolverCallback);
const result = new RichTextList(MOCK_MENTION_RICH_TEXT_LIST).toHTML();
expect(mentionResolverCallback).toHaveBeenCalledTimes(3);
expect(result).toEqual(
'<a href="https://notioncms.dev/guides/how-notioncms.dev-is-built-with-eleventy-and-notion-cms/">How notioncms.dev is built with Eleventy and Notion CMS</a>Guides@Lance Chen'
);
});
test("should resolve to raw HTML", async () => {
// NOTE: Twitter actually generates the script tag with the async attribute set as just `async`, it is changed to `async=""` since cheerio makes it so.
const TWITTER_FOLLOW_BUTTON =
'<a href="https://twitter.com/lancechentw?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @lancechentw</a><script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>';
const mentionResolverCallback = jest.fn((richText) => {
if (
richText.type === "mention" &&
richText.mention.type === "user" &&
richText.mention.user.id === "309c630b-2c5a-48fa-9a2f-9540f32499a2"
) {
return {
type: "html",
html: TWITTER_FOLLOW_BUTTON,
};
}
});
const mentionResolver = require("./MentionResolver.js");
mentionResolver.setResolver(mentionResolverCallback);
const result = new RichTextList(MOCK_MENTION_RICH_TEXT_LIST).toHTML();
expect(mentionResolverCallback).toHaveBeenCalledTimes(3);
expect(result).toEqual(
`How notioncms.dev is built with Eleventy and Notion CMSGuides${TWITTER_FOLLOW_BUTTON}`
);
});