|
| 1 | +import { Activity } from "react"; |
| 2 | + |
| 3 | +import { act, render } from "@testing-library/react-native"; |
| 4 | +import { View } from "react-native-css/components/View"; |
| 5 | +import { registerCSS, testID } from "react-native-css/jest"; |
| 6 | +import { colorScheme } from "react-native-css/runtime"; |
| 7 | + |
| 8 | +/** |
| 9 | + * <Activity mode="hidden"> renders children without mounting effects, so |
| 10 | + * conditions may change between useNativeCss's state initializer and its |
| 11 | + * first effect setup. |
| 12 | + */ |
| 13 | + |
| 14 | +function Fixture({ className }: { className: string }) { |
| 15 | + return <View testID={testID} className={className} />; |
| 16 | +} |
| 17 | + |
| 18 | +function ActivityFixture({ |
| 19 | + hidden, |
| 20 | + className, |
| 21 | +}: { |
| 22 | + hidden: boolean; |
| 23 | + className: string; |
| 24 | +}) { |
| 25 | + return ( |
| 26 | + <Activity mode={hidden ? "hidden" : "visible"}> |
| 27 | + <Fixture className={className} /> |
| 28 | + </Activity> |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +test("pre-rendered hidden Activity shows current styles on first show and stays reactive", () => { |
| 33 | + registerCSS(` |
| 34 | +.audit-activity-prerender { color: blue; } |
| 35 | +
|
| 36 | +@media (prefers-color-scheme: dark) { |
| 37 | + .audit-activity-prerender { color: red; } |
| 38 | +} |
| 39 | +`); |
| 40 | + |
| 41 | + const screen = render( |
| 42 | + <ActivityFixture hidden className="audit-activity-prerender" />, |
| 43 | + ); |
| 44 | + |
| 45 | + // Effects mount for the first time |
| 46 | + screen.rerender( |
| 47 | + <ActivityFixture hidden={false} className="audit-activity-prerender" />, |
| 48 | + ); |
| 49 | + expect(screen.getByTestId(testID).props.style).toStrictEqual({ |
| 50 | + color: "#00f", |
| 51 | + }); |
| 52 | + |
| 53 | + // Subscriptions are live |
| 54 | + act(() => { |
| 55 | + colorScheme.set("dark"); |
| 56 | + }); |
| 57 | + expect(screen.getByTestId(testID).props.style).toStrictEqual({ |
| 58 | + color: "#f00", |
| 59 | + }); |
| 60 | + |
| 61 | + screen.unmount(); |
| 62 | +}); |
| 63 | + |
| 64 | +test("applies condition changes made while pre-rendered hidden, before first show", () => { |
| 65 | + registerCSS(` |
| 66 | +.audit-activity-stale { color: blue; } |
| 67 | +
|
| 68 | +@media (prefers-color-scheme: dark) { |
| 69 | + .audit-activity-stale { color: red; } |
| 70 | +} |
| 71 | +`); |
| 72 | + |
| 73 | + const screen = render( |
| 74 | + <ActivityFixture hidden className="audit-activity-stale" />, |
| 75 | + ); |
| 76 | + |
| 77 | + // Change conditions after the initializer, before effects mount |
| 78 | + act(() => { |
| 79 | + colorScheme.set("dark"); |
| 80 | + }); |
| 81 | + |
| 82 | + screen.rerender( |
| 83 | + <ActivityFixture hidden={false} className="audit-activity-stale" />, |
| 84 | + ); |
| 85 | + |
| 86 | + expect(screen.getByTestId(testID).props.style).toStrictEqual({ |
| 87 | + color: "#f00", |
| 88 | + }); |
| 89 | + |
| 90 | + screen.unmount(); |
| 91 | +}); |
| 92 | + |
| 93 | +test("unrelated condition changes during the pre-render window do not force a catch-up render", () => { |
| 94 | + registerCSS(` |
| 95 | +.audit-activity-unrelated { color: blue; } |
| 96 | +`); |
| 97 | + |
| 98 | + // colorScheme is not a dependency, so changing it should not force a re-render |
| 99 | + let renders = 0; |
| 100 | + function CountingFixture({ className }: { className: string }) { |
| 101 | + renders++; |
| 102 | + return <View testID={testID} className={className} />; |
| 103 | + } |
| 104 | + |
| 105 | + const screen = render( |
| 106 | + <Activity mode="hidden"> |
| 107 | + <CountingFixture className="audit-activity-unrelated" /> |
| 108 | + </Activity>, |
| 109 | + ); |
| 110 | + |
| 111 | + expect(renders).toBe(1); |
| 112 | + |
| 113 | + act(() => { |
| 114 | + colorScheme.set("dark"); |
| 115 | + }); |
| 116 | + |
| 117 | + screen.rerender( |
| 118 | + <Activity mode="visible"> |
| 119 | + <CountingFixture className="audit-activity-unrelated" /> |
| 120 | + </Activity>, |
| 121 | + ); |
| 122 | + |
| 123 | + expect(screen.getByTestId(testID).props.style).toStrictEqual({ |
| 124 | + color: "#00f", |
| 125 | + }); |
| 126 | + |
| 127 | + // No extra catch-up render |
| 128 | + expect(renders).toBe(2); |
| 129 | + |
| 130 | + screen.unmount(); |
| 131 | +}); |
| 132 | + |
| 133 | +test("applies condition changes made while hidden, after a visible mount (mid-life replay)", () => { |
| 134 | + registerCSS(` |
| 135 | +.audit-activity-midlife { color: blue; } |
| 136 | +
|
| 137 | +@media (prefers-color-scheme: dark) { |
| 138 | + .audit-activity-midlife { color: red; } |
| 139 | +} |
| 140 | +`); |
| 141 | + |
| 142 | + const screen = render( |
| 143 | + <ActivityFixture hidden={false} className="audit-activity-midlife" />, |
| 144 | + ); |
| 145 | + expect(screen.getByTestId(testID).props.style).toStrictEqual({ |
| 146 | + color: "#00f", |
| 147 | + }); |
| 148 | + |
| 149 | + // Hiding keeps state but cleans up effects |
| 150 | + screen.rerender( |
| 151 | + <ActivityFixture hidden className="audit-activity-midlife" />, |
| 152 | + ); |
| 153 | + |
| 154 | + act(() => { |
| 155 | + colorScheme.set("dark"); |
| 156 | + }); |
| 157 | + |
| 158 | + screen.rerender( |
| 159 | + <ActivityFixture hidden={false} className="audit-activity-midlife" />, |
| 160 | + ); |
| 161 | + expect(screen.getByTestId(testID).props.style).toStrictEqual({ |
| 162 | + color: "#f00", |
| 163 | + }); |
| 164 | + |
| 165 | + screen.unmount(); |
| 166 | +}); |
0 commit comments