mathbox-react 0.0.11-rc → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/build/cjs/components/components.d.ts +2 -2
  2. package/build/cjs/components/types.d.ts +2 -2
  3. package/build/cjs/components/util.d.ts +1 -1
  4. package/build/cjs/index.js +1 -1
  5. package/build/cjs/index.js.map +1 -1
  6. package/build/esm/components/components.d.ts +2 -2
  7. package/build/esm/components/types.d.ts +2 -2
  8. package/build/esm/components/util.d.ts +1 -1
  9. package/build/esm/index.js +1 -1
  10. package/build/esm/index.js.map +1 -1
  11. package/build/index.d.ts +4 -4
  12. package/package.json +12 -17
  13. package/.eslintrc.js +0 -72
  14. package/.github/renovate.json +0 -13
  15. package/.github/workflows/lint.yaml +0 -20
  16. package/.prettierrc +0 -1
  17. package/.storybook/main.js +0 -9
  18. package/.storybook/preview.js +0 -11
  19. package/LICENSE +0 -29
  20. package/README.md +0 -6
  21. package/babel.config.js +0 -3
  22. package/build/cjs/components/components.spec.d.ts +0 -1
  23. package/build/cjs/stories/utils.d.ts +0 -5
  24. package/build/esm/components/components.spec.d.ts +0 -1
  25. package/build/esm/stories/utils.d.ts +0 -5
  26. package/jest.config.js +0 -12
  27. package/rollup.config.js +0 -38
  28. package/src/components/ContainedMathbox.tsx +0 -38
  29. package/src/components/Mathbox.tsx +0 -63
  30. package/src/components/MathboxNodeContext.ts +0 -6
  31. package/src/components/components.spec.tsx +0 -311
  32. package/src/components/components.tsx +0 -443
  33. package/src/components/index.ts +0 -3
  34. package/src/components/types.ts +0 -9
  35. package/src/components/util.ts +0 -106
  36. package/src/index.ts +0 -1
  37. package/src/stories/Cartesian.stories.tsx +0 -36
  38. package/src/stories/Grid.stories.tsx +0 -38
  39. package/src/stories/Mathbox.stories.tsx +0 -16
  40. package/src/stories/Point.stories.tsx +0 -75
  41. package/src/stories/utils.tsx +0 -33
  42. package/src/testSetup.ts +0 -3
  43. package/src/testUtils.ts +0 -46
  44. package/tsconfig.base.json +0 -17
  45. package/tsconfig.json +0 -5
@@ -1,311 +0,0 @@
1
- import React, { useState } from "react"
2
- import { render, act } from "@testing-library/react"
3
- import { MathboxSelection } from "mathbox"
4
- import ContainedMathbox from "./ContainedMathbox"
5
- import Mathbox from "./Mathbox"
6
- import { Cartesian, Grid, Voxel } from "./components"
7
- import { MathboxRef } from "./types"
8
-
9
- function assertNotNil<T>(value: T): asserts value is NonNullable<T> {
10
- if (value === undefined) {
11
- throw new Error("Unexpected undefined value")
12
- }
13
- if (value === null) {
14
- throw new Error("Unexpected null value")
15
- }
16
- }
17
-
18
- /**
19
- * Assert that two mathbox selections have the same nodes in the same order.
20
- */
21
- const assertSelectionsEqual = (s1: MathboxSelection, s2: MathboxSelection) => {
22
- expect(s1.length).toBe(s2.length)
23
- Array(s1.length)
24
- .fill(null)
25
- .forEach((_, i) => {
26
- expect(s1[i]).toBe(s2[i])
27
- })
28
- }
29
-
30
- describe("ContainedMathbox", () => {
31
- it("makes a new mathbox instance only if the options have changed", () => {
32
- const mbRef: MathboxRef<"root"> = { current: null }
33
- const { rerender } = render(<ContainedMathbox options={{}} ref={mbRef} />)
34
-
35
- const mb0 = mbRef.current
36
- assertNotNil(mb0)
37
- rerender(<ContainedMathbox options={{}} ref={mbRef} />)
38
- const mb1 = mbRef.current
39
- expect(mb1).toBe(mb0)
40
- assertNotNil(mb1)
41
- rerender(<ContainedMathbox options={{ plugins: ["core"] }} ref={mbRef} />)
42
- const mb2 = mbRef.current
43
- assertNotNil(mb2)
44
- expect(mb2).not.toBe(mb1)
45
- })
46
- })
47
-
48
- describe("Cartesian", () => {
49
- it("exposes Mathbox instance via ref", () => {
50
- const mbRef: MathboxRef<"root"> = { current: null }
51
- const cartesianRef: MathboxRef<"cartesian"> = { current: null }
52
- render(
53
- <ContainedMathbox ref={mbRef}>
54
- <Cartesian ref={cartesianRef} />
55
- </ContainedMathbox>
56
- )
57
-
58
- expect(mbRef.current?.[0].type).toBe("root")
59
- expect(cartesianRef.current?.[0].type).toBe("cartesian")
60
- })
61
-
62
- it("creates a cartesian instance as child of root", () => {
63
- const mbRef: MathboxRef<"root"> = { current: null }
64
- render(
65
- <ContainedMathbox ref={mbRef}>
66
- <Cartesian />
67
- </ContainedMathbox>
68
- )
69
- expect(mbRef.current?.select("cartesian").length).toBe(1)
70
- })
71
-
72
- it("creates mathbox children as children of itself", () => {
73
- const mbRef: MathboxRef<"root"> = { current: null }
74
- render(
75
- <ContainedMathbox ref={mbRef}>
76
- <Cartesian>
77
- <Grid />
78
- <Grid />
79
- </Cartesian>
80
- </ContainedMathbox>
81
- )
82
- mbRef.current?.print()
83
- expect(mbRef.current?.select("cartesian").length).toBe(1)
84
- expect(mbRef.current?.select("cartesian grid").length).toBe(2)
85
- })
86
-
87
- it("removes its mathbox instance when unmounted", () => {
88
- const mbRef: MathboxRef<"root"> = { current: null }
89
- const { rerender } = render(
90
- <ContainedMathbox ref={mbRef}>
91
- <Cartesian />
92
- </ContainedMathbox>
93
- )
94
- expect(mbRef.current?.select("cartesian").length).toBe(1)
95
- rerender(<ContainedMathbox ref={mbRef} />)
96
- expect(mbRef.current?.select("cartesian").length).toBe(0)
97
- })
98
-
99
- it.each([
100
- { props: { visible: true, scale: [3, 2, 1] } },
101
- { props: { visible: false, scale: [1, 2, 3] } },
102
- ])("passes appropriate props to its mathbox instance", ({ props }) => {
103
- const mbRef: MathboxRef<"root"> = { current: null }
104
- render(
105
- <ContainedMathbox ref={mbRef}>
106
- <Cartesian {...props} />
107
- </ContainedMathbox>
108
- )
109
- const cartesian = mbRef.current?.select<"cartesian">("cartesian")
110
-
111
- assertNotNil(cartesian)
112
-
113
- expect(cartesian.get("visible")).toBe(props.visible)
114
- // Mathbox converts scale to a ThreeJS Vec3
115
- expect(cartesian.get("scale").toArray()).toStrictEqual(props.scale)
116
- })
117
-
118
- it("updates props on its mathbox instance when rerendered", () => {
119
- const mbRef: MathboxRef<"root"> = { current: null }
120
- const { rerender } = render(
121
- <ContainedMathbox ref={mbRef}>
122
- <Cartesian />
123
- </ContainedMathbox>
124
- )
125
- const cartesian = mbRef.current?.select<"cartesian">("cartesian")
126
- assertNotNil(cartesian)
127
-
128
- expect(cartesian.get("visible")).toBe(true)
129
- rerender(
130
- <ContainedMathbox ref={mbRef}>
131
- <Cartesian visible={false} />
132
- </ContainedMathbox>
133
- )
134
- expect(cartesian.get("visible")).toBe(false)
135
- })
136
-
137
- it("updates liveProps on its mathbox instance when rerendered", async () => {
138
- const mbRef: MathboxRef<"root"> = { current: null }
139
- const { rerender } = render(
140
- <ContainedMathbox ref={mbRef}>
141
- <Cartesian>
142
- <Grid
143
- liveProps={{
144
- width: (t) => 1 + t,
145
- }}
146
- />
147
- </Cartesian>
148
- </ContainedMathbox>
149
- )
150
- const grid = mbRef.current?.select<"grid">("grid")
151
- assertNotNil(grid)
152
-
153
- const w1 = grid.get("width")
154
- await new Promise((resolve) => {
155
- setTimeout(resolve, 500)
156
- })
157
- const w2 = grid.get("width")
158
- expect(w1).toBe(1)
159
- // Mathbox uses seconds
160
- expect(w2 - w1).toBeGreaterThan(0.45)
161
- expect(w2 - w1).toBeLessThanOrEqual(0.5)
162
- rerender(
163
- <ContainedMathbox ref={mbRef}>
164
- <Cartesian>
165
- <Grid width={10} />
166
- </Cartesian>
167
- </ContainedMathbox>
168
- )
169
- expect(grid.get("width")).toBe(10)
170
- })
171
-
172
- it("re-renders inside new instance when root changes", () => {
173
- const mbRef: MathboxRef<"root"> = { current: null }
174
- const cartesianRef: MathboxRef<"cartesian"> = { current: null }
175
- const gridRef: MathboxRef<"grid"> = { current: null }
176
- const options1 = {}
177
- const { rerender } = render(
178
- <ContainedMathbox ref={mbRef} options={options1}>
179
- <Cartesian ref={cartesianRef}>
180
- <Grid ref={gridRef} />
181
- </Cartesian>
182
- </ContainedMathbox>
183
- )
184
-
185
- const mb1 = mbRef.current
186
- const cartesian1 = cartesianRef.current
187
- const grid1 = gridRef.current
188
- assertNotNil(mb1)
189
- assertNotNil(cartesian1)
190
- assertNotNil(grid1)
191
-
192
- expect(cartesian1).toHaveLength(1)
193
- assertSelectionsEqual(mb1.select("cartesian"), cartesian1)
194
- expect(grid1).toHaveLength(1)
195
- assertSelectionsEqual(mb1.select("grid"), grid1)
196
-
197
- /**
198
- * When re-rendered, this will create a new mathBox since the options
199
- * object prop will have changed.
200
- */
201
- const options2 = { plugins: ["core"] }
202
- expect(options1).not.toBe(options2)
203
-
204
- rerender(
205
- <ContainedMathbox ref={mbRef} options={options2}>
206
- <Cartesian ref={cartesianRef}>
207
- <Grid ref={gridRef} />
208
- </Cartesian>
209
- </ContainedMathbox>
210
- )
211
-
212
- const mb2 = mbRef.current
213
- const cartesian2 = cartesianRef.current
214
- const grid2 = gridRef.current
215
- assertNotNil(mb2)
216
- assertNotNil(cartesian2)
217
- assertNotNil(grid2)
218
-
219
- // The options have changed (via deep equal) so a new instance is created
220
- expect(mb1).not.toBe(mb2)
221
-
222
- expect(cartesian2).toHaveLength(1)
223
- assertSelectionsEqual(mb2.select("cartesian"), cartesian2)
224
- expect(grid2).toHaveLength(1)
225
- assertSelectionsEqual(mb2.select("grid"), grid2)
226
- })
227
-
228
- it("Can render a new instance without error", async () => {
229
- const mbRef: MathboxRef<"root"> = { current: null }
230
- let containerDiv: HTMLDivElement | null = null
231
- let setKey: (key: string) => void
232
- const KeyedMathbox = () => {
233
- const [container, setContainer] = useState<HTMLDivElement | null>(null)
234
- const [key, setKeyState] = useState("key-0")
235
- setKey = setKeyState
236
- containerDiv = container
237
- return (
238
- <div ref={setContainer} key={key}>
239
- {container && (
240
- <Mathbox ref={mbRef} container={container}>
241
- <Cartesian />
242
- </Mathbox>
243
- )}
244
- </div>
245
- )
246
- }
247
- render(<KeyedMathbox />)
248
-
249
- assertNotNil(mbRef.current)
250
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
251
- act(() => setKey!("key-2"))
252
-
253
- assertNotNil(containerDiv)
254
- expect(mbRef.current.three.element).toBe(containerDiv)
255
- expect(mbRef.current.select("cartesian").length).toBe(1)
256
- })
257
-
258
- it("Can add and remove children without error", async () => {
259
- const mbRef: MathboxRef<"root"> = { current: null }
260
-
261
- const { rerender } = render(
262
- <ContainedMathbox ref={mbRef} options={{}}>
263
- <Cartesian>
264
- <Grid />
265
- </Cartesian>
266
- </ContainedMathbox>
267
- )
268
-
269
- assertNotNil(mbRef.current)
270
- expect(mbRef.current.select("grid").length).toBe(1)
271
-
272
- rerender(
273
- <ContainedMathbox ref={mbRef} options={{}}>
274
- <Cartesian>
275
- <Grid />
276
- <Grid />
277
- </Cartesian>
278
- </ContainedMathbox>
279
- )
280
-
281
- assertNotNil(mbRef.current)
282
- expect(mbRef.current.select("grid").length).toBe(2)
283
-
284
- rerender(
285
- <ContainedMathbox ref={mbRef} options={{}}>
286
- <Cartesian />
287
- </ContainedMathbox>
288
- )
289
-
290
- assertNotNil(mbRef.current)
291
- expect(mbRef.current.select("grid").length).toBe(0)
292
- })
293
- })
294
-
295
- describe("<Voxel />", () => {
296
- it("throws a runtime error if it has children", () => {
297
- const willThrow = () =>
298
- render(
299
- <ContainedMathbox>
300
- <Cartesian>
301
- {/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
302
- {/* @ts-expect-error */}
303
- <Voxel>
304
- <Grid />
305
- </Voxel>
306
- </Cartesian>
307
- </ContainedMathbox>
308
- )
309
- expect(willThrow).toThrow("Component <Voxel /> cannot have children.")
310
- })
311
- })