|
| 1 | +import { ScrollView as RNScrollView, View as RNView } from "react-native"; |
| 2 | + |
| 3 | +import { generateStateHash } from "../../native/react/rules"; |
| 4 | +import { |
| 5 | + mappingToConfig, |
| 6 | + type ComponentState, |
| 7 | + type Config, |
| 8 | +} from "../../native/react/useNativeCss"; |
| 9 | +import { |
| 10 | + VAR_SYMBOL, |
| 11 | + type Effect, |
| 12 | + type VariableContextValue, |
| 13 | +} from "../../native/reactivity"; |
| 14 | +import type { StyledConfiguration } from "../../runtime.types"; |
| 15 | + |
| 16 | +/** |
| 17 | + * The resolved-style cache is keyed by `generateStateHash`, which hashes `state.configs` by object |
| 18 | + * identity. Sharing therefore depends on one mapping producing one config array, rather than an |
| 19 | + * equal array per consumer. |
| 20 | + * |
| 21 | + * The rules half of that key already holds: `StyleCollection.styles(className)` is keyed by the |
| 22 | + * class string, so every element carrying the same class list references one rule set. The config |
| 23 | + * half is the one input that is minted per consumer. |
| 24 | + */ |
| 25 | + |
| 26 | +/** A stand-in for the rule set a shared class list resolves to — any object is a valid weak key. */ |
| 27 | +const ruleFor = (): WeakKey => ({}); |
| 28 | + |
| 29 | +/** The smallest `ComponentState` `generateStateHash` reads: it only touches `configs`. */ |
| 30 | +const stateFor = (configs: Config[]): ComponentState => { |
| 31 | + const observers = new Set<Effect>(); |
| 32 | + const ruleEffect: Effect = { observers, run: () => undefined }; |
| 33 | + const inheritedVariables: VariableContextValue = { [VAR_SYMBOL]: true }; |
| 34 | + |
| 35 | + return { |
| 36 | + configs, |
| 37 | + inheritedContainers: {}, |
| 38 | + inheritedVariables, |
| 39 | + ruleEffect, |
| 40 | + ruleEffectGetter: (observable) => observable.get(ruleEffect), |
| 41 | + styleEffect: { observers, run: () => undefined }, |
| 42 | + }; |
| 43 | +}; |
| 44 | + |
| 45 | +const viewMapping = { |
| 46 | + className: "style", |
| 47 | +} satisfies StyledConfiguration<typeof RNView>; |
| 48 | + |
| 49 | +const scrollViewMapping = { |
| 50 | + className: "style", |
| 51 | + contentContainerClassName: "contentContainerStyle", |
| 52 | +} satisfies StyledConfiguration<typeof RNScrollView>; |
| 53 | + |
| 54 | +test("two consumers of one mapping share a cache key", () => { |
| 55 | + const rules = [ruleFor()]; |
| 56 | + |
| 57 | + const first = generateStateHash( |
| 58 | + stateFor(mappingToConfig(viewMapping)), |
| 59 | + rules, |
| 60 | + ); |
| 61 | + const second = generateStateHash( |
| 62 | + stateFor(mappingToConfig(viewMapping)), |
| 63 | + rules, |
| 64 | + ); |
| 65 | + |
| 66 | + expect(first).toBe(second); |
| 67 | +}); |
| 68 | + |
| 69 | +test("mappings that differ keep different cache keys", () => { |
| 70 | + const rules = [ruleFor()]; |
| 71 | + |
| 72 | + const view = generateStateHash(stateFor(mappingToConfig(viewMapping)), rules); |
| 73 | + const scrollView = generateStateHash( |
| 74 | + stateFor(mappingToConfig(scrollViewMapping)), |
| 75 | + rules, |
| 76 | + ); |
| 77 | + |
| 78 | + expect(view).not.toBe(scrollView); |
| 79 | +}); |
| 80 | + |
| 81 | +test("a state hash always carries the config, so it is never the empty string", () => { |
| 82 | + // The empty string used to double as a no-keys sentinel in `generateStateHash`. With the key a |
| 83 | + // join rather than a digest, an empty key list renders as the empty string too — so the sentinel |
| 84 | + // and a real state would have shared one cache entry. The config is an unconditional key, which |
| 85 | + // is what makes the sentinel unnecessary rather than merely unlikely. |
| 86 | + const state = stateFor(mappingToConfig(viewMapping)); |
| 87 | + |
| 88 | + expect(generateStateHash(state, [])).not.toBe(""); |
| 89 | + expect(generateStateHash(state, [ruleFor()])).not.toBe(""); |
| 90 | +}); |
| 91 | + |
| 92 | +test("a mapping that is not an object is refused by name", () => { |
| 93 | + // The derivation is cached on the mapping OBJECT. A primitive cannot be a weak-map key, so |
| 94 | + // without this guard the failure surfaces as a `WeakMap` error naming nothing the caller wrote. |
| 95 | + expect(() => mappingToConfig("style" as never)).toThrow( |
| 96 | + /mapping must be an object/u, |
| 97 | + ); |
| 98 | + expect(() => mappingToConfig(undefined as never)).toThrow( |
| 99 | + /mapping must be an object/u, |
| 100 | + ); |
| 101 | +}); |
| 102 | + |
| 103 | +test("a mapping mutated after its first use is not re-derived", () => { |
| 104 | + // Documented rather than defended: `useCssElement` already froze the derivation per instance, so |
| 105 | + // a mutation only ever reached NEWLY mounted elements — the same mapping meaning two things at |
| 106 | + // once. Deriving once per mapping settles it on one. |
| 107 | + const mapping: Record<string, string> = { className: "style" }; |
| 108 | + const first = mappingToConfig(mapping); |
| 109 | + |
| 110 | + mapping.className = "contentContainerStyle"; |
| 111 | + |
| 112 | + expect(mappingToConfig(mapping)).toBe(first); |
| 113 | +}); |
| 114 | + |
| 115 | +test("a mapping built per call still produces an equal config", () => { |
| 116 | + // Every wrapper the library ships passes a module constant, but a caller may build the mapping |
| 117 | + // inline. That path cannot share on identity and has to keep working unchanged. |
| 118 | + expect(mappingToConfig({ className: "style" })).toEqual( |
| 119 | + mappingToConfig(viewMapping), |
| 120 | + ); |
| 121 | +}); |
0 commit comments