Files
webawesome/src/components/split-panel/split-panel.test.ts

294 lines
9.9 KiB
TypeScript
Raw Normal View History

2023-06-22 10:56:24 -04:00
import { dragElement } from '../../internal/test.js';
import { expect, fixture, html, oneEvent } from '@open-wc/testing';
2023-06-22 10:56:24 -04:00
import { queryByTestId } from '../../internal/test/data-testid-helpers.js';
import { resetMouse } from '@web/test-runner-commands';
2023-09-08 13:45:49 -04:00
import type WaSplitPanel from './split-panel.js';
const DIVIDER_WIDTH_IN_PX = 4;
2023-09-08 13:45:49 -04:00
const getPanel = (splitPanel: WaSplitPanel, testid: string): HTMLElement => {
const startPanel = queryByTestId<HTMLElement>(splitPanel, testid);
expect(startPanel).not.to.be.null;
return startPanel!;
};
2023-09-08 13:45:49 -04:00
const getPanelWidth = (splitPanel: WaSplitPanel, testid: string) => {
const panel = getPanel(splitPanel, testid);
const { width } = panel.getBoundingClientRect();
return width;
};
2023-09-08 13:45:49 -04:00
const getPanelHeight = (splitPanel: WaSplitPanel, testid: string) => {
const panel = getPanel(splitPanel, testid);
const { height } = panel.getBoundingClientRect();
return height;
};
2023-09-08 13:45:49 -04:00
const getDivider = (splitPanel: WaSplitPanel): Element => {
const divider = splitPanel.shadowRoot?.querySelector('[part="divider"]');
expect(divider).not.to.be.null;
return divider!;
};
2021-12-22 18:32:27 -05:00
2023-09-08 13:45:49 -04:00
describe('<wa-split-panel>', () => {
afterEach(async () => {
// eslint-disable-next-line
await resetMouse().catch(() => {});
});
2021-12-22 18:32:27 -05:00
it('should render a component', async () => {
2023-09-08 13:45:49 -04:00
const splitPanel = await fixture(html` <wa-split-panel></wa-split-panel> `);
2021-12-22 18:32:27 -05:00
expect(splitPanel).to.exist;
});
it('should be accessible', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture(
html`<wa-split-panel>
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
await expect(splitPanel).to.be.accessible();
2021-12-22 18:32:27 -05:00
});
it('should show both panels', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture(
html`<wa-split-panel>
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
expect(splitPanel).to.contain.text('Start');
expect(splitPanel).to.contain.text('End');
});
describe('panel sizing horizontal', () => {
it('has two evenly sized panels by default', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel>
<div slot="start" data-testid="start-panel">Start</div>
<div slot="end" data-testid="end-panel">End</div>
</wa-split-panel>`
);
const startPanelWidth = getPanelWidth(splitPanel, 'start-panel');
const endPanelWidth = getPanelWidth(splitPanel, 'end-panel');
expect(startPanelWidth).to.be.equal(endPanelWidth);
});
it('changes the sizing of the panels based on the position attribute', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel position="25">
<div slot="start" data-testid="start-panel">Start</div>
<div slot="end" data-testid="end-panel">End</div>
</wa-split-panel>`
);
const startPanelWidth = getPanelWidth(splitPanel, 'start-panel');
const endPanelWidth = getPanelWidth(splitPanel, 'end-panel');
expect(startPanelWidth * 3).to.be.equal(endPanelWidth - DIVIDER_WIDTH_IN_PX);
});
it('updates the position in pixels to the correct result', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel position="25">
<div slot="start" data-testid="start-panel">Start</div>
<div slot="end" data-testid="end-panel">End</div>
</wa-split-panel>`
);
splitPanel.position = 10;
const startPanelWidth = getPanelWidth(splitPanel, 'start-panel');
expect(startPanelWidth).to.be.equal(splitPanel.positionInPixels - DIVIDER_WIDTH_IN_PX / 2);
});
2023-09-08 13:45:49 -04:00
it('emits the wa-reposition event on position change', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel>
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
2023-09-08 13:45:49 -04:00
const repositionPromise = oneEvent(splitPanel, 'wa-reposition');
splitPanel.position = 10;
return repositionPromise;
});
it('can be resized using the mouse', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel>
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
2024-04-29 10:55:52 -04:00
const positionInPixels = Math.round(splitPanel.positionInPixels);
const divider = getDivider(splitPanel);
await dragElement(divider, -30);
2024-04-29 10:55:52 -04:00
const positionInPixelsAfterDrag = Math.round(splitPanel.positionInPixels);
expect(positionInPixelsAfterDrag).to.be.equal(positionInPixels - 30);
});
it('cannot be resized if disabled', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel disabled>
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
2024-04-29 10:55:52 -04:00
const positionInPixels = Math.round(splitPanel.positionInPixels);
const divider = getDivider(splitPanel);
await dragElement(divider, -30);
2024-04-29 10:55:52 -04:00
const positionInPixelsAfterDrag = Math.round(splitPanel.positionInPixels);
expect(positionInPixelsAfterDrag).to.be.equal(positionInPixels);
});
it('snaps to predefined positions', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel>
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
2024-04-29 10:55:52 -04:00
const positionInPixels = Math.round(splitPanel.positionInPixels);
splitPanel.snap = `${positionInPixels - 40}px`;
const divider = getDivider(splitPanel);
await dragElement(divider, -30);
2024-04-29 10:55:52 -04:00
const positionInPixelsAfterDrag = Math.round(splitPanel.positionInPixels);
expect(positionInPixelsAfterDrag).to.be.equal(positionInPixels - 40);
});
});
describe('panel sizing vertical', () => {
it('has two evenly sized panels by default', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel vertical style="height: 400px;">
<div slot="start" data-testid="start-panel">Start</div>
<div slot="end" data-testid="end-panel">End</div>
</wa-split-panel>`
);
const startPanelHeight = getPanelHeight(splitPanel, 'start-panel');
const endPanelHeight = getPanelHeight(splitPanel, 'end-panel');
expect(startPanelHeight).to.be.equal(endPanelHeight);
});
it('changes the sizing of the panels based on the position attribute', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel position="25" vertical style="height: 400px;">
<div slot="start" data-testid="start-panel">Start</div>
<div slot="end" data-testid="end-panel">End</div>
</wa-split-panel>`
);
const startPanelHeight = getPanelHeight(splitPanel, 'start-panel');
const endPanelHeight = getPanelHeight(splitPanel, 'end-panel');
expect(startPanelHeight * 3).to.be.equal(endPanelHeight - DIVIDER_WIDTH_IN_PX);
});
it('updates the position in pixels to the correct result', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel position="25" vertical style="height: 400px;">
<div slot="start" data-testid="start-panel">Start</div>
<div slot="end" data-testid="end-panel">End</div>
</wa-split-panel>`
);
splitPanel.position = 10;
const startPanelHeight = getPanelHeight(splitPanel, 'start-panel');
expect(startPanelHeight).to.be.equal(splitPanel.positionInPixels - DIVIDER_WIDTH_IN_PX / 2);
});
2023-09-08 13:45:49 -04:00
it('emits the wa-reposition event on position change ', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel vertical style="height: 400px;">
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
2023-09-08 13:45:49 -04:00
const repositionPromise = oneEvent(splitPanel, 'wa-reposition');
splitPanel.position = 10;
return repositionPromise;
});
it('can be resized using the mouse ', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel vertical style="height: 400px;">
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
2024-04-29 10:55:52 -04:00
const positionInPixels = Math.round(splitPanel.positionInPixels);
const divider = getDivider(splitPanel);
await dragElement(divider, 0, -30);
2024-04-29 10:55:52 -04:00
const positionInPixelsAfterDrag = Math.round(splitPanel.positionInPixels);
expect(positionInPixelsAfterDrag).to.be.equal(positionInPixels - 30);
});
it('cannot be resized if disabled', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel disabled vertical style="height: 400px;">
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
2024-04-29 10:55:52 -04:00
const positionInPixels = Math.round(splitPanel.positionInPixels);
const divider = getDivider(splitPanel);
await dragElement(divider, 0, -30);
2024-04-29 10:55:52 -04:00
const positionInPixelsAfterDrag = Math.round(splitPanel.positionInPixels);
expect(positionInPixelsAfterDrag).to.be.equal(positionInPixels);
});
it('snaps to predefined positions', async () => {
2023-10-12 15:03:38 -04:00
const splitPanel = await fixture<WaSplitPanel>(
html`<wa-split-panel vertical style="height: 400px;">
<div slot="start">Start</div>
<div slot="end">End</div>
</wa-split-panel>`
);
2024-04-29 10:55:52 -04:00
const positionInPixels = Math.round(splitPanel.positionInPixels);
splitPanel.snap = `${positionInPixels - 40}px`;
const divider = getDivider(splitPanel);
await dragElement(divider, 0, -30);
2024-04-29 10:55:52 -04:00
const positionInPixelsAfterDrag = Math.round(splitPanel.positionInPixels);
expect(positionInPixelsAfterDrag).to.be.equal(positionInPixels - 40);
});
});
2021-12-22 18:32:27 -05:00
});