Skip to content

Commit ac2a4fe

Browse files
committed
test: update test suite for fiber architecture
Add fiber-specific tests (rendering, reconciliation, hooks, effects, commit, context, events, portals, memo, refs, errors, internals). Update existing integration tests for fiber-based API. Remove old reconciler tests.
1 parent 0de43c8 commit ac2a4fe

31 files changed

+2063
-651
lines changed

tests/MiniReact.context.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
render,
66
useContext,
77
useState,
8-
} from "../src/MiniReact";
9-
import type { FunctionalComponent } from "../src/core/types";
8+
} from "@/MiniReact";
9+
import type { FunctionalComponent } from "@/core/types";
1010

1111
describe("MiniReact.Context API", () => {
1212
let container: HTMLElement;

tests/MiniReact.createElement.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "bun:test";
2-
import { createElement } from "../src/MiniReact";
3-
import { TEXT_ELEMENT } from "../src/core/types";
2+
import { createElement } from "@/MiniReact";
3+
import { TEXT_ELEMENT } from "@/core/types";
44

55
describe("MiniReact.createElement", () => {
66
test("should create an element with type and props", () => {

tests/MiniReact.createElementFC.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test } from "bun:test";
2-
import { createElement } from "../src/MiniReact";
3-
import type { InternalTextElement, MiniReactElement } from "../src/core/types";
4-
import { TEXT_ELEMENT } from "../src/core/types";
2+
import { createElement } from "@/MiniReact";
3+
import type { InternalTextElement, MiniReactElement } from "@/core/types";
4+
import { TEXT_ELEMENT } from "@/core/types";
55

66
describe("MiniReact.createElement with Functional Components", () => {
77
const MyComponent = (
@@ -433,7 +433,7 @@ describe("MiniReact.createElement with Functional Components", () => {
433433
"deep-value",
434434
);
435435
expect(
436-
typedProps.deep.level1.level2.level3.level4.level5.array[0].nested,
436+
typedProps.deep.level1.level2.level3.level4.level5.array[0]!.nested,
437437
).toBe("in-array");
438438
});
439439
});

tests/MiniReact.events.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2-
import { createElement, render, useState } from "../src/MiniReact";
3-
import type { FunctionalComponent, SyntheticEvent } from "../src/MiniReact";
2+
import { createElement, render, useState } from "@/MiniReact";
3+
import type { FunctionalComponent, SyntheticEvent } from "@/MiniReact";
44

55
describe("MiniReact.events", () => {
66
let container: HTMLElement;

tests/MiniReact.fragments.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { beforeEach, describe, expect, test } from "bun:test";
2-
import { Fragment, createElement, render, useState } from "../src/MiniReact";
2+
import { Fragment, createElement, render, useState } from "@/MiniReact";
33

44
describe("MiniReact Fragment Tests", () => {
55
let container: HTMLElement;
@@ -181,13 +181,13 @@ describe("MiniReact Fragment Tests", () => {
181181
const button = container.querySelector("button");
182182
const spans = container.querySelectorAll("span");
183183

184-
expect(spans[0].textContent).toBe("Count: ");
185-
expect(spans[1].textContent).toBe("0");
184+
expect(spans[0]!.textContent).toBe("Count: ");
185+
expect(spans[1]!.textContent).toBe("0");
186186

187187
// Simulate click
188188
button?.click();
189189

190-
expect(spans[1].textContent).toBe("1");
190+
expect(spans[1]!.textContent).toBe("1");
191191
});
192192
});
193193

tests/MiniReact.jsx.test.ts

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { beforeEach, describe, expect, test } from "bun:test";
2+
import { Fragment, jsx, jsxDEV, jsxs, render, useState } from "@/MiniReact";
3+
import type { FunctionalComponent, MiniReactElement } from "@/core/types";
24
import { Window } from "happy-dom";
3-
import {
4-
Fragment,
5-
jsx,
6-
jsxDEV,
7-
jsxs,
8-
render,
9-
useState,
10-
} from "../src/MiniReact";
11-
import type { FunctionalComponent, MiniReactElement } from "../src/core/types";
125

136
let window: Window;
147
let document: Document;
@@ -30,7 +23,7 @@ describe("JSX Runtime Functions", () => {
3023
const element = jsx("div", { id: "test" }) as MiniReactElement;
3124

3225
expect(element.type).toBe("div");
33-
expect((element.props as Record<string, unknown>).id).toBe("test");
26+
expect((element.props as Record<string, unknown>)["id"]).toBe("test");
3427
expect(element.props.children).toEqual([]);
3528
});
3629

@@ -52,8 +45,8 @@ describe("JSX Runtime Functions", () => {
5245
test("jsx() handles key prop", () => {
5346
const element = jsx("div", { id: "test" }, "my-key") as MiniReactElement;
5447

55-
expect((element.props as Record<string, unknown>).key).toBe("my-key");
56-
expect((element.props as Record<string, unknown>).id).toBe("test");
48+
expect((element.props as Record<string, unknown>)["key"]).toBe("my-key");
49+
expect((element.props as Record<string, unknown>)["id"]).toBe("test");
5750
});
5851

5952
test("jsxs() works identically to jsx()", () => {
@@ -75,13 +68,13 @@ describe("JSX Runtime Functions", () => {
7568
);
7669

7770
expect(element.type).toBe("div");
78-
expect((element.props as Record<string, unknown>).id).toBe("test");
79-
expect((element.props as Record<string, unknown>).key).toBe("key");
71+
expect((element.props as Record<string, unknown>)["id"]).toBe("test");
72+
expect((element.props as Record<string, unknown>)["key"]).toBe("key");
8073

8174
// Test that jsxDEV creates similar structure
8275
expect(elementWithSource.type).toBe("div");
8376
expect(
84-
(elementWithSource as unknown as Record<string, unknown>).__source,
77+
(elementWithSource as unknown as Record<string, unknown>)["__source"],
8578
).toEqual(source);
8679
});
8780

@@ -133,10 +126,10 @@ describe("JSX Fragment Support", () => {
133126
render(fragmentElement, container);
134127

135128
expect(container.children.length).toBe(2);
136-
expect(container.children[0].tagName).toBe("SPAN");
137-
expect(container.children[0].textContent).toBe("Hello");
138-
expect(container.children[1].tagName).toBe("SPAN");
139-
expect(container.children[1].textContent).toBe("World");
129+
expect(container.children[0]!.tagName).toBe("SPAN");
130+
expect(container.children[0]!.textContent).toBe("Hello");
131+
expect(container.children[1]!.tagName).toBe("SPAN");
132+
expect(container.children[1]!.textContent).toBe("World");
140133
});
141134

142135
test("Fragment with single child", () => {
@@ -147,8 +140,8 @@ describe("JSX Fragment Support", () => {
147140
render(fragmentElement, container);
148141

149142
expect(container.children.length).toBe(1);
150-
expect(container.children[0].tagName).toBe("DIV");
151-
expect(container.children[0].textContent).toBe("Single child");
143+
expect(container.children[0]!.tagName).toBe("DIV");
144+
expect(container.children[0]!.textContent).toBe("Single child");
152145
});
153146

154147
test("Fragment with no children", () => {
@@ -168,9 +161,9 @@ describe("JSX Fragment Support", () => {
168161
render(fragmentElement, container);
169162

170163
expect(container.childNodes.length).toBe(3);
171-
expect(container.childNodes[0].textContent).toBe("Text node");
172-
expect(container.childNodes[1].textContent).toBe("Element");
173-
expect(container.childNodes[2].textContent).toBe("42");
164+
expect(container.childNodes[0]!.textContent).toBe("Text node");
165+
expect(container.childNodes[1]!.textContent).toBe("Element");
166+
expect(container.childNodes[2]!.textContent).toBe("42");
174167
});
175168
});
176169

@@ -185,8 +178,8 @@ describe("JSX with Functional Components", () => {
185178
render(element, container);
186179

187180
expect(container.children.length).toBe(1);
188-
expect(container.children[0].tagName).toBe("H1");
189-
expect(container.children[0].textContent).toBe("Hello, JSX!");
181+
expect(container.children[0]!.tagName).toBe("H1");
182+
expect(container.children[0]!.textContent).toBe("Hello, JSX!");
190183
});
191184

192185
test("jsx() with component children", () => {
@@ -201,8 +194,8 @@ describe("JSX with Functional Components", () => {
201194
render(jsx(App, null), container);
202195

203196
expect(container.children.length).toBe(1);
204-
expect(container.children[0].tagName).toBe("BUTTON");
205-
expect(container.children[0].textContent).toBe("Click me");
197+
expect(container.children[0]!.tagName).toBe("BUTTON");
198+
expect(container.children[0]!.textContent).toBe("Click me");
206199
});
207200

208201
test("jsx() with nested components", () => {
@@ -228,14 +221,14 @@ describe("JSX with Functional Components", () => {
228221

229222
render(app, container);
230223

231-
const card = container.children[0] as HTMLElement;
224+
const card = container.children[0]! as HTMLElement;
232225
expect(card.className).toBe("card");
233226
expect(card.children.length).toBe(2);
234-
expect(card.children[0].tagName).toBe("H2");
235-
expect(card.children[0].textContent).toBe("My Card");
236-
expect(card.children[1].className).toBe("content");
237-
expect(card.children[1].children[0].tagName).toBe("P");
238-
expect(card.children[1].children[0].textContent).toBe("Card content");
227+
expect(card.children[0]!.tagName).toBe("H2");
228+
expect(card.children[0]!.textContent).toBe("My Card");
229+
expect(card.children[1]!.className).toBe("content");
230+
expect(card.children[1]!.children[0]!.tagName).toBe("P");
231+
expect(card.children[1]!.children[0]!.textContent).toBe("Card content");
239232
});
240233
});
241234

@@ -291,10 +284,10 @@ describe("JSX with Hooks", () => {
291284

292285
render(jsx(MultiState, null), container);
293286

294-
const nameP = container.children[0].children[0] as HTMLElement;
295-
const ageP = container.children[0].children[1] as HTMLElement;
296-
const nameButton = container.children[0].children[2] as HTMLElement;
297-
const ageButton = container.children[0].children[3] as HTMLElement;
287+
const nameP = container.children[0]!.children[0]! as HTMLElement;
288+
const ageP = container.children[0]!.children[1]! as HTMLElement;
289+
const nameButton = container.children[0]!.children[2]! as HTMLElement;
290+
const ageButton = container.children[0]!.children[3]! as HTMLElement;
298291

299292
expect(nameP.textContent).toBe("Name: Anonymous");
300293
expect(ageP.textContent).toBe("Age: 0");
@@ -368,7 +361,7 @@ describe("JSX Error Handling", () => {
368361
render(element, container);
369362

370363
expect(container.children.length).toBe(1);
371-
expect(container.children[0].textContent).toBe("text");
364+
expect(container.children[0]!.textContent).toBe("text");
372365
});
373366

374367
test("jsx() handles empty children array", () => {
@@ -377,7 +370,7 @@ describe("JSX Error Handling", () => {
377370
render(element, container);
378371

379372
expect(container.children.length).toBe(1);
380-
expect(container.children[0].children.length).toBe(0);
373+
expect(container.children[0]!.children.length).toBe(0);
381374
});
382375

383376
test("jsx() handles component returning null", () => {
@@ -402,9 +395,9 @@ describe("JSX Performance and Edge Cases", () => {
402395
render(element, container);
403396

404397
expect(container.children.length).toBe(1);
405-
expect(container.children[0].children.length).toBe(100);
406-
expect(container.children[0].children[0].textContent).toBe("Item 0");
407-
expect(container.children[0].children[99].textContent).toBe("Item 99");
398+
expect(container.children[0]!.children.length).toBe(100);
399+
expect(container.children[0]!.children[0]!.textContent).toBe("Item 0");
400+
expect(container.children[0]!.children[99]!.textContent).toBe("Item 99");
408401
});
409402

410403
test("jsx() maintains referential equality for same inputs", () => {
@@ -415,8 +408,8 @@ describe("JSX Performance and Edge Cases", () => {
415408
// Elements should have the same structure but be different objects
416409
expect(element1).not.toBe(element2);
417410
expect(element1.type).toBe(element2.type);
418-
expect((element1.props as Record<string, unknown>).id).toBe(
419-
(element2.props as Record<string, unknown>).id,
411+
expect((element1.props as Record<string, unknown>)["id"]).toBe(
412+
(element2.props as Record<string, unknown>)["id"],
420413
);
421414
});
422415

@@ -425,9 +418,9 @@ describe("JSX Performance and Edge Cases", () => {
425418
const element = jsxDEV("div", { id: "test" }, "key", true, source, {});
426419

427420
expect(element.type).toBe("div");
428-
expect((element.props as Record<string, unknown>).id).toBe("test");
429-
expect((element.props as Record<string, unknown>).key).toBe("key");
430-
expect((element as unknown as Record<string, unknown>).__source).toEqual(
421+
expect((element.props as Record<string, unknown>)["id"]).toBe("test");
422+
expect((element.props as Record<string, unknown>)["key"]).toBe("key");
423+
expect((element as unknown as Record<string, unknown>)["__source"]).toEqual(
431424
source,
432425
);
433426
});

tests/MiniReact.memo.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { beforeEach, describe, expect, test } from "bun:test";
2-
import { createElement, memo, render, useState } from "../src/MiniReact";
2+
import { createElement, memo, render, useState } from "@/MiniReact";
33

44
describe("MiniReact.memo Component Memoization", () => {
55
let container: HTMLElement;

tests/MiniReact.performance.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
startProfiling,
66
stopProfiling,
77
useState,
8-
} from "../src/MiniReact";
8+
} from "@/MiniReact";
99

1010
describe("MiniReact.Performance Tools", () => {
1111
let container: HTMLElement;

tests/MiniReact.portals.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
useContext,
88
useEffect,
99
useState,
10-
} from "../src/MiniReact";
10+
} from "@/MiniReact";
1111

1212
describe("MiniReact Portal Tests", () => {
1313
let container: HTMLElement;

0 commit comments

Comments
 (0)