Files
gui/pkgs/ui/components-test/Image.native.test.tsx
zeekayandHanzo Dev 69c6e860d7 transplant onto tamagui v2.6.0, brand as a re-runnable transform
This repo could never sync. There was NO merge-base with tamagui — 372 commits
here against 15,791 there, unrelated histories — because it began as a
copy-paste rebrand with a flattened layout. Every "sync" since was a hand
re-application that drifted, which is why the tree sat on 7.3.1 while upstream
shipped fixes we then rediscovered by hand.

This commit's PARENT IS TAMAGUI. A future sync is `git merge tama/main` then
`node scripts/rebrand.mjs` — a real merge, because the history is now shared.

174 packages: 166 upstream, our 8 restored (chrome, shell, telemetry, recipes,
get, font-geist-{sans,mono}, @hanzo/booker), bento-get gained. 170/170 build.

Our patches to files UPSTREAM ALSO OWNS are carried, which the package-level
delta missed and which was where the value actually was:
  - useMedia's server snapshot (`initState ?? getMedia()`). Upstream still
    returns undefined there, so every Tamagui Dialog kills a prerender — the
    exact crash hanzo.app spent a day on.
  - data-slot on web host nodes, absent upstream entirely.
Both applied ONTO upstream's newer files (it rewrote useMedia +150/-63 and
createComponent +160/-55), marked `PATCH (ours, carried across the transplant)`.

Seven traps are encoded in scripts/rebrand.mjs, each found by a gate:
skip itself and lockfiles; special-case the double-branded names in BOTH scoped
and bare form; map path strings, not just paths; drop a level for the flattened
group; check disk rather than a stale index.

Rust build artifacts are NOT committed. An earlier draft of this commit swept
apps/desktop/src-tauri/target/debug in via `git add -A` — 3,859 files including
a 272MB .a, over GitHub's 100MB cap, so the push could never have landed. They
are gitignored now.

Co-Authored-By: Hanzo Dev <[email protected]>
2026-07-30 18:25:06 -07:00

167 lines
4.3 KiB
TypeScript

import { getDefaultGuiConfig } from '@hanzogui/config-default'
import { createImage } from '@hanzogui/image'
import { GuiProvider, createGui, setMediaState } from '@hanzogui/core'
import React from 'react'
import TestRenderer, { act } from 'react-test-renderer'
import { afterEach, expect, test, vi } from 'vitest'
const conf = createGui(getDefaultGuiConfig('native'))
const HostImage = React.forwardRef<any, any>((props, ref) =>
React.createElement('image-host', { ...props, ref })
)
const Image = createImage({ Component: HostImage })
afterEach(() => {
setMediaState({})
})
function flattenStyle(style: any): Record<string, any> {
if (Array.isArray(style)) {
return Object.assign({}, ...style.map(flattenStyle))
}
return style || {}
}
test('Image resolves Gui styles and preserves native image props', async () => {
const source = { uri: 'https://example.com/image.png' }
const onLoad = vi.fn()
let rendered: TestRenderer.ReactTestRenderer
setMediaState({ sm: true } as any)
await act(async () => {
rendered = TestRenderer.create(
<GuiProvider config={conf} defaultTheme="light">
<Image
source={source}
objectFit="contain"
position="absolute"
t={1}
l={2}
r={3}
b={4}
bg="$background"
br="$4"
aspectRatio={2}
$sm={{ opacity: 0.5 }}
accessibilityLabel="test image"
onLoad={onLoad}
/>
</GuiProvider>
)
})
const hostImage = rendered!.root.findByType('image-host')
const style = flattenStyle(hostImage.props.style)
expect(style).toMatchObject({
position: 'absolute',
top: 1,
left: 2,
right: 3,
bottom: 4,
aspectRatio: 2,
opacity: 0.5,
})
expect(style.backgroundColor).toBeTruthy()
expect(style.borderTopLeftRadius).toBeTypeOf('number')
expect(style.borderTopRightRadius).toBe(style.borderTopLeftRadius)
expect(style.borderBottomLeftRadius).toBe(style.borderTopLeftRadius)
expect(style.borderBottomRightRadius).toBe(style.borderTopLeftRadius)
expect(hostImage.props.source).toEqual(source)
expect(hostImage.props.resizeMode).toBe('contain')
expect(hostImage.props.accessibilityLabel).toBe('test image')
await act(async () => {
hostImage.props.onLoad({
nativeEvent: {
source: {
width: 100,
height: 50,
},
},
})
})
expect(onLoad).toHaveBeenCalledWith({
target: {
naturalHeight: 50,
naturalWidth: 100,
},
type: 'load',
})
await act(async () => {
rendered!.unmount()
})
})
test('Image prefers src when both source props are provided', async () => {
let rendered: TestRenderer.ReactTestRenderer
await act(async () => {
rendered = TestRenderer.create(
<GuiProvider config={conf} defaultTheme="light">
<Image
src="https://example.com/preferred.png"
source={{ uri: 'https://example.com/deprecated.png' }}
/>
</GuiProvider>
)
})
expect(rendered!.root.findByType('image-host').props.source).toMatchObject({
uri: 'https://example.com/preferred.png',
})
await act(async () => {
rendered!.unmount()
})
})
test('Image keeps layout dimensions out of native source metadata', async () => {
let rendered: TestRenderer.ReactTestRenderer
await act(async () => {
rendered = TestRenderer.create(
<GuiProvider config={conf} defaultTheme="light">
<>
<Image
testID="numeric"
src="https://example.com/numeric.png"
width={120}
height={80}
/>
<Image
testID="percentage"
src="https://example.com/percentage.png"
width="100%"
height="50%"
/>
</>
</GuiProvider>
)
})
const [numericImage, percentageImage] = rendered!.root.findAllByType('image-host')
expect(flattenStyle(numericImage.props.style)).toMatchObject({
width: 120,
height: 80,
})
expect(numericImage.props.source).toEqual({
uri: 'https://example.com/numeric.png',
})
expect(flattenStyle(percentageImage.props.style)).toMatchObject({
width: '100%',
height: '50%',
})
expect(percentageImage.props.source).toEqual({
uri: 'https://example.com/percentage.png',
})
await act(async () => {
rendered!.unmount()
})
})