|
1 |
| -import React from "react"; |
2 |
| -import { render, cleanup } from "react-testing-library"; |
3 |
| - |
4 |
| -import Highlight from "../Highlight"; |
5 |
| -import defaultProps from "../../defaultProps"; |
6 |
| - |
7 |
| -const exampleCode = ` |
8 |
| -(function someDemo() { |
9 |
| - var test = "Hello World!"; |
10 |
| - console.log(test); |
11 |
| -})(); |
12 |
| -
|
13 |
| -return () => <App />; |
14 |
| -`.trim(); |
15 |
| - |
16 |
| -describe("<Highlight />", () => { |
17 |
| - afterEach(cleanup); |
18 |
| - |
19 |
| - describe("snapshots", () => { |
20 |
| - it("renders correctly", () => { |
21 |
| - const { container } = render( |
22 |
| - <Highlight {...defaultProps} code={exampleCode} language="jsx"> |
23 |
| - {({ className, style, tokens, getLineProps, getTokenProps }) => ( |
24 |
| - <pre className={className} style={style}> |
25 |
| - {tokens.map((line, i) => ( |
26 |
| - <div {...getLineProps({ line, key: i })}> |
27 |
| - {line.map((token, key) => ( |
28 |
| - <span {...getTokenProps({ token, key })} /> |
29 |
| - ))} |
30 |
| - </div> |
31 |
| - ))} |
32 |
| - </pre> |
33 |
| - )} |
34 |
| - </Highlight> |
35 |
| - ); |
36 |
| - |
37 |
| - expect(container).toMatchSnapshot(); |
38 |
| - }); |
39 |
| - |
40 |
| - it("renders unsupported languages correctly", () => { |
41 |
| - const { container } = render( |
42 |
| - <Highlight |
43 |
| - {...defaultProps} |
44 |
| - code={exampleCode} |
45 |
| - language="abcdefghijklmnop" |
46 |
| - > |
47 |
| - {({ className, style, tokens, getLineProps, getTokenProps }) => ( |
48 |
| - <pre className={className} style={style}> |
49 |
| - {tokens.map((line, i) => ( |
50 |
| - <div {...getLineProps({ line, key: i })}> |
51 |
| - {line.map((token, key) => ( |
52 |
| - <span {...getTokenProps({ token, key })} /> |
53 |
| - ))} |
54 |
| - </div> |
55 |
| - ))} |
56 |
| - </pre> |
57 |
| - )} |
58 |
| - </Highlight> |
59 |
| - ); |
60 |
| - |
61 |
| - expect(container).toMatchSnapshot(); |
62 |
| - }); |
63 |
| - |
64 |
| - it("renders without style props when no theme is passed", () => { |
65 |
| - const { container } = render( |
66 |
| - <Highlight |
67 |
| - {...defaultProps} |
68 |
| - theme={undefined} |
69 |
| - code={exampleCode} |
70 |
| - language="jsx" |
71 |
| - > |
72 |
| - {({ className, style, tokens, getLineProps, getTokenProps }) => ( |
73 |
| - <pre className={className} style={style}> |
74 |
| - {tokens.map((line, i) => ( |
75 |
| - <div {...getLineProps({ line, key: i })}> |
76 |
| - {line.map((token, key) => ( |
77 |
| - <span {...getTokenProps({ token, key })} /> |
78 |
| - ))} |
79 |
| - </div> |
80 |
| - ))} |
81 |
| - </pre> |
82 |
| - )} |
83 |
| - </Highlight> |
84 |
| - ); |
85 |
| - |
86 |
| - expect(container.innerHTML.includes("style")).toBeFalsy(); |
87 |
| - }); |
88 |
| - }); |
89 |
| - |
90 |
| - describe("getLineProps", () => { |
91 |
| - it("transforms lineProps inputs correctly", () => { |
92 |
| - const input = { |
93 |
| - key: "line-1", |
94 |
| - style: { cssProp: "test" }, |
95 |
| - className: "line-class", |
96 |
| - line: [{ types: ["punctuation"], content: "!" }], |
97 |
| - restPropsTest: true |
98 |
| - }; |
99 |
| - |
100 |
| - render( |
101 |
| - <Highlight {...defaultProps} code={exampleCode} language="jsx"> |
102 |
| - {({ getLineProps }) => { |
103 |
| - const output = getLineProps(input); |
104 |
| - |
105 |
| - expect(output).toEqual({ |
106 |
| - key: "line-1", |
107 |
| - style: { |
108 |
| - cssProp: "test", |
109 |
| - backgroundColor: null, |
110 |
| - color: expect.any(String) |
111 |
| - }, |
112 |
| - className: "token-line line-class", |
113 |
| - restPropsTest: true |
114 |
| - }); |
115 |
| - |
116 |
| - return null; |
117 |
| - }} |
118 |
| - </Highlight> |
119 |
| - ); |
120 |
| - }); |
121 |
| - }); |
122 |
| - |
123 |
| - describe("getTokenProps", () => { |
124 |
| - it("transforms tokenProps inputs correctly", () => { |
125 |
| - const input = { |
126 |
| - key: "token-1", |
127 |
| - style: { cssProp: "test" }, |
128 |
| - className: "token-class", |
129 |
| - token: { types: ["punctuation"], content: "!" }, |
130 |
| - restPropsTest: true |
131 |
| - }; |
132 |
| - |
133 |
| - render( |
134 |
| - <Highlight {...defaultProps} code={exampleCode} language="jsx"> |
135 |
| - {({ getTokenProps }) => { |
136 |
| - const output = getTokenProps(input); |
137 |
| - |
138 |
| - expect(output).toEqual({ |
139 |
| - key: "token-1", |
140 |
| - style: { cssProp: "test", color: expect.any(String) }, |
141 |
| - className: "token punctuation token-class", |
142 |
| - restPropsTest: true, |
143 |
| - children: "!" |
144 |
| - }); |
145 |
| - |
146 |
| - return null; |
147 |
| - }} |
148 |
| - </Highlight> |
149 |
| - ); |
150 |
| - }); |
151 |
| - |
152 |
| - it("transforms constructor token style correctly", () => { |
153 |
| - // From https://github.com/FormidableLabs/prism-react-renderer/issues/11 |
154 |
| - render( |
155 |
| - <Highlight {...defaultProps} code={"open Common;"} language="reason"> |
156 |
| - {({ tokens, getTokenProps }) => { |
157 |
| - const line = tokens[0]; |
158 |
| - const token = line[2]; |
159 |
| - const output = getTokenProps({ token, key: 2 }); |
160 |
| - |
161 |
| - expect(typeof output.style).not.toBe("function"); |
162 |
| - |
163 |
| - return null; |
164 |
| - }} |
165 |
| - </Highlight> |
166 |
| - ); |
167 |
| - }); |
168 |
| - }); |
169 |
| -}); |
| 1 | +import React from "react"; |
| 2 | +import { render, cleanup } from "@testing-library/react"; |
| 3 | + |
| 4 | +import Highlight from "../Highlight"; |
| 5 | +import defaultProps from "../../defaultProps"; |
| 6 | + |
| 7 | +const exampleCode = ` |
| 8 | +(function someDemo() { |
| 9 | + var test = "Hello World!"; |
| 10 | + console.log(test); |
| 11 | +})(); |
| 12 | +
|
| 13 | +return () => <App />; |
| 14 | +`.trim(); |
| 15 | + |
| 16 | +describe("<Highlight />", () => { |
| 17 | + afterEach(cleanup); |
| 18 | + |
| 19 | + describe("snapshots", () => { |
| 20 | + it("renders correctly", () => { |
| 21 | + const { container } = render( |
| 22 | + <Highlight {...defaultProps} code={exampleCode} language="jsx"> |
| 23 | + {({ className, style, tokens, getLineProps, getTokenProps }) => ( |
| 24 | + <pre className={className} style={style}> |
| 25 | + {tokens.map((line, i) => ( |
| 26 | + <div {...getLineProps({ line, key: i })}> |
| 27 | + {line.map((token, key) => ( |
| 28 | + <span {...getTokenProps({ token, key })} /> |
| 29 | + ))} |
| 30 | + </div> |
| 31 | + ))} |
| 32 | + </pre> |
| 33 | + )} |
| 34 | + </Highlight> |
| 35 | + ); |
| 36 | + |
| 37 | + expect(container).toMatchSnapshot(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("renders unsupported languages correctly", () => { |
| 41 | + const { container } = render( |
| 42 | + <Highlight |
| 43 | + {...defaultProps} |
| 44 | + code={exampleCode} |
| 45 | + language="abcdefghijklmnop" |
| 46 | + > |
| 47 | + {({ className, style, tokens, getLineProps, getTokenProps }) => ( |
| 48 | + <pre className={className} style={style}> |
| 49 | + {tokens.map((line, i) => ( |
| 50 | + <div {...getLineProps({ line, key: i })}> |
| 51 | + {line.map((token, key) => ( |
| 52 | + <span {...getTokenProps({ token, key })} /> |
| 53 | + ))} |
| 54 | + </div> |
| 55 | + ))} |
| 56 | + </pre> |
| 57 | + )} |
| 58 | + </Highlight> |
| 59 | + ); |
| 60 | + |
| 61 | + expect(container).toMatchSnapshot(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("renders without style props when no theme is passed", () => { |
| 65 | + const { container } = render( |
| 66 | + <Highlight |
| 67 | + {...defaultProps} |
| 68 | + theme={undefined} |
| 69 | + code={exampleCode} |
| 70 | + language="jsx" |
| 71 | + > |
| 72 | + {({ className, style, tokens, getLineProps, getTokenProps }) => ( |
| 73 | + <pre className={className} style={style}> |
| 74 | + {tokens.map((line, i) => ( |
| 75 | + <div {...getLineProps({ line, key: i })}> |
| 76 | + {line.map((token, key) => ( |
| 77 | + <span {...getTokenProps({ token, key })} /> |
| 78 | + ))} |
| 79 | + </div> |
| 80 | + ))} |
| 81 | + </pre> |
| 82 | + )} |
| 83 | + </Highlight> |
| 84 | + ); |
| 85 | + |
| 86 | + expect(container.innerHTML.includes("style")).toBeFalsy(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("getLineProps", () => { |
| 91 | + it("transforms lineProps inputs correctly", () => { |
| 92 | + const input = { |
| 93 | + key: "line-1", |
| 94 | + style: { cssProp: "test" }, |
| 95 | + className: "line-class", |
| 96 | + line: [{ types: ["punctuation"], content: "!" }], |
| 97 | + restPropsTest: true, |
| 98 | + }; |
| 99 | + |
| 100 | + render( |
| 101 | + <Highlight {...defaultProps} code={exampleCode} language="jsx"> |
| 102 | + {({ getLineProps }) => { |
| 103 | + const output = getLineProps(input); |
| 104 | + |
| 105 | + expect(output).toEqual({ |
| 106 | + key: "line-1", |
| 107 | + style: { |
| 108 | + cssProp: "test", |
| 109 | + backgroundColor: null, |
| 110 | + color: expect.any(String), |
| 111 | + }, |
| 112 | + className: "token-line line-class", |
| 113 | + restPropsTest: true, |
| 114 | + }); |
| 115 | + |
| 116 | + return null; |
| 117 | + }} |
| 118 | + </Highlight> |
| 119 | + ); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe("getTokenProps", () => { |
| 124 | + it("transforms tokenProps inputs correctly", () => { |
| 125 | + const input = { |
| 126 | + key: "token-1", |
| 127 | + style: { cssProp: "test" }, |
| 128 | + className: "token-class", |
| 129 | + token: { types: ["punctuation"], content: "!" }, |
| 130 | + restPropsTest: true, |
| 131 | + }; |
| 132 | + |
| 133 | + render( |
| 134 | + <Highlight {...defaultProps} code={exampleCode} language="jsx"> |
| 135 | + {({ getTokenProps }) => { |
| 136 | + const output = getTokenProps(input); |
| 137 | + |
| 138 | + expect(output).toEqual({ |
| 139 | + key: "token-1", |
| 140 | + style: { cssProp: "test", color: expect.any(String) }, |
| 141 | + className: "token punctuation token-class", |
| 142 | + restPropsTest: true, |
| 143 | + children: "!", |
| 144 | + }); |
| 145 | + |
| 146 | + return null; |
| 147 | + }} |
| 148 | + </Highlight> |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it("transforms constructor token style correctly", () => { |
| 153 | + // From https://github.com/FormidableLabs/prism-react-renderer/issues/11 |
| 154 | + render( |
| 155 | + <Highlight {...defaultProps} code={"open Common;"} language="reason"> |
| 156 | + {({ tokens, getTokenProps }) => { |
| 157 | + const line = tokens[0]; |
| 158 | + const token = line[2]; |
| 159 | + const output = getTokenProps({ token, key: 2 }); |
| 160 | + |
| 161 | + expect(typeof output.style).not.toBe("function"); |
| 162 | + |
| 163 | + return null; |
| 164 | + }} |
| 165 | + </Highlight> |
| 166 | + ); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments