i更新
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
import type { App } from 'vue';
|
||||
|
||||
import { createApp, h } from 'vue';
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { useLayoutContentStyle } from '../use-layout-style';
|
||||
|
||||
let activeApp: App | undefined;
|
||||
|
||||
afterEach(() => {
|
||||
activeApp?.unmount();
|
||||
activeApp = undefined;
|
||||
document.body.innerHTML = '';
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('useLayoutContentStyle', () => {
|
||||
it('positions overlays without observing content size', () => {
|
||||
const resizeObserver = vi.fn();
|
||||
vi.stubGlobal('ResizeObserver', resizeObserver);
|
||||
|
||||
const host = document.createElement('div');
|
||||
document.body.append(host);
|
||||
|
||||
activeApp = createApp({
|
||||
setup() {
|
||||
const { contentElement, overlayStyle } = useLayoutContentStyle();
|
||||
return () =>
|
||||
h('main', { ref: contentElement }, [
|
||||
h('div', { style: overlayStyle.value }),
|
||||
]);
|
||||
},
|
||||
});
|
||||
activeApp.mount(host);
|
||||
|
||||
const overlay = host.querySelector<HTMLElement>('main > div');
|
||||
expect(overlay).not.toBeNull();
|
||||
if (!overlay) throw new Error('overlay element was not rendered');
|
||||
|
||||
expect(resizeObserver).not.toHaveBeenCalled();
|
||||
expect(overlay.style.position).toBe('absolute');
|
||||
expect(`${overlay.style.inset}`).toBe('0');
|
||||
});
|
||||
});
|
||||
-139
@@ -1,139 +0,0 @@
|
||||
import type { App } from 'vue';
|
||||
|
||||
import { createApp } from 'vue';
|
||||
|
||||
import { CSS_VARIABLE_LAYOUT_VIEWPORT_HEIGHT } from '@vben-core/shared/constants';
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { useLayoutViewportHeight } from '../use-layout-viewport-height';
|
||||
|
||||
let activeApp: App | undefined;
|
||||
|
||||
function stubDvhSupport(supported: boolean) {
|
||||
vi.stubGlobal('CSS', {
|
||||
supports: vi.fn((property: string, value: string) => {
|
||||
return supported && property === 'height' && value === '1dvh';
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
function mountViewportHeight() {
|
||||
const host = document.createElement('div');
|
||||
document.body.append(host);
|
||||
|
||||
activeApp = createApp({
|
||||
setup() {
|
||||
useLayoutViewportHeight();
|
||||
return () => null;
|
||||
},
|
||||
});
|
||||
activeApp.mount(host);
|
||||
}
|
||||
|
||||
function getViewportHeightVar() {
|
||||
return document.documentElement.style.getPropertyValue(
|
||||
CSS_VARIABLE_LAYOUT_VIEWPORT_HEIGHT,
|
||||
);
|
||||
}
|
||||
|
||||
async function flushAnimationFrame() {
|
||||
await new Promise<void>((resolve) => {
|
||||
requestAnimationFrame(() => resolve());
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
activeApp?.unmount();
|
||||
activeApp = undefined;
|
||||
document.body.innerHTML = '';
|
||||
document.documentElement.style.removeProperty(
|
||||
CSS_VARIABLE_LAYOUT_VIEWPORT_HEIGHT,
|
||||
);
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('useLayoutViewportHeight', () => {
|
||||
it('does not write the CSS variable when dvh is supported', () => {
|
||||
const resizeObserver = vi.fn();
|
||||
const addEventListener = vi.spyOn(window, 'addEventListener');
|
||||
vi.stubGlobal('ResizeObserver', resizeObserver);
|
||||
stubDvhSupport(true);
|
||||
|
||||
mountViewportHeight();
|
||||
|
||||
expect(getViewportHeightVar()).toBe('');
|
||||
expect(resizeObserver).not.toHaveBeenCalled();
|
||||
expect(addEventListener).not.toHaveBeenCalledWith(
|
||||
'resize',
|
||||
expect.any(Function),
|
||||
);
|
||||
});
|
||||
|
||||
it('writes innerHeight when dvh is unsupported', () => {
|
||||
const resizeObserver = vi.fn();
|
||||
vi.stubGlobal('ResizeObserver', resizeObserver);
|
||||
stubDvhSupport(false);
|
||||
vi.stubGlobal('visualViewport', null);
|
||||
vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(800);
|
||||
|
||||
mountViewportHeight();
|
||||
|
||||
expect(getViewportHeightVar()).toBe('800px');
|
||||
expect(resizeObserver).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('prefers visualViewport.height over innerHeight', () => {
|
||||
stubDvhSupport(false);
|
||||
vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(800);
|
||||
const visualViewport = new EventTarget() as VisualViewport;
|
||||
Object.defineProperty(visualViewport, 'height', {
|
||||
configurable: true,
|
||||
value: 640,
|
||||
writable: true,
|
||||
});
|
||||
vi.stubGlobal('visualViewport', visualViewport);
|
||||
|
||||
mountViewportHeight();
|
||||
|
||||
expect(getViewportHeightVar()).toBe('640px');
|
||||
});
|
||||
|
||||
it('updates the CSS variable on visualViewport resize and stops after unmount', async () => {
|
||||
stubDvhSupport(false);
|
||||
const visualViewport = new EventTarget() as VisualViewport;
|
||||
Object.defineProperty(visualViewport, 'height', {
|
||||
configurable: true,
|
||||
value: 640,
|
||||
writable: true,
|
||||
});
|
||||
vi.stubGlobal('visualViewport', visualViewport);
|
||||
|
||||
mountViewportHeight();
|
||||
expect(getViewportHeightVar()).toBe('640px');
|
||||
|
||||
Object.defineProperty(visualViewport, 'height', {
|
||||
configurable: true,
|
||||
value: 520,
|
||||
writable: true,
|
||||
});
|
||||
visualViewport.dispatchEvent(new Event('resize'));
|
||||
await flushAnimationFrame();
|
||||
|
||||
expect(getViewportHeightVar()).toBe('520px');
|
||||
|
||||
activeApp?.unmount();
|
||||
activeApp = undefined;
|
||||
|
||||
Object.defineProperty(visualViewport, 'height', {
|
||||
configurable: true,
|
||||
value: 400,
|
||||
writable: true,
|
||||
});
|
||||
visualViewport.dispatchEvent(new Event('resize'));
|
||||
await flushAnimationFrame();
|
||||
|
||||
expect(getViewportHeightVar()).toBe('520px');
|
||||
});
|
||||
});
|
||||
@@ -1,129 +0,0 @@
|
||||
import type { App, WritableComputedRef } from 'vue';
|
||||
|
||||
import { createApp, nextTick } from 'vue';
|
||||
|
||||
import { ELEMENT_ID_LAYOUT_SCROLL } from '@vben-core/shared/constants';
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { SCROLL_FIXED_CLASS, useScrollLock } from '../use-scroll-lock';
|
||||
|
||||
let activeApp: App | undefined;
|
||||
|
||||
function createScrollableElement(id?: string) {
|
||||
const element = document.createElement('div');
|
||||
if (id) {
|
||||
element.id = id;
|
||||
}
|
||||
element.style.overflow = 'auto';
|
||||
Object.defineProperties(element, {
|
||||
clientHeight: { configurable: true, value: 100 },
|
||||
scrollHeight: { configurable: true, value: 120 },
|
||||
});
|
||||
document.body.append(element);
|
||||
return element;
|
||||
}
|
||||
|
||||
function mountScrollLock(options?: { immediate?: boolean }) {
|
||||
let scrollLock: undefined | WritableComputedRef<boolean>;
|
||||
const host = document.createElement('div');
|
||||
document.body.append(host);
|
||||
|
||||
activeApp = createApp({
|
||||
setup() {
|
||||
scrollLock = useScrollLock(options);
|
||||
return () => null;
|
||||
},
|
||||
});
|
||||
activeApp.mount(host);
|
||||
|
||||
if (!scrollLock) {
|
||||
throw new Error('useScrollLock was not initialized');
|
||||
}
|
||||
return scrollLock;
|
||||
}
|
||||
|
||||
async function flushMountedLock() {
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
activeApp?.unmount();
|
||||
activeApp = undefined;
|
||||
document.body.innerHTML = '';
|
||||
document.body.style.cssText = '';
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('useScrollLock', () => {
|
||||
it('should lock the layout scroll element first', async () => {
|
||||
const element = createScrollableElement(ELEMENT_ID_LAYOUT_SCROLL);
|
||||
element.style.setProperty('scrollbar-gutter', 'auto');
|
||||
const scrollLock = mountScrollLock();
|
||||
|
||||
await flushMountedLock();
|
||||
|
||||
expect(scrollLock.value).toBe(true);
|
||||
expect(element.style.overflow).toBe('hidden');
|
||||
expect(element.style.getPropertyValue('scrollbar-gutter')).toBe('stable');
|
||||
expect(document.body.style.overflow).not.toBe('hidden');
|
||||
|
||||
activeApp?.unmount();
|
||||
activeApp = undefined;
|
||||
|
||||
expect(element.style.overflow).toBe('auto');
|
||||
expect(element.style.getPropertyValue('scrollbar-gutter')).toBe('auto');
|
||||
});
|
||||
|
||||
it('should support manual locking', async () => {
|
||||
const element = createScrollableElement(ELEMENT_ID_LAYOUT_SCROLL);
|
||||
const scrollLock = mountScrollLock({ immediate: false });
|
||||
|
||||
await flushMountedLock();
|
||||
expect(scrollLock.value).toBe(false);
|
||||
|
||||
scrollLock.value = true;
|
||||
expect(element.style.overflow).toBe('hidden');
|
||||
expect(element.style.getPropertyValue('scrollbar-gutter')).toBe('stable');
|
||||
|
||||
scrollLock.value = false;
|
||||
expect(element.style.overflow).toBe('auto');
|
||||
expect(element.style.getPropertyValue('scrollbar-gutter')).toBe('');
|
||||
});
|
||||
|
||||
it('should fall back to body and compensate fixed nodes', async () => {
|
||||
document.body.style.overflow = 'auto';
|
||||
Object.defineProperties(document.body, {
|
||||
clientHeight: { configurable: true, value: 100 },
|
||||
scrollHeight: { configurable: true, value: 120 },
|
||||
});
|
||||
vi.spyOn(document.documentElement, 'scrollHeight', 'get').mockReturnValue(
|
||||
120,
|
||||
);
|
||||
vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(100);
|
||||
vi.spyOn(window, 'getComputedStyle').mockReturnValue({
|
||||
overflowY: 'auto',
|
||||
} as CSSStyleDeclaration);
|
||||
|
||||
const fixedNode = document.createElement('div');
|
||||
fixedNode.className = SCROLL_FIXED_CLASS;
|
||||
fixedNode.style.transition = 'opacity 200ms';
|
||||
document.body.append(fixedNode);
|
||||
const scrollLock = mountScrollLock();
|
||||
|
||||
await flushMountedLock();
|
||||
|
||||
expect(scrollLock.value).toBe(true);
|
||||
expect(document.body.style.overflow).toBe('hidden');
|
||||
expect(document.body.style.paddingRight).toBe('0px');
|
||||
expect(fixedNode.style.paddingRight).toBe('0px');
|
||||
|
||||
activeApp?.unmount();
|
||||
activeApp = undefined;
|
||||
|
||||
expect(document.body.style.overflow).toBe('auto');
|
||||
expect(document.body.style.paddingRight).toBe('');
|
||||
expect(fixedNode.style.paddingRight).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -1,48 +0,0 @@
|
||||
import type { SortableOptions } from 'sortablejs';
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { useSortable } from '../use-sortable';
|
||||
|
||||
describe('useSortable', () => {
|
||||
beforeEach(() => {
|
||||
vi.mock('sortablejs/modular/sortable.complete.esm.js', () => ({
|
||||
default: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
}));
|
||||
});
|
||||
it('should call Sortable.create with the correct options', async () => {
|
||||
// Create a mock element
|
||||
const mockElement = document.createElement('div') as HTMLDivElement;
|
||||
|
||||
// Define custom options
|
||||
const customOptions: SortableOptions = {
|
||||
group: 'test-group',
|
||||
sort: false,
|
||||
};
|
||||
|
||||
// Use the useSortable function
|
||||
const { initializeSortable } = useSortable(mockElement, customOptions);
|
||||
|
||||
// Initialize sortable
|
||||
await initializeSortable();
|
||||
|
||||
// Import sortablejs to access the mocked create function
|
||||
const Sortable =
|
||||
// @ts-expect-error - This is a dynamic import
|
||||
await import('sortablejs/modular/sortable.complete.esm.js');
|
||||
|
||||
// Verify that Sortable.create was called with the correct parameters
|
||||
expect(Sortable.default.create).toHaveBeenCalledTimes(1);
|
||||
expect(Sortable.default.create).toHaveBeenCalledWith(
|
||||
mockElement,
|
||||
expect.objectContaining({
|
||||
animation: 300,
|
||||
delay: 400,
|
||||
delayOnTouchOnly: true,
|
||||
...customOptions,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user