Skip to content

Commit e9edc94

Browse files
Merge pull request #16 from moveyourdigital/improve-testing-suite
Improve testing suite
2 parents ffee96d + c749fd3 commit e9edc94

File tree

7 files changed

+213
-40
lines changed

7 files changed

+213
-40
lines changed

README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,41 @@ However, each renderer supports a set of props, including `className` which can
6767
className: "language-js"
6868
},
6969
delimiter: {
70-
className: "article-hr"
70+
className: "border border-2 w-16 mx-auto"
7171
},
7272
embed: {
73-
className: "styled-iframe"
73+
className: "border-0"
7474
},
7575
header: {
76-
className: "lead"
76+
className: "font-bold"
7777
},
7878
image: {
79-
className: "img-fluid"
79+
className: "w-full max-w-screen-md",
80+
actionsClassNames: {
81+
stretched: "w-full h-80 object-cover",
82+
withBorder: "border border-2",
83+
withBackground: "p-2",
84+
}
8085
},
8186
list: {
82-
className: "unstyled-list"
87+
className: "list-inside"
8388
},
8489
paragraph: {
85-
className: "lead"
90+
className: "text-base text-opacity-75",
91+
actionsClassNames: {
92+
alignment: "text-{alignment}", // This is a substitution placeholder: left or center.
93+
}
8694
},
8795
quote: {
88-
className: "block-quote"
96+
className: "py-3 px-5 italic font-serif"
8997
},
9098
table: {
91-
className: "table"
99+
className: "table-auto"
92100
}
93101
}} />
94102
```
95103

96-
Below are the defaults for each renderer:
104+
The example above uses [TailwindCSS](https://tailwindcss.com/) classes, replacing the default ones, which you can find below:
97105

98106
```js
99107
const defaultConfigs = {
@@ -104,7 +112,7 @@ const defaultConfigs = {
104112
className: ""
105113
},
106114
embed: {
107-
className: "styled-iframe",
115+
className: "",
108116
rel: "noreferer nofollower external", // Generates an <a> if not able to receive an "embed" property
109117
sandbox: undefined
110118
},
@@ -132,12 +140,12 @@ const defaultConfigs = {
132140
}
133141
},
134142
table: {
135-
className: "table"
143+
className: ""
136144
}
137145
}
138146
```
139147

140-
So, in theory, any CSS framework (such as Bootstrap) can work seamlessly with this library as long as you pass the correct properties.
148+
So, in theory, any CSS framework can work seamlessly with this library as long as you pass the correct properties.
141149

142150
## Custom Renderers
143151

src/__snapshots__/index.test.tsx.snap

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`<Block /> when receives a Config object with className only applies className to specified block 1`] = `
4+
Array [
5+
<p
6+
className="lead"
7+
>
8+
Mollit deserunt culpa fugiat ea do laboris minim ex do. Elit cillum qui aute sint irure aliqua excepteur minim. Eiusmod velit cupidatat ea culpa magna magna id consectetur enim irure ex excepteur tempor quis. Veniam incididunt ullamco adipisicing dolor ex proident ex amet dolor. Nisi in adipisicing non quis id Lorem consectetur.
9+
</p>,
10+
<h3>
11+
Quis consectetur nostrud eu officia aute laborum labore nulla?
12+
</h3>,
13+
]
14+
`;
15+
16+
exports[`<Block /> when receives a Renderers object must maintan backward compatibility with 0.1.x format 1`] = `
17+
Array [
18+
<p>
19+
Mollit deserunt culpa fugiat ea do laboris minim ex do. Elit cillum qui aute sint irure aliqua excepteur minim. Eiusmod velit cupidatat ea culpa magna magna id consectetur enim irure ex excepteur tempor quis. Veniam incididunt ullamco adipisicing dolor ex proident ex amet dolor. Nisi in adipisicing non quis id Lorem consectetur.
20+
</p>,
21+
<h2>
22+
Do laboris magna quis nisi consequat!
23+
</h2>,
24+
<pre>
25+
<code
26+
lang="text/javascript"
27+
>
28+
const foo = new Bar();
29+
</code>
30+
</pre>,
31+
]
32+
`;
33+
34+
exports[`<Block /> when receives a Renderers object uses the provided renderer for the specified block 1`] = `
35+
Array [
36+
<p>
37+
Mollit deserunt culpa fugiat ea do laboris minim ex do. Elit cillum qui aute sint irure aliqua excepteur minim. Eiusmod velit cupidatat ea culpa magna magna id consectetur enim irure ex excepteur tempor quis. Veniam incididunt ullamco adipisicing dolor ex proident ex amet dolor. Nisi in adipisicing non quis id Lorem consectetur.
38+
</p>,
39+
<h2>
40+
Do laboris magna quis nisi consequat!
41+
</h2>,
42+
<div>
43+
<pre>
44+
<code
45+
lang="text/javascript"
46+
>
47+
const foo = new Bar();
48+
</code>
49+
</pre>
50+
<p>
51+
Warning: do not run this code in production
52+
</p>
53+
</div>,
54+
]
55+
`;
56+
357
exports[`<Block /> when receives an EditorJS blocks data renders all known block tags 1`] = `
458
Array [
559
<p>

src/index.test.tsx

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,38 @@ describe('<Block />', () => {
119119
});
120120
});
121121

122+
describe('when receives a Config object with className', () => {
123+
const data: DataProp = {
124+
time: 1610632160642,
125+
blocks: [
126+
{
127+
type: 'paragraph',
128+
data: {
129+
text: 'Mollit deserunt culpa fugiat ea do laboris minim ex do. Elit cillum qui aute sint irure aliqua excepteur minim. Eiusmod velit cupidatat ea culpa magna magna id consectetur enim irure ex excepteur tempor quis. Veniam incididunt ullamco adipisicing dolor ex proident ex amet dolor. Nisi in adipisicing non quis id Lorem consectetur.',
130+
},
131+
},
132+
{
133+
type: 'header',
134+
data: {
135+
text: 'Quis consectetur nostrud eu officia aute laborum labore nulla?',
136+
level: 3,
137+
},
138+
},
139+
],
140+
version: '2.19.0',
141+
};
142+
143+
const config: ConfigProp = {
144+
paragraph: {
145+
className: "lead"
146+
},
147+
}
148+
149+
it('only applies className to specified block', () => {
150+
expect(create(<Blocks data={data} config={config} />).toJSON()).toMatchSnapshot();
151+
});
152+
});
153+
122154
describe('when receives a Renderers object', () => {
123155
const data: DataProp = {
124156
time: 1610632160642,
@@ -129,6 +161,13 @@ describe('<Block />', () => {
129161
text: 'Mollit deserunt culpa fugiat ea do laboris minim ex do. Elit cillum qui aute sint irure aliqua excepteur minim. Eiusmod velit cupidatat ea culpa magna magna id consectetur enim irure ex excepteur tempor quis. Veniam incididunt ullamco adipisicing dolor ex proident ex amet dolor. Nisi in adipisicing non quis id Lorem consectetur.',
130162
},
131163
},
164+
{
165+
type: 'header',
166+
data: {
167+
text: 'Do laboris magna quis nisi consequat!',
168+
level: 2,
169+
},
170+
},
132171
{
133172
type: 'code',
134173
data: {
@@ -140,40 +179,47 @@ describe('<Block />', () => {
140179
version: '2.19.0',
141180
};
142181

143-
const CustomRenderCode: RenderFn<{
144-
code: string | number
145-
lang: "text/javascript" | "text/typescript"
146-
}> = ({ data: d, className }) => (
147-
<div>
148-
<pre>
149-
{d?.code && (
150-
<code lang={d.lang} className={className}>{`${d.code}`}</code>
151-
)}
152-
</pre>
153-
<p>Warning: do not run this code in production</p>
154-
</div>
155-
)
156-
157-
const CustomRenderHeader = ({
158-
data: d,
159-
className,
160-
}: {
161-
data: {[s:string]: any},
162-
className?: string
163-
}) => (
164-
<h1></h1>
165-
)
182+
it('uses the provided renderer for the specified block', () => {
183+
const CustomRenderCode: RenderFn<{
184+
code: string | number
185+
lang: "text/javascript" | "text/typescript"
186+
}> = ({ data: d, className }) => (
187+
<div>
188+
<pre>
189+
{d?.code && (
190+
<code lang={d.lang} className={className}>{`${d.code}`}</code>
191+
)}
192+
</pre>
193+
<p>Warning: do not run this code in production</p>
194+
</div>
195+
)
166196

167-
const renderers: RenderersProp = {
168-
code: CustomRenderCode,
169-
header: CustomRenderHeader,
170-
}
197+
const renderers: RenderersProp = {
198+
code: CustomRenderCode,
199+
}
171200

172-
it('uses the provided renderer for the specified block', () => {
173201
expect(create(<Blocks data={data} renderers={renderers} />).toJSON()).toMatchSnapshot();
174202
});
175203

176204
it('must maintan backward compatibility with 0.1.x format', () => {
205+
const CustomRenderHeader = ({
206+
data: d,
207+
className,
208+
}: {
209+
data: { [s: string]: any },
210+
className?: string
211+
}) => {
212+
const Tag = `h${d?.level || 1}` as keyof JSX.IntrinsicElements;
213+
214+
return (
215+
<Tag className={className}>{d.text}</Tag>
216+
)
217+
}
218+
219+
const renderers: RenderersProp = {
220+
header: CustomRenderHeader,
221+
}
222+
177223
expect(create(<Blocks data={data} renderers={renderers} />).toJSON()).toMatchSnapshot();
178224
});
179225
});

src/renderers/code/__snapshots__/index.test.tsx.snap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,28 @@ exports[`<Code /> when receives a code block renders a <pre> tag with <code> 1`]
1212
</code>
1313
</pre>
1414
`;
15+
16+
exports[`<Code /> when receives a code block with className renders a <pre> tag with <code className> 1`] = `
17+
<pre>
18+
<code
19+
className="code-block"
20+
lang="text/html"
21+
>
22+
body {
23+
font-size: 14px;
24+
line-height: 16px;
25+
}
26+
</code>
27+
</pre>
28+
`;
29+
30+
exports[`<Code /> when receives a code block without lang renders a <pre> tag with <code> without lang property 1`] = `
31+
<pre>
32+
<code>
33+
body {
34+
font-size: 14px;
35+
line-height: 16px;
36+
}
37+
</code>
38+
</pre>
39+
`;

src/renderers/code/index.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,25 @@ describe('<Code />', () => {
1313
expect(create(<Code data={data} />).toJSON()).toMatchSnapshot();
1414
});
1515
});
16+
17+
describe('when receives a code block with className', () => {
18+
const data: CodeBlockData = {
19+
code: 'body {\n font-size: 14px;\n line-height: 16px;\n}',
20+
lang: 'text/html',
21+
};
22+
23+
it('renders a <pre> tag with <code className>', () => {
24+
expect(create(<Code data={data} className={"code-block"} />).toJSON()).toMatchSnapshot();
25+
});
26+
});
27+
28+
describe('when receives a code block without lang', () => {
29+
const data: CodeBlockData = {
30+
code: 'body {\n font-size: 14px;\n line-height: 16px;\n}',
31+
};
32+
33+
it('renders a <pre> tag with <code> without lang property', () => {
34+
expect(create(<Code data={data} />).toJSON()).toMatchSnapshot();
35+
});
36+
});
1637
});

src/renderers/header/__snapshots__/index.test.tsx.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`<Header /> when receives a header block with className renders a <h2 className> 1`] = `
4+
<h2
5+
className="header-block"
6+
>
7+
Editor.js
8+
</h2>
9+
`;
10+
311
exports[`<Header /> when receives an header level 2 block renders a <h2> tag 1`] = `
412
<h2>
513
Editor.js

src/renderers/header/index.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@ describe('<Header />', () => {
1313
expect(create(<Header data={data} />).toJSON()).toMatchSnapshot();
1414
});
1515
});
16+
17+
describe('when receives a header block with className', () => {
18+
const data: HeaderBlockData = {
19+
text: 'Editor.js',
20+
level: 2,
21+
};
22+
23+
it('renders a <h2 className>', () => {
24+
expect(create(<Header data={data} className={"header-block"} />).toJSON()).toMatchSnapshot();
25+
});
26+
});
1627
});

0 commit comments

Comments
 (0)