|
| 1 | +import * as React from "react"; |
| 2 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 3 | +import { render } from "@testing-library/react"; |
| 4 | +import { template, useSlot, type SlotChildren } from "../src"; |
| 5 | + |
| 6 | +describe("Console warnings with React 19", () => { |
| 7 | + beforeEach(() => { |
| 8 | + // Spy on console methods |
| 9 | + vi.spyOn(console, "warn"); |
| 10 | + vi.spyOn(console, "error"); |
| 11 | + vi.spyOn(console, "log"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should not log any warnings or errors when using default slots with various children configurations", () => { |
| 15 | + const TestComponent = (props: { children: SlotChildren }) => { |
| 16 | + const { slot } = useSlot(props.children); |
| 17 | + return <div>{slot.default()}</div>; |
| 18 | + }; |
| 19 | + |
| 20 | + // Test single child |
| 21 | + render( |
| 22 | + <TestComponent> |
| 23 | + <div>Test content</div> |
| 24 | + </TestComponent>, |
| 25 | + ); |
| 26 | + |
| 27 | + // Test multiple children |
| 28 | + render( |
| 29 | + <TestComponent> |
| 30 | + <div>Test content 1</div> |
| 31 | + <div>Test content 2</div> |
| 32 | + text node |
| 33 | + </TestComponent>, |
| 34 | + ); |
| 35 | + |
| 36 | + // Test single template |
| 37 | + render( |
| 38 | + <TestComponent> |
| 39 | + <template.default> |
| 40 | + <div>Template content</div> |
| 41 | + </template.default> |
| 42 | + </TestComponent>, |
| 43 | + ); |
| 44 | + |
| 45 | + // Test multiple templates with mixed content |
| 46 | + render( |
| 47 | + <TestComponent> |
| 48 | + <template.default> |
| 49 | + <div>Template 1</div> |
| 50 | + text node |
| 51 | + </template.default> |
| 52 | + <template.default> |
| 53 | + <div>Template 2</div> |
| 54 | + </template.default> |
| 55 | + <template.default>{() => <div>Template 3</div>}</template.default> |
| 56 | + {() => <div>Template 4</div>} |
| 57 | + </TestComponent>, |
| 58 | + ); |
| 59 | + |
| 60 | + // Check that no console methods were called across all renders |
| 61 | + expect(console.warn).not.toHaveBeenCalled(); |
| 62 | + expect(console.error).not.toHaveBeenCalled(); |
| 63 | + expect(console.log).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should not log any warnings or errors when using named slots with various configurations", () => { |
| 67 | + // Test component with optional and required named slots |
| 68 | + const TestComponent1 = (props: { children: SlotChildren }) => { |
| 69 | + const { slot, hasSlot } = useSlot(props.children); |
| 70 | + return ( |
| 71 | + <div> |
| 72 | + {hasSlot.header && slot.header()} |
| 73 | + {slot.content()} |
| 74 | + {slot.footer?.()} |
| 75 | + </div> |
| 76 | + ); |
| 77 | + }; |
| 78 | + |
| 79 | + // Test component with multiple instances of the same named slot |
| 80 | + const TestComponent2 = (props: { children: SlotChildren }) => { |
| 81 | + const { slot } = useSlot(props.children); |
| 82 | + return <div>{slot.foo()}</div>; |
| 83 | + }; |
| 84 | + |
| 85 | + // Test optional and required named slots |
| 86 | + render( |
| 87 | + <TestComponent1> |
| 88 | + <template.header> |
| 89 | + <div>Header</div> |
| 90 | + </template.header> |
| 91 | + <template.content> |
| 92 | + <div>Content</div> |
| 93 | + </template.content> |
| 94 | + </TestComponent1>, |
| 95 | + ); |
| 96 | + |
| 97 | + // Test multiple instances of the same named slot with mixed syntax |
| 98 | + render( |
| 99 | + <TestComponent2> |
| 100 | + <template.foo> |
| 101 | + <div>Foo Template 1</div> |
| 102 | + </template.foo> |
| 103 | + <template.foo> |
| 104 | + <div>Foo Template 2</div> |
| 105 | + </template.foo> |
| 106 | + <div slot-name="foo">Foo Direct</div> |
| 107 | + </TestComponent2>, |
| 108 | + ); |
| 109 | + |
| 110 | + // Check that no console methods were called across all renders |
| 111 | + expect(console.warn).not.toHaveBeenCalled(); |
| 112 | + expect(console.error).not.toHaveBeenCalled(); |
| 113 | + expect(console.log).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments