Skip to content

feat(overlay): add test file #22

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 1 commit into from
Dec 17, 2021
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
198 changes: 198 additions & 0 deletions packages/devui-vue/devui/overlay/__tests__/overlay.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import { mount } from '@vue/test-utils';
import { FixedOverlay } from '../src/fixed-overlay';
import { nextTick } from 'vue';
import { FlexibleOverlay } from '../src/flexible-overlay';
import { OriginOrDomRef, Rect, ConnectionPosition } from '../src/overlay-types';

let overlayContianerElement: HTMLElement;
let origin: OriginOrDomRef;

describe('overlay', () => {
beforeEach(() => {
const div = document.createElement('div');
div.id = 'd-overlay-anchor';
document.body.append(div);
overlayContianerElement = document.querySelector('#d-overlay-anchor');
})

describe('fixed overlay', () => {

it('should be create', async() => {
const wrapper = mount(FixedOverlay, {
props: {
visible: true
}
})
await nextTick();
let bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.getAttribute('style')).toBeNull();
await wrapper.setProps({visible: false});
await nextTick();
bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.style.display).toBe('none');
wrapper.unmount()
})
it('test backgroundClass, backgroundStyle, overlayStyle', async() => {
const wrapper = mount(FixedOverlay, {
props: {
visible: true,
backgroundClass: 'bgColor',
backgroundStyle: 'width: 100px',
overlayStyle: 'width: 100%'
}
})
await nextTick();
const bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.classList).toContain('bgColor');
expect(bgElement.style.width).toBe('100px');
expect((overlayContianerElement.querySelector('.devui-overlay') as HTMLElement).style.width).toBe('100%');
wrapper.unmount();
})
it('test hasBackdrop', async() => {
const wrapper = mount(FixedOverlay, {
props: {
visible: true,
hasBackdrop: false
}
})
await nextTick();
let bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.classList).toContain('devui-overlay-background__disabled');
await wrapper.setProps({hasBackdrop: true});
await nextTick();
bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.classList).toContain('devui-overlay-background__color');
wrapper.unmount();
})
it('test emit update:visible and onBackdropClick', async() => {
const wrapper = mount(FixedOverlay, {
props: {
visible: true
}
})
await nextTick();
const bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
const fn = jest.fn();
await wrapper.setProps({onBackdropClick: fn})
bgElement.click();
expect(wrapper.emitted('update:visible').length).toBe(1);
expect(fn).toHaveBeenCalled();
wrapper.unmount();
})
})

describe('flexible overlay', () => {
beforeEach(() => {
origin = {x:100, y: 100, width: 100, height: 100} as Rect;
// 解决 ResizeObserver is not defined
global.ResizeObserver = window.ResizeObserver || jest.fn().mockImplementation(() => ({
disconnect: jest.fn(),
observe: jest.fn(),
unobserve: jest.fn(),
}));
})

it('should be creat', async() => {
const wrapper = mount(FlexibleOverlay, {
props: {
origin: origin,
visible: true
}
})
await nextTick();
let bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.getAttribute('style')).toBeNull();
await wrapper.setProps({ visible: false });
await nextTick();
bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.style.display).toBe('none');
wrapper.unmount();
})
it('test backgroundClass, backgroundStyle', async() => {
const wrapper = mount(FlexibleOverlay, {
props: {
origin: origin,
visible: true,
backgroundClass: 'bgColor',
backgroundStyle: 'width: 100px'
}
})
await nextTick();
const bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.classList).toContain('bgColor');
expect(bgElement.style.width).toBe('100px');
wrapper.unmount();
})
it('test hasBackdrop', async() => {
const wrapper = mount(FlexibleOverlay, {
props: {
origin: origin,
visible: true,
hasBackdrop: false
}
})
await nextTick();
let bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.classList).toContain('devui-overlay-background__disabled');
await wrapper.setProps({hasBackdrop: true});
await nextTick();
bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
expect(bgElement.classList).toContain('devui-overlay-background__color');
wrapper.unmount();
})
it('test emit update:visible and onBackdropClick', async() => {
const wrapper = mount(FlexibleOverlay, {
props: {
origin: origin,
visible: true
}
})
await nextTick();
const bgElement = overlayContianerElement.querySelector('.devui-overlay-background') as HTMLElement;
const fn = jest.fn();
await wrapper.setProps({onBackdropClick: fn})
bgElement.click();
expect(wrapper.emitted('update:visible').length).toBe(1);
expect(fn).toHaveBeenCalled();
wrapper.unmount();
})
it('test position', async() => {
const position = {
originX: 'left',
originY: 'top',
overlayX: 'left',
overlayY: 'top'
} as ConnectionPosition
const wrapper = mount(FlexibleOverlay, {
props: {
origin: origin,
visible: true,
},
slots: {
default: '<div style="width:100px; height: 100px;"></div>'
}
})
await wrapper.setProps({position: position})
await nextTick();
let overlayElement = overlayContianerElement.querySelector('.devui-overlay') as HTMLElement;
expect(overlayElement.style.left).toBe('100px');
expect(overlayElement.style.top).toBe('100px');
position.originX = 'center';
position.originY = 'center';
await wrapper.setProps({position: Object.assign({}, position)})
await nextTick();
overlayElement = overlayContianerElement.querySelector('.devui-overlay') as HTMLElement;
expect(overlayElement.style.left).toBe('150px');
expect(overlayElement.style.top).toBe('150px');
position.originX = 'right';
position.originY = 'bottom'
await wrapper.setProps({position: Object.assign({}, position)})
await nextTick();
overlayElement = overlayContianerElement.querySelector('.devui-overlay') as HTMLElement;
expect(overlayElement.style.left).toBe('200px');
expect(overlayElement.style.top).toBe('200px');
wrapper.unmount();
})
})
})