import { describe, it, expect, vi } from 'vitest'
import { render } from '@testing-library/react'
import { WidgetContext, useWidgetId } from './widget-context'
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = (): void => {}
function Probe({ onId }: { onId: (id: string) => void }) {
onId(useWidgetId())
return null
}
describe('useWidgetId', () => {
it('throws a clear error when called outside a WidgetContext', () => {
const spy = vi.spyOn(console, 'error').mockImplementation(noop)
expect(() => render()).toThrow(
/useWidgetId\(\) must be called inside /,
)
spy.mockRestore()
})
it('returns the id from the surrounding context', () => {
let captured = ''
render(
(captured = id)} />
,
)
expect(captured).toBe('test-id')
})
it('isolates ids across sibling Providers', () => {
const ids: string[] = []
render(
<>
ids.push(id)} />
ids.push(id)} />
>,
)
expect(ids).toEqual(['a', 'b'])
})
it('inner Provider value shadows outer', () => {
let captured = ''
render(
(captured = id)} />
,
)
expect(captured).toBe('inner')
})
})