8000 test(react-dom/observe): add fallback tests for 'undefined' 'IntersectionObserver.thresholds' by sukvvon · Pull Request #1580 · toss/suspensive · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

test(react-dom/observe): add fallback tests for 'undefined' 'IntersectionObserver.thresholds' #1580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions packages/react-dom/src/utils/observe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,99 @@ it('should convert options to id', () => {
).toMatchInlineSnapshot(`"threshold_0,0.5,1"`)
})

it('should fallback to [threshold] when observer.thresholds is undefined and threshold is a number', () => {
const element = document.createElement('div')
const cb = vi.fn()
const mockObserve = vi.fn()
const mockUnobserve = vi.fn()
const mockDisconnect = vi.fn()

let recordedThresholds: number[] | undefined

class FakeObserver {
thresholds?: undefined
constructor(_: IntersectionObserverCallback, options?: IntersectionObserverInit) {
recordedThresholds = Array.isArray(options?.threshold) ? options.threshold : [options?.threshold ?? 0]
}
observe = mockObserve
unobserve = mockUnobserve
disconnect = mockDisconnect
}

const originalObserver = globalThis.IntersectionObserver
globalThis.IntersectionObserver = FakeObserver as unknown as typeof IntersectionObserver

const unobserve = observe(element, cb, { threshold: 0.7 })

expect(recordedThresholds).toEqual([0.7])
expect(mockObserve).toHaveBeenCalledWith(element)

unobserve()
globalThis.IntersectionObserver = originalObserver
})

it('should fallback to [threshold] when observer.thresholds is undefined and threshold is an array', () => {
const element = document.createElement('div')
const cb = vi.fn()
const mockObserve = vi.fn()
const mockUnobserve = vi.fn()
const mockDisconnect = vi.fn()

let recordedThresholds: number[] | undefined

class FakeObserver {
thresholds?: undefined
constructor(_: IntersectionObserverCallback, options?: IntersectionObserverInit) {
recordedThresholds = Array.isArray(options?.threshold) ? options.threshold : [options?.threshold ?? 0]
}
observe = mockObserve
unobserve = mockUnobserve
disconnect = mockDisconnect
}

const originalObserver = globalThis.IntersectionObserver
globalThis.IntersectionObserver = FakeObserver as unknown as typeof IntersectionObserver

const unobserve = observe(element, cb, { threshold: [0.1, 0.5, 0.9] })

expect(recordedThresholds).toEqual([0.1, 0.5, 0.9])
expect(mockObserve).toHaveBeenCalledWith(element)

unobserve()
globalThis.IntersectionObserver = originalObserver
})

it('should fallback to [0] when observer.thresholds is undefined and threshold is not set', () => {
const element = document.createElement('div')
const cb = vi.fn()
const mockObserve = vi.fn()
const mockUnobserve = vi.fn()
const mockDisconnect = vi.fn()

let recordedThresholds: number[] | undefined

class FakeObserver {
thresholds?: undefined
constructor(_: IntersectionObserverCallback, options?: IntersectionObserverInit) {
recordedThresholds = Array.isArray(options?.threshold) ? options.threshold : [options?.threshold ?? 0]
}
observe = mockObserve
unobserve = mockUnobserve
disconnect = mockDisconnect
}

const originalObserver = globalThis.IntersectionObserver
globalThis.IntersectionObserver = FakeObserver as unknown as typeof IntersectionObserver

const unobserve = observe(element, cb)

expect(recordedThresholds).toEqual([0])
expect(mockObserve).toHaveBeenCalledWith(element)

unobserve()
globalThis.IntersectionObserver = originalObserver
})

it('should use fallback intersectionRatio when IntersectionObserver is undefined', () => {
const element = document.createElement('div')
const cb = vi.fn()
Expand Down
Loading
0