This commit increases strictnes of tests by failing on tests (even though they pass) if `console.warn` or `console.error` is used. This is used to fix warning outputs from Vue, cleaning up test output and preventing potential issues with tests. This commit fixes all of the failing tests, including refactoring in code to make them more testable through injecting Vue lifecycle hook function stubs. This removes `shallowMount`ing done on places, improving the speed of executing unit tests. It also reduces complexity and increases maintainability by removing `@vue/test-utils` dependency for these tests. Changes: - Register global hook for all tests to fail if console.error or console.warn is being used. - Fix all issues with failing tests. - Create test helper function for running code in a wrapper component to run code in reliable/unified way to surpress Vue warnings about code not running inside `setup`.
67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
import {
|
|
onBeforeMount, onBeforeUnmount,
|
|
watch, type Ref,
|
|
} from 'vue';
|
|
import { throttle, type ThrottleFunction } from '@/application/Common/Timing/Throttle';
|
|
import { useResizeObserverPolyfill } from './UseResizeObserverPolyfill';
|
|
import { useAnimationFrameLimiter } from './UseAnimationFrameLimiter';
|
|
import type { LifecycleHook } from '../Common/LifecycleHook';
|
|
|
|
export function useResizeObserver(
|
|
config: ResizeObserverConfig,
|
|
usePolyfill = useResizeObserverPolyfill,
|
|
useFrameLimiter = useAnimationFrameLimiter,
|
|
throttler: ThrottleFunction = throttle,
|
|
onSetup: LifecycleHook = onBeforeMount,
|
|
onTeardown: LifecycleHook = onBeforeUnmount,
|
|
) {
|
|
const { resetNextFrame, cancelNextFrame } = useFrameLimiter();
|
|
// This prevents the 'ResizeObserver loop completed with undelivered notifications' error when
|
|
// the browser can't process all observations within one animation frame.
|
|
// Reference: https://github.com/WICG/resize-observer/issues/38
|
|
|
|
const { resizeObserverReady } = usePolyfill();
|
|
// This ensures compatibility with ancient browsers. All modern browsers support ResizeObserver.
|
|
// Compatibility info: https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver#browser_compatibility
|
|
|
|
const throttledCallback = throttler(config.observeCallback, config.throttleInMs);
|
|
// Throttling enhances performance during rapid changes such as window resizing.
|
|
|
|
let observer: ResizeObserver | null;
|
|
|
|
const disposeObserver = () => {
|
|
cancelNextFrame();
|
|
observer?.disconnect();
|
|
observer = null;
|
|
};
|
|
|
|
onSetup(() => {
|
|
watch(() => config.observedElementRef.value, (element) => {
|
|
if (!element) {
|
|
disposeObserver();
|
|
return;
|
|
}
|
|
resizeObserverReady.then((createObserver) => {
|
|
disposeObserver();
|
|
observer = createObserver((...args) => {
|
|
resetNextFrame(() => throttledCallback(...args));
|
|
});
|
|
observer.observe(element, config?.observeOptions);
|
|
});
|
|
}, { immediate: true });
|
|
});
|
|
|
|
onTeardown(() => {
|
|
disposeObserver();
|
|
});
|
|
}
|
|
|
|
export interface ResizeObserverConfig {
|
|
readonly observedElementRef: ObservedElementReference;
|
|
readonly throttleInMs: number;
|
|
readonly observeCallback: ResizeObserverCallback;
|
|
readonly observeOptions?: ResizeObserverOptions;
|
|
}
|
|
|
|
export type ObservedElementReference = Readonly<Ref<HTMLElement | undefined>>;
|