|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * Copyright (c) 2017 Kent C. Dodds |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + */ |
| 15 | + |
| 16 | +import type { ExpectationResult, MatcherState } from '@vitest/expect' |
| 17 | +import type { Locator } from '../locators' |
| 18 | +import { getElementFromUserInput } from './utils' |
| 19 | + |
| 20 | +export default function toBeInViewport( |
| 21 | + this: MatcherState, |
| 22 | + actual: Element | Locator, |
| 23 | + options?: { ratio?: number }, |
| 24 | +): ExpectationResult { |
| 25 | + const htmlElement = getElementFromUserInput(actual, toBeInViewport, this) |
| 26 | + |
| 27 | + const expectedRatio = options?.ratio ?? 0 |
| 28 | + return getViewportIntersection(htmlElement, expectedRatio).then(({ pass, ratio }) => { |
| 29 | + return { |
| 30 | + pass, |
| 31 | + message: () => { |
| 32 | + const is = pass ? 'is' : 'is not' |
| 33 | + const ratioText = expectedRatio > 0 ? ` with ratio ${expectedRatio}` : '' |
| 34 | + const actualRatioText = ratio !== undefined ? ` (actual ratio: ${ratio.toFixed(3)})` : '' |
| 35 | + return [ |
| 36 | + this.utils.matcherHint( |
| 37 | + `${this.isNot ? '.not' : ''}.toBeInViewport`, |
| 38 | + 'element', |
| 39 | + '', |
| 40 | + ), |
| 41 | + '', |
| 42 | + `Received element ${is} in viewport${ratioText}${actualRatioText}:`, |
| 43 | + ` ${this.utils.printReceived(htmlElement.cloneNode(false))}`, |
| 44 | + ].join('\n') |
| 45 | + }, |
| 46 | + } |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Get viewport intersection ratio using IntersectionObserver API |
| 52 | + * This implementation follows Playwright's approach using IntersectionObserver as the primary mechanism |
| 53 | + */ |
| 54 | +async function getViewportIntersection(element: HTMLElement | SVGElement, expectedRatio: number): Promise<{ pass: boolean; ratio?: number }> { |
| 55 | + // Use IntersectionObserver API to get the intersection ratio |
| 56 | + // Following Playwright's exact pattern from viewportRatio function |
| 57 | + const intersectionRatio = await new Promise<number>((resolve) => { |
| 58 | + // This mimics Playwright's Promise-based implementation in a synchronous context |
| 59 | + const observer = new IntersectionObserver((entries) => { |
| 60 | + if (entries.length > 0) { |
| 61 | + resolve(entries[0].intersectionRatio) |
| 62 | + } |
| 63 | + else { |
| 64 | + resolve(0) |
| 65 | + } |
| 66 | + observer.disconnect() |
| 67 | + }) |
| 68 | + |
| 69 | + observer.observe(element) |
| 70 | + |
| 71 | + // Firefox workaround: requestAnimationFrame to ensure observer callback fires |
| 72 | + // This is exactly how Playwright handles it |
| 73 | + requestAnimationFrame(() => {}) |
| 74 | + }) |
| 75 | + |
| 76 | + // Apply the same logic as Playwright: |
| 77 | + // ratio > 0 && ratio > (expectedRatio - 1e-9) |
| 78 | + const pass = intersectionRatio > 0 && intersectionRatio > (expectedRatio - 1e-9) |
| 79 | + |
| 80 | + return { pass, ratio: intersectionRatio } |
| 81 | +} |
0 commit comments