-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowcase.spec.tsx
More file actions
115 lines (100 loc) · 3.23 KB
/
showcase.spec.tsx
File metadata and controls
115 lines (100 loc) · 3.23 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React from "react";
import { withBem } from "./index";
import { describe, expect, test } from "@jest/globals";
import { toMatchJSX } from "./toMatchJSX";
expect.extend({
toMatchJSX,
});
describe("showcase", () => {
test("Simplest way to create a block with some elements", () => {
const Acme = withBem.named(
"Acme",
function ({ bem: { className, element } }) {
return (
<div className={className}>
<h1 className={element`heading`}>Hello</h1>
</div>
);
},
);
expect(<Acme />).toMatchJSX(
<div className="acme">
<h1 className="acme__heading">Hello</h1>
</div>,
);
});
test("BEM helper as a shorthand if there are no elements", () => {
const Acme = withBem.named("Acme", function ({ bem }) {
return <div className={bem}>Hello</div>;
});
expect(<Acme />).toMatchJSX(<div className="acme">Hello</div>);
});
test("Adding block modifiers", () => {
const Acme = withBem.named("Acme", function ({ bem: { block } }) {
const [toggle, setToggle] = React.useState(true);
const onClick = React.useCallback(
() => setToggle((current) => !current),
[setToggle],
);
return (
<div className={block`${{ toggle }} always-enabled`}>
<button onClick={onClick}>Toggle</button>
</div>
);
});
expect(<Acme />).toMatchJSX(
<div className="acme acme--toggle acme--always-enabled">
<button>Toggle</button>
</div>,
);
});
test("Mixing the block with other classes", () => {
const Acme = withBem.named("Acme", function ({ bem: { block } }) {
return <div className={block``.mix`me-2 d-flex`}></div>;
});
expect(<Acme />).toMatchJSX(<div className="acme me-2 d-flex" />);
});
test("Mixing with parent block", () => {
const Child = withBem.named("Child", function Child({ bem: { block } }) {
return <div className={block`${{ active: true }}`.mix`me-2`} />;
});
const Parent = withBem.named(
"Parent",
function Parent({ bem: { className, element } }) {
return (
<div className={className}>
<Child className={element`element`} />
</div>
);
},
);
expect(<Parent />).toMatchJSX(
<div className="parent">
<div className="child parent__element child--active me-2" />
</div>,
);
});
test("Using elements with modifiers", () => {
const Acme = withBem.named(
"Acme",
function ({ bem: { className, element } }: withBem.props) {
return (
<div className={className}>
<div className={element`item ${{ selected: true }} me-2`} />
<div className={element`item ${{ variant: "primary" }}`} />
<div className={element`item ${["theme-dark"]}`} />
<div className={element`item`.mix`d-flex`} />
</div>
);
},
);
expect(<Acme />).toMatchJSX(
<div className="acme">
<div className="acme__item acme__item--selected me-2" />
<div className="acme__item acme__item--variant-primary" />
<div className="acme__item acme__item--theme-dark" />
<div className="acme__item d-flex" />
</div>,
);
});
});