|
| 1 | +import React from "react"; |
| 2 | +import { withBem } from "./index"; |
| 3 | +import { describe, expect, test } from "@jest/globals"; |
| 4 | +import { toMatchJSX } from "./toMatchJSX"; |
| 5 | + |
| 6 | +expect.extend({ |
| 7 | + toMatchJSX, |
| 8 | +}); |
| 9 | + |
| 10 | +describe("showcase", () => { |
| 11 | + test("Simplest way to create a block with some elements", () => { |
| 12 | + const Acme = withBem.named( |
| 13 | + "Acme", |
| 14 | + function ({ bem: { className, element } }) { |
| 15 | + return ( |
| 16 | + <div className={className}> |
| 17 | + <h1 className={element`heading`}>Hello</h1> |
| 18 | + </div> |
| 19 | + ); |
| 20 | + }, |
| 21 | + ); |
| 22 | + |
| 23 | + expect(<Acme />).toMatchJSX( |
| 24 | + <div className="acme"> |
| 25 | + <h1 className="acme__heading">Hello</h1> |
| 26 | + </div>, |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + test("BEM helper as a shorthand if there are no elements", () => { |
| 31 | + const Acme = withBem.named("Acme", function ({ bem }) { |
| 32 | + return <div className={bem}>Hello</div>; |
| 33 | + }); |
| 34 | + |
| 35 | + expect(<Acme />).toMatchJSX(<div className="acme">Hello</div>); |
| 36 | + }); |
| 37 | + |
| 38 | + test("Adding block modifiers", () => { |
| 39 | + const Acme = withBem.named("Acme", function ({ bem: { block } }) { |
| 40 | + const [toggle, setToggle] = React.useState(true); |
| 41 | + const onClick = React.useCallback( |
| 42 | + () => setToggle((current) => !current), |
| 43 | + [setToggle], |
| 44 | + ); |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className={block`${{ toggle }} always-enabled`}> |
| 48 | + <button onClick={onClick}>Toggle</button> |
| 49 | + </div> |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + expect(<Acme />).toMatchJSX( |
| 54 | + <div className="acme acme--toggle acme--always-enabled"> |
| 55 | + <button>Toggle</button> |
| 56 | + </div>, |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + test("Mixing the block with other classes", () => { |
| 61 | + const Acme = withBem.named("Acme", function ({ bem: { block } }) { |
| 62 | + return <div className={block``.mix`me-2 d-flex`}></div>; |
| 63 | + }); |
| 64 | + |
| 65 | + expect(<Acme />).toMatchJSX(<div className="acme me-2 d-flex" />); |
| 66 | + }); |
| 67 | + |
| 68 | + test("Mixing with parent block", () => { |
| 69 | + const Child = withBem.named("Child", function Child({ bem: { block } }) { |
| 70 | + return <div className={block`${{ active: true }}`.mix`me-2`} />; |
| 71 | + }); |
| 72 | + |
| 73 | + const Parent = withBem.named( |
| 74 | + "Parent", |
| 75 | + function Parent({ bem: { className, element } }) { |
| 76 | + return ( |
| 77 | + <div className={className}> |
| 78 | + <Child className={element`element`} /> |
| 79 | + </div> |
| 80 | + ); |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + expect(<Parent />).toMatchJSX( |
| 85 | + <div className="parent"> |
| 86 | + <div className="child parent__element child--active me-2" /> |
| 87 | + </div>, |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + test("Using elements with modifiers", () => { |
| 92 | + const Acme = withBem.named( |
| 93 | + "Acme", |
| 94 | + function ({ bem: { className, element } }: withBem.props) { |
| 95 | + return ( |
| 96 | + <div className={className}> |
| 97 | + <div className={element`item ${{ selected: true }} me-2`} /> |
| 98 | + <div className={element`item ${{ variant: "primary" }}`} /> |
| 99 | + <div className={element`item ${["theme-dark"]}`} /> |
| 100 | + <div className={element`item`.mix`d-flex`} /> |
| 101 | + </div> |
| 102 | + ); |
| 103 | + }, |
| 104 | + ); |
| 105 | + |
| 106 | + expect(<Acme />).toMatchJSX( |
| 107 | + <div className="acme"> |
| 108 | + <div className="acme__item acme__item--selected me-2" /> |
| 109 | + <div className="acme__item acme__item--variant-primary" /> |
| 110 | + <div className="acme__item acme__item--theme-dark" /> |
| 111 | + <div className="acme__item d-flex" /> |
| 112 | + </div>, |
| 113 | + ); |
| 114 | + }); |
| 115 | +}); |
0 commit comments