|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import React from "react"; |
| 6 | + |
| 7 | +import { render } from "@testing-library/react"; |
| 8 | + |
| 9 | +import { styled } from "../src"; |
| 10 | + |
| 11 | +describe("Basic functionality", () => { |
| 12 | + it("it should output the correct type of DOM node", async () => { |
| 13 | + const Button = styled("button"); |
| 14 | + const { container } = render(<Button />); |
| 15 | + expect(container.firstChild?.nodeName).toEqual("BUTTON"); |
| 16 | + }); |
| 17 | + |
| 18 | + it("it should apply the base class", async () => { |
| 19 | + const Button = styled("button", "root"); |
| 20 | + const { container } = render(<Button />); |
| 21 | + expect(container.firstChild).toHaveClass("root"); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should pass through children", async () => { |
| 25 | + const Paragraph = styled("p"); |
| 26 | + const { container } = render(<Paragraph>Hello</Paragraph>); |
| 27 | + expect(container.firstChild).toHaveTextContent("Hello"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should pass provide typescript support for built in types", async () => { |
| 31 | + const Input = styled("input"); |
| 32 | + const onChange = jest.fn(); |
| 33 | + const { container } = render(<Input value={"test"} onChange={onChange} />); |
| 34 | + expect(container.firstChild).toHaveAttribute("value", "test"); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("supports variants and compound variants", () => { |
| 39 | + it("should work with a single boolean variant but not set", async () => { |
| 40 | + const Button = styled("button", "root", { |
| 41 | + primary: { true: "primary" }, |
| 42 | + }); |
| 43 | + |
| 44 | + const { container } = render(<Button />); |
| 45 | + |
| 46 | + expect(container.firstChild).toHaveClass("root"); |
| 47 | + expect(container.firstChild).not.toHaveClass("primary"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should work with a single boolean variant", async () => { |
| 51 | + const Button = styled("button", "root", { |
| 52 | + primary: { true: "primary" }, |
| 53 | + }); |
| 54 | + |
| 55 | + const { container } = render(<Button primary />); |
| 56 | + |
| 57 | + expect(container.firstChild).toHaveClass("root"); |
| 58 | + expect(container.firstChild).toHaveClass("primary"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should work with a multiple variants", async () => { |
| 62 | + const PageTitle = styled("h2", "root", { |
| 63 | + highlighted: { true: "highlighted" }, |
| 64 | + size: { 0: "size0", 1: "size1" }, |
| 65 | + align: { left: "left", center: "center", right: "right" }, |
| 66 | + }); |
| 67 | + |
| 68 | + const { container } = render( |
| 69 | + <PageTitle highlighted size={0} align="left" /> |
| 70 | + ); |
| 71 | + |
| 72 | + expect(container.firstChild).toHaveClass("root"); |
| 73 | + expect(container.firstChild).toHaveClass("highlighted"); |
| 74 | + expect(container.firstChild).toHaveClass("size0"); |
| 75 | + expect(container.firstChild).toHaveClass("left"); |
| 76 | + expect(container.firstChild).not.toHaveClass("center"); |
| 77 | + expect(container.firstChild).not.toHaveClass("right"); |
| 78 | + expect(container.firstChild).not.toHaveClass("size1"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should work with compound variants", async () => { |
| 82 | + const Button = styled( |
| 83 | + "button", |
| 84 | + "root", |
| 85 | + { |
| 86 | + border: { |
| 87 | + true: "borderTrue", |
| 88 | + }, |
| 89 | + color: { |
| 90 | + primary: "colorPrimary", |
| 91 | + secondary: "colorSecondary", |
| 92 | + }, |
| 93 | + }, |
| 94 | + [ |
| 95 | + { |
| 96 | + border: true, |
| 97 | + color: "primary", |
| 98 | + css: "borderPrimary", |
| 99 | + }, |
| 100 | + { |
| 101 | + border: true, |
| 102 | + color: "secondary", |
| 103 | + css: "borderSecondary", |
| 104 | + }, |
| 105 | + ] |
| 106 | + ); |
| 107 | + |
| 108 | + const { container } = render(<Button border color={"primary"} />); |
| 109 | + |
| 110 | + expect(container.firstChild).toHaveClass("root"); |
| 111 | + expect(container.firstChild).toHaveClass("borderTrue"); |
| 112 | + expect(container.firstChild).toHaveClass("colorPrimary"); |
| 113 | + expect(container.firstChild).toHaveClass("borderPrimary"); |
| 114 | + expect(container.firstChild).not.toHaveClass("borderSecondary"); |
| 115 | + expect(container.firstChild).not.toHaveClass("colorSecondary"); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe("supports array styles", () => { |
| 120 | + it("should should apply the base classes", async () => { |
| 121 | + const Button = styled("button", ["baseButton", "button"]); |
| 122 | + const { container } = render(<Button />); |
| 123 | + expect(container.firstChild).toHaveClass("baseButton"); |
| 124 | + expect(container.firstChild).toHaveClass("button"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should should apply the base classes", async () => { |
| 128 | + const Button = styled( |
| 129 | + "button", |
| 130 | + ["baseButton", "button"], |
| 131 | + { |
| 132 | + primary: { true: ["primary", "bold"] }, |
| 133 | + big: { true: ["big"] }, |
| 134 | + }, |
| 135 | + [ |
| 136 | + { |
| 137 | + primary: true, |
| 138 | + big: true, |
| 139 | + css: ["primaryBig", "primaryBigBold"], |
| 140 | + }, |
| 141 | + ] |
| 142 | + ); |
| 143 | + const { container } = render(<Button primary big />); |
| 144 | + expect(container.firstChild).toHaveClass("baseButton"); |
| 145 | + expect(container.firstChild).toHaveClass("button"); |
| 146 | + expect(container.firstChild).toHaveClass("primary"); |
| 147 | + expect(container.firstChild).toHaveClass("bold"); |
| 148 | + expect(container.firstChild).toHaveClass("big"); |
| 149 | + expect(container.firstChild).toHaveClass("primaryBig"); |
| 150 | + expect(container.firstChild).toHaveClass("primaryBigBold"); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe("supports more exotic setups", () => { |
| 155 | + it("should be able to style nested react components", async () => { |
| 156 | + const BaseButton = styled("button", "baseButton", { |
| 157 | + size: { big: "big", small: "small" }, |
| 158 | + }); |
| 159 | + const Button = styled(BaseButton, "button", { |
| 160 | + color: { primary: "colorPrimary", secondary: "colorSecondary" }, |
| 161 | + }); |
| 162 | + const { container } = render(<Button size="big" color="primary" />); |
| 163 | + |
| 164 | + expect(container.firstChild?.nodeName).toEqual("BUTTON"); |
| 165 | + expect(container.firstChild).toHaveClass("baseButton"); |
| 166 | + expect(container.firstChild).toHaveClass("button"); |
| 167 | + expect(container.firstChild).toHaveClass("big"); |
| 168 | + expect(container.firstChild).toHaveClass("colorPrimary"); |
| 169 | + }); |
| 170 | + |
| 171 | + it("should pass down refs", async () => { |
| 172 | + const Button = styled("button"); |
| 173 | + const ref = jest.fn(); |
| 174 | + const { container } = render(<Button ref={ref} />); |
| 175 | + expect(container.firstChild?.nodeName).toEqual("BUTTON"); |
| 176 | + expect(ref).toBeCalled(); |
| 177 | + }); |
| 178 | +}); |
0 commit comments