Skip to content

Commit 8ace3c9

Browse files
committed
test: refactor
1 parent d931057 commit 8ace3c9

File tree

7 files changed

+221
-15
lines changed

7 files changed

+221
-15
lines changed

packages/frameworks/react/tests/machine.test.ts

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,173 @@ describe("basic", () => {
449449
vi.useRealTimers()
450450
})
451451
})
452+
453+
describe("edge cases", () => {
454+
test("reactive props updates", async () => {
455+
const actionSpy = vi.fn()
456+
457+
const machine = createMachine<any>({
458+
props({ props }) {
459+
return { max: props.max || 0 }
460+
},
461+
initialState() {
462+
return "test"
463+
},
464+
context({ bindable }) {
465+
return {
466+
count: bindable(() => ({ defaultValue: 0 })),
467+
}
468+
},
469+
states: {
470+
test: {
471+
on: {
472+
INCREMENT: {
473+
actions: ["increment"],
474+
},
475+
CHECK: {
476+
guard: ({ prop, context }: any) => context.get("count") < prop("max"),
477+
actions: ["allowAction"],
478+
},
479+
},
480+
},
481+
},
482+
implementations: {
483+
actions: {
484+
allowAction: actionSpy,
485+
increment: ({ context }) => context.set("count", context.get("count") + 1),
486+
},
487+
},
488+
})
489+
490+
const { result } = renderHook(() => useMachine(machine, { max: 5 }))
491+
492+
// Increment count to 3
493+
await act(async () => result.current.send({ type: "INCREMENT" }))
494+
await act(async () => result.current.send({ type: "INCREMENT" }))
495+
await act(async () => result.current.send({ type: "INCREMENT" }))
496+
497+
// max is 5, count is 3, should allow action
498+
await act(async () => result.current.send({ type: "CHECK" }))
499+
expect(actionSpy).toHaveBeenCalledTimes(1)
500+
})
501+
502+
test("state.matches() helper", async () => {
503+
const machine = createMachine<any>({
504+
initialState() {
505+
return "idle"
506+
},
507+
states: {
508+
idle: {
509+
on: {
510+
START: { target: "loading" },
511+
},
512+
},
513+
loading: {
514+
on: {
515+
SUCCESS: { target: "success" },
516+
ERROR: { target: "error" },
517+
},
518+
},
519+
success: {},
520+
error: {},
521+
},
522+
})
523+
524+
const { result, send } = renderMachine(machine)
525+
526+
expect(result.current.state.matches("idle")).toBe(true)
527+
expect(result.current.state.matches("idle", "loading")).toBe(true)
528+
expect(result.current.state.matches("loading", "success")).toBe(false)
529+
530+
await send({ type: "START" })
531+
expect(result.current.state.matches("loading")).toBe(true)
532+
expect(result.current.state.matches("idle", "loading", "error")).toBe(true)
533+
534+
await send({ type: "SUCCESS" })
535+
expect(result.current.state.matches("success", "error")).toBe(true)
536+
expect(result.current.state.matches("idle", "loading")).toBe(false)
537+
})
538+
539+
test("same-state transitions with actions", async () => {
540+
const actionSpy = vi.fn()
541+
542+
const machine = createMachine<any>({
543+
initialState() {
544+
return "active"
545+
},
546+
states: {
547+
active: {
548+
on: {
549+
PING: {
550+
target: "active",
551+
actions: ["onPing"],
552+
},
553+
},
554+
},
555+
},
556+
implementations: {
557+
actions: {
558+
onPing: actionSpy,
559+
},
560+
},
561+
})
562+
563+
const { result, send } = renderMachine(machine)
564+
565+
expect(result.current.state.get()).toBe("active")
566+
567+
await send({ type: "PING" })
568+
expect(result.current.state.get()).toBe("active")
569+
expect(actionSpy).toHaveBeenCalledTimes(1)
570+
571+
await send({ type: "PING" })
572+
expect(result.current.state.get()).toBe("active")
573+
expect(actionSpy).toHaveBeenCalledTimes(2)
574+
})
575+
576+
test("event previous/current tracking", async () => {
577+
let capturedPrevious: any = null
578+
let capturedCurrent: any = null
579+
580+
const machine = createMachine<any>({
581+
initialState() {
582+
return "test"
583+
},
584+
states: {
585+
test: {
586+
on: {
587+
FIRST: {
588+
target: "second",
589+
},
590+
SECOND: {
591+
actions: ["captureEvents"],
592+
},
593+
},
594+
},
595+
second: {
596+
on: {
597+
THIRD: {
598+
actions: ["captureEvents"],
599+
},
600+
},
601+
},
602+
},
603+
implementations: {
604+
actions: {
605+
captureEvents({ event }) {
606+
capturedPrevious = event.previous()
607+
capturedCurrent = event.current()
608+
},
609+
},
610+
},
611+
})
612+
613+
const { send } = renderMachine(machine)
614+
615+
await send({ type: "FIRST", data: "first-data" })
616+
await send({ type: "THIRD", data: "third-data" })
617+
618+
expect(capturedCurrent).toMatchObject({ type: "THIRD", data: "third-data" })
619+
expect(capturedPrevious).toMatchObject({ type: "FIRST", data: "first-data" })
620+
})
621+
})

packages/frameworks/react/vite.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { defineConfig } from "vitest/config"
2+
import react from "@vitejs/plugin-react"
23

34
export default defineConfig({
5+
plugins: [react()],
46
test: {
57
retry: 2,
68
globals: true,

packages/frameworks/solid/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@
2626
},
2727
"dependencies": {
2828
"@solid-primitives/keyed": "^1.5.2",
29-
"@zag-js/utils": "workspace:*",
3029
"@zag-js/core": "workspace:*",
3130
"@zag-js/store": "workspace:*",
32-
"@zag-js/types": "workspace:*"
31+
"@zag-js/types": "workspace:*",
32+
"@zag-js/utils": "workspace:*"
3333
},
3434
"devDependencies": {
35-
"@types/jsdom": "^21.1.7",
36-
"solid-js": "1.9.9",
37-
"clean-package": "2.2.0",
3835
"@solidjs/testing-library": "^0.8.10",
3936
"@testing-library/jest-dom": "^6.8.0",
40-
"jsdom": "^27.0.0"
37+
"@types/jsdom": "^21.1.7",
38+
"clean-package": "2.2.0",
39+
"jsdom": "^27.0.0",
40+
"solid-js": "1.9.9",
41+
"vite-plugin-solid": "^2.11.9"
4142
},
4243
"peerDependencies": {
4344
"solid-js": ">=1.1.3"

packages/frameworks/solid/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "../../../tsconfig.json",
3-
"include": ["src", "tests"],
3+
"include": ["src", "tests", "vite.config.ts"],
44
"compilerOptions": {
55
"tsBuildInfoFile": "node_modules/.cache/.tsbuildinfo"
66
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { defineConfig } from "vitest/config"
2+
import solidPlugin from "vite-plugin-solid"
23

34
export default defineConfig({
5+
plugins: [solidPlugin()],
46
test: {
57
retry: 2,
68
globals: true,
79
environment: "jsdom",
810
css: false,
911
setupFiles: "./vitest.setup.ts",
1012
},
13+
resolve: {
14+
conditions: ["development", "browser"],
15+
},
1116
})

pnpm-lock.yaml

Lines changed: 35 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"lib": ["dom", "dom.iterable", "esnext"],
1313
"target": "esnext",
1414
"module": "esnext",
15-
"moduleResolution": "node",
15+
"moduleResolution": "bundler",
1616
"noImplicitReturns": false,
1717
"resolveJsonModule": true,
1818
"exactOptionalPropertyTypes": true,

0 commit comments

Comments
 (0)