Skip to content
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

feat: develop a Split Pane component #509

Merged
merged 27 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c90b2f2
feat: develop a Split Pane component
mortalYoung Nov 15, 2021
be95b65
feat: improve Split
mortalYoung Nov 16, 2021
0e2c512
feat: support to set resize strategy for panes
mortalYoung Nov 17, 2021
0ff7b0c
test: add unit tests for SplitPane
mortalYoung Nov 18, 2021
989e195
feat: add exports in components/index
mortalYoung Nov 18, 2021
2d5fdd6
test: update tests
mortalYoung Nov 18, 2021
ce8ad43
refactor: use built-in splitPane component in collapse and editor (#510)
mortalYoung Nov 19, 2021
a5b7d08
fix: editor cannot resize
mortalYoung Nov 22, 2021
91a3a6f
test: update snapshots
mortalYoung Nov 22, 2021
374a576
feat: support to set minSize and maxSize
mortalYoung Nov 24, 2021
06cc807
feat: support maxSize and minSize in Pane
mortalYoung Nov 30, 2021
7148b30
feat: improve Split
mortalYoung Nov 30, 2021
09f4ae8
test: fix circular dependency
mortalYoung Dec 1, 2021
29b3782
test: update tests
mortalYoung Dec 1, 2021
4888356
test: update snapshots
mortalYoung Dec 2, 2021
4948f3c
docs: add Split component stories
mortalYoung Dec 3, 2021
118a89a
Merge remote-tracking branch 'origin/main' into feat/split
mortalYoung Feb 21, 2022
7607618
fix: improve the mouse move function in SplitPane
mortalYoung Feb 21, 2022
f161cbb
test: update tests
mortalYoung Feb 21, 2022
532a8a0
fix: replace react-split-pane in editor group
mortalYoung Feb 23, 2022
e5b1a1b
test: update tests
mortalYoung Feb 23, 2022
74ebf30
fix: fix the first editor in group can resize
mortalYoung Feb 23, 2022
e6a8672
fix: fix unrelated size changed will trigger resize
mortalYoung Feb 23, 2022
1f62f3f
test: update tests
mortalYoung Feb 23, 2022
09a80ab
fix: improve resize stratygy on editor groups
mortalYoung Feb 23, 2022
d90d872
fix: remove useless style
mortalYoung Feb 24, 2022
01a9de4
fix: use focusBorder as the backup of sash.hoverBorder
mortalYoung Feb 24, 2022
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
Prev Previous commit
Next Next commit
test: update tests
  • Loading branch information
mortalYoung committed Feb 23, 2022
commit e5b1a1b4355212a2b5a83aac278c7a092e2d61c8
18 changes: 14 additions & 4 deletions src/components/collapse/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ describe('Test The Collapse Component', () => {
});

let original;
const observerFnCollection: any[] = [];
beforeEach(() => {
original = HTMLElement.prototype.getBoundingClientRect;
// @ts-ignore
HTMLElement.prototype.getBoundingClientRect = () => ({
height: 500,
});

global.ResizeObserver = jest.fn().mockImplementation((fn) => {
observerFnCollection.push(fn);
return {
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});
});

afterEach(() => {
HTMLElement.prototype.getBoundingClientRect = original;
observerFnCollection.length = 0;
});

test('Match Snapshot', () => {
Expand Down Expand Up @@ -213,8 +224,7 @@ describe('Test The Collapse Component', () => {
HTMLElement.prototype.getBoundingClientRect = () => ({
height: 1000,
});
// Trigger the window resize event.
fireEvent(window, new Event('resize'));
observerFnCollection.forEach((f) => f());
await sleep(150);
});

Expand Down Expand Up @@ -277,7 +287,7 @@ describe('Test The Collapse Component', () => {
/>
);

let mock1 = container.querySelector('div[data-collapse-id="mock1"]');
const mock1 = container.querySelector('div[data-collapse-id="mock1"]');
expect(mock1?.parentElement?.style.height).toBe('26px');

await act(async () => {
Expand Down Expand Up @@ -321,7 +331,7 @@ describe('Test The Collapse Component', () => {
]}
/>
);
let mock1 = container.querySelector('div[data-collapse-id="mock1"]');
const mock1 = container.querySelector('div[data-collapse-id="mock1"]');
expect(mock1?.parentElement?.style.height).toBe('26px');

await act(async () => {
Expand Down
63 changes: 29 additions & 34 deletions src/components/split/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ afterEach(cleanup);

describe('Test The SplitPane Component', () => {
let original;
const observerFnCollection: any[] = [];
beforeEach(() => {
original = HTMLElement.prototype.getBoundingClientRect;
// @ts-ignore
HTMLElement.prototype.getBoundingClientRect = () => ({
width: 500,
height: 0,
});

global.ResizeObserver = jest.fn().mockImplementation((fn) => {
observerFnCollection.push(fn);
return {
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});
});

afterEach(() => {
HTMLElement.prototype.getBoundingClientRect = original;
observerFnCollection.length = 0;
});

test('Match Snapshot', () => {
Expand Down Expand Up @@ -166,17 +177,18 @@ describe('Test The SplitPane Component', () => {
});

test('Should support to resize', async () => {
const mockFn = jest.fn();
mockFn
const mockFn = jest
.fn()
.mockImplementationOnce(() => ['keep', 'keep', 'pave'])
.mockImplementationOnce(() => 'keep')
.mockImplementationOnce(() => undefined);
const { container } = render(
const mockChange = jest.fn();
render(
<SplitPane
role="split"
sizes={['10%', '10px']}
style={{ width: 500 }}
onChange={jest.fn()}
onChange={mockChange}
onResizeStrategy={mockFn}
>
<div>1</div>
Expand All @@ -191,62 +203,45 @@ describe('Test The SplitPane Component', () => {
width: 1000,
height: 0,
});

// Trigger the window resize event.
fireEvent(window, new Event('resize'));
observerFnCollection.forEach((f) => f());
await sleep(150);
});

let panes = container.querySelectorAll<HTMLDivElement>(
`.${paneItemClassName}`
);
expect(mockFn).toBeCalled();
expect(mockFn).toBeCalledTimes(1);
// normal strategy, to specify one pane to be pave
expect(panes[0].style.width).toBe('50px');
expect(panes[1].style.width).toBe('10px');
expect(panes[2].style.width).toBe('940px');
expect(mockChange.mock.calls[0][0]).toEqual([50, 10, 940]);

await act(async () => {
// @ts-ignore
HTMLElement.prototype.getBoundingClientRect = () => ({
width: 1500,
height: 0,
});

// Trigger the window resize event.
fireEvent(window, new Event('resize'));
observerFnCollection.forEach((f) => f());
await sleep(150);
});

panes = container.querySelectorAll<HTMLDivElement>(
`.${paneItemClassName}`
);
expect(mockFn).toBeCalled();
expect(mockFn).toBeCalledTimes(2);
// to specify global strategy to be keep
expect(panes[0].style.width).toBe('50px');
expect(panes[1].style.width).toBe('10px');
expect(panes[2].style.width).toBe('940px');
expect(mockChange.mock.calls[1][0]).toEqual([50, 10, 440]);

await act(async () => {
// @ts-ignore
HTMLElement.prototype.getBoundingClientRect = () => ({
width: 2000,
height: 0,
});

// Trigger the window resize event.
fireEvent(window, new Event('resize'));
observerFnCollection.forEach((f) => f());
await sleep(150);
});

panes = container.querySelectorAll<HTMLDivElement>(
`.${paneItemClassName}`
);
expect(mockFn).toBeCalled();
expect(mockFn).toBeCalledTimes(3);
// abnormal strategy, same as returns 'pave', we don't recommend to set it
expect(panes[0].style.width).toBe(`${2000 / 3}px`);
expect(panes[1].style.width).toBe(`${2000 / 3}px`);
expect(panes[2].style.width).toBe(`${2000 / 3}px`);
expect(mockChange.mock.calls[2][0]).toEqual([
2000 / 3,
2000 / 3,
2000 / 3,
]);
});

test('Should have limited sizes', () => {
Expand Down
54 changes: 28 additions & 26 deletions src/workbench/__tests__/workbench.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,28 @@ function expectBasicPartsInTheDocument() {

describe('Test Workbench Component', () => {
let original;
const observerFnCollection: any[] = [];
beforeEach(() => {
original = HTMLElement.prototype.getBoundingClientRect;
// @ts-ignore
HTMLElement.prototype.getBoundingClientRect = () => ({
width: 500,
height: 500,
});

global.ResizeObserver = jest.fn().mockImplementation((fn) => {
observerFnCollection.push(fn);
return {
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});
});

afterEach(() => {
HTMLElement.prototype.getBoundingClientRect = original;
observerFnCollection.length = 0;
});

function workbenchModel(): IWorkbench & ILayout {
Expand Down Expand Up @@ -113,6 +124,7 @@ describe('Test Workbench Component', () => {
sidebar: sidebarState,
splitPanePos: layout.splitPanePos,
horizontalSplitPanePos: layout.horizontalSplitPanePos,
groupSplitPos: layout.groupSplitPos,
};
}

Expand Down Expand Up @@ -236,24 +248,17 @@ describe('Test Workbench Component', () => {
expect(select('.mo-menuBar--horizontal')).toBeInTheDocument();
});

test('Should resize panes when called on window.resize', async () => {
test('Should resize panes when called on ResizeObserver', async () => {
const workbench = workbenchModel();
render(<WorkbenchView {...workbench} />);

const sidebarDOM = select<HTMLDivElement>('.mo-sidebar');
const editorDOM = select<HTMLDivElement>('.mo-editor');
const panelDOM = select<HTMLDivElement>('.mo-panel');

const sideBarPane = sidebarDOM?.parentElement;
const editorPane = editorDOM?.parentElement;
const panelPane = panelDOM?.parentElement;

expect(sideBarPane?.style.width).toBe('300px');
expect(
(sideBarPane?.nextElementSibling as HTMLDivElement).style.width
).toBe('200px');
expect(editorPane?.style.height).toBe('350px');
expect(panelPane?.style.height).toBe('150px');
const horizontalMockFn = jest.fn();
const paneChangeMockFn = jest.fn();
render(
<WorkbenchView
{...workbench}
onHorizontalPaneSizeChange={horizontalMockFn}
onPaneSizeChange={paneChangeMockFn}
/>
);

await act(async () => {
// mock resize
Expand All @@ -262,17 +267,14 @@ describe('Test Workbench Component', () => {
width: 1000,
height: 1000,
});
fireEvent(window, new Event('resize'));
observerFnCollection.forEach((f) => f());
await sleep(150);
});

// expect the sidebar's size won't changed
expect(sideBarPane?.style.width).toBe('300px');
expect(
(sideBarPane?.nextElementSibling as HTMLDivElement).style.width
).toBe('700px');
expect(editorPane?.style.height).toBe('850px');
// expect the panel's size won't changed
expect(panelPane?.style.height).toBe('150px');
expect(horizontalMockFn).toBeCalled();
expect(horizontalMockFn.mock.calls[0][0]).toEqual([850, 150]);

expect(paneChangeMockFn).toBeCalled();
expect(paneChangeMockFn.mock.calls[0][0]).toEqual([300, 700]);
});
});
Loading