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

优化一些组件bug和一些组件样式问题,单元测试报告在终端输出 #56

Merged
merged 15 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module.exports = {
},
],
},
collectCoverage: true,
coveragePathIgnorePatterns: ['<rootDir>/scripts/', 'tests/'],
coverageDirectory: './.ci/',
coverageReporters: ['json', 'html'],
moduleNameMapper: {
'^@bifrostui/(styles.*)$': '<rootDir>/tests/mocks/styleMock.js',
'^@bifrostui/react$': '<rootDir>/packages/bui-core/src',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { snapshotTest } from 'testing';

describe('Collapse snapshot', () => {
snapshotTest('Collapse');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Collapse snapshot Collapse demo snapshot 0 1`] = `
<div
className="bui-stack"
style={
Object {
"--align-items": "center",
"--flex-direction": "column",
"--flex-wrap": "wrap",
"--justify-content": "center",
"--spacing": "8px",
}
}
>
<div
className="bui-switch bui-switch-medium bui-switch-primary"
>
<input
checked={false}
className="bui-switch-input"
disabled={false}
onChange={[Function]}
type="checkbox"
/>
</div>
<div
className="bui-collapse undefined"
style={
Object {
"WebKitWidth": "0px",
"WebkitTransition": "width 1500ms cubic-bezier(0.4, 0, 0.6, 1) 0ms",
"transition": "width 1500ms cubic-bezier(0.4, 0, 0.6, 1) 0ms",
"width": "0px",
}
}
>
<div
className="bui-card"
style={
Object {
"background": "#CCCCCC",
"height": "100px",
"width": "100px",
}
}
/>
</div>
</div>
`;

exports[`Collapse snapshot Collapse demo snapshot 0 2`] = `
<div
className="bui-stack"
style={
Object {
"--align-items": "center",
"--flex-direction": "column",
"--flex-wrap": "wrap",
"--justify-content": "center",
"--spacing": "8px",
}
}
>
<div
className="bui-switch bui-switch-medium bui-switch-primary bui-switch-checked"
>
<input
checked={true}
className="bui-switch-input"
disabled={false}
onChange={[Function]}
type="checkbox"
/>
</div>
<div
className="bui-collapse undefined"
style={
Object {
"WebKitHeight": "fit-content",
"WebkitTransition": "height 1500ms cubic-bezier(0.0, 0, 0.2, 1) 0ms",
"height": "fit-content",
"transition": "height 1500ms cubic-bezier(0.0, 0, 0.2, 1) 0ms",
}
}
>
<div
className="bui-card"
style={
Object {
"background": "#CCCCCC",
"height": "100px",
"width": "100px",
}
}
/>
</div>
</div>
`;

exports[`Collapse snapshot Collapse demo snapshot 0 3`] = `
<div
className="bui-stack"
style={
Object {
"--align-items": "center",
"--flex-direction": "column",
"--flex-wrap": "wrap",
"--justify-content": "center",
"--spacing": "8px",
}
}
>
<div
className="bui-switch bui-switch-medium bui-switch-primary bui-switch-checked"
>
<input
checked={true}
className="bui-switch-input"
disabled={false}
onChange={[Function]}
type="checkbox"
/>
</div>
<div
className="bui-collapse undefined"
style={
Object {
"WebKitHeight": "fit-content",
"WebkitTransition": "height 1500ms cubic-bezier(0.0, 0, 0.2, 1) 0ms",
"height": "fit-content",
"transition": "height 1500ms cubic-bezier(0.0, 0, 0.2, 1) 0ms",
}
}
>
<div
className="bui-card"
style={
Object {
"background": "#CCCCCC",
"height": "100px",
"width": "100px",
}
}
/>
</div>
</div>
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactElement, useState } from 'react';
import { render, isConformant, userEvent, screen } from 'testing';
import { render, isConformant, userEvent, screen, waitFor } from 'testing';
import CollapsePanel, { CollapsePanelItem, CollapsePanelProps } from '../index';

const setup = (props: CollapsePanelProps, children?: ReactElement) => {
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('CollapsePanel', () => {
expect(firstPanel).toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).not.toHaveClass('bui-collapse-panel-item-active');
});
it('should only open one panel when use accordion', () => {
it('should only open one panel when use accordion', async () => {
const { container } = setup({
defaultActiveKeys: ['1'],
accordion: true,
Expand All @@ -100,8 +100,10 @@ describe('CollapsePanel', () => {
)[1];

userEvent.click(screen.getByText('这是面板标题2'));
expect(firstPanel).not.toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).toHaveClass('bui-collapse-panel-item-active');
await waitFor(() => {
expect(firstPanel).not.toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).toHaveClass('bui-collapse-panel-item-active');
});
});
it('should use custom icon', () => {
const customIcon = <div>custom icon</div>;
Expand All @@ -119,7 +121,7 @@ describe('CollapsePanel', () => {
const icon = screen.getByText('custom icon');
expect(icon).toBeVisible();
});
it('should use custom icon by function', () => {
it('should use custom icon by function', async () => {
setup({
arrowIcon: (active) => {
if (active) {
Expand All @@ -138,10 +140,12 @@ describe('CollapsePanel', () => {
const unactivedIcon = screen.getByText('unactived icon');
expect(unactivedIcon).toBeVisible();
userEvent.click(screen.getByText('这是面板标题1'));
const activeIcon = screen.getByText('active icon');
expect(activeIcon).toBeVisible();
await waitFor(() => {
const activeIcon = screen.getByText('active icon');
expect(activeIcon).toBeVisible();
});
});
it('should call onChange', () => {
it('should call onChange', async () => {
const fakeChange = jest.fn();
const Component = () => {
const [activeKeys, setActiveKeys] = useState(['1']);
Expand All @@ -167,10 +171,12 @@ describe('CollapsePanel', () => {
)[1];

userEvent.click(screen.getByText('这是面板标题2'));
expect(fakeChange).toBeCalled();
expect(fakeChange).toBeCalledWith(['2', '1']);
expect(firstPanel).toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).toHaveClass('bui-collapse-panel-item-active');
await waitFor(() => {
expect(fakeChange).toBeCalled();
expect(fakeChange).toBeCalledWith(['2', '1']);
expect(firstPanel).toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).toHaveClass('bui-collapse-panel-item-active');
});
});
it('should return null', () => {
const { container } = setup({});
Expand Down Expand Up @@ -206,7 +212,7 @@ describe('CollapsePanel', () => {
expect(firstPanel).toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).not.toHaveClass('bui-collapse-panel-item-active');
});
it('should only open one panel when use accordion', () => {
it('should only open one panel when use accordion', async () => {
const { container } = setupByElement({
defaultActiveKeys: ['1'],
accordion: true,
Expand All @@ -219,8 +225,10 @@ describe('CollapsePanel', () => {
)[1];

userEvent.click(screen.getByText('这是面板标题2'));
expect(firstPanel).not.toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).toHaveClass('bui-collapse-panel-item-active');
await waitFor(() => {
expect(firstPanel).not.toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).toHaveClass('bui-collapse-panel-item-active');
});
});
it('should use custom icon', () => {
const customIcon = <div>custom icon</div>;
Expand All @@ -236,7 +244,7 @@ describe('CollapsePanel', () => {
const icon = screen.getByText('custom icon');
expect(icon).toBeVisible();
});
it('should use custom icon by function', () => {
it('should use custom icon by function', async () => {
setupByElement(
{
arrowIcon: (active) => {
Expand All @@ -251,10 +259,12 @@ describe('CollapsePanel', () => {
const unactivedIcon = screen.getByText('unactived icon');
expect(unactivedIcon).toBeVisible();
userEvent.click(screen.getByText('这是面板标题1'));
const activeIcon = screen.getByText('active icon');
expect(activeIcon).toBeVisible();
await waitFor(() => {
const activeIcon = screen.getByText('active icon');
expect(activeIcon).toBeVisible();
});
});
it('should call onChange', () => {
it('should call onChange', async () => {
const fakeChange = jest.fn();
const Component = () => {
const [activeKeys, setActiveKeys] = useState(['1']);
Expand Down Expand Up @@ -283,10 +293,12 @@ describe('CollapsePanel', () => {
)[1];

userEvent.click(screen.getByText('这是面板标题2'));
expect(fakeChange).toBeCalled();
expect(fakeChange).toBeCalledWith(['2', '1']);
expect(firstPanel).toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).toHaveClass('bui-collapse-panel-item-active');
await waitFor(() => {
expect(fakeChange).toBeCalled();
expect(fakeChange).toBeCalledWith(['2', '1']);
expect(firstPanel).toHaveClass('bui-collapse-panel-item-active');
expect(secondPanel).toHaveClass('bui-collapse-panel-item-active');
});
});
it('should return null', () => {
const { container } = setupByElement({}, 0);
Expand Down
6 changes: 3 additions & 3 deletions packages/bui-core/src/Dialog/FunctionalDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const useDialog = () => {
const wrapAPI: DialogFunction = (
props: DialogOptions | string,
): DialogPromise => {
const options = { theme: holderRef.current.theme, ...formatProps(props) };
const options = { theme: holderRef.current?.theme, ...formatProps(props) };
const { onConfirm, onCancel, ...rest } = options;
return new Promise((resolve) => {
DialogGenerator({
Expand All @@ -141,13 +141,13 @@ const useDialog = () => {
Dialog({
type: 'confirm',
...formatProps(options),
theme: holderRef.current.theme,
theme: holderRef.current?.theme,
});
wrapAPI.prompt = (options: PromptOptions) =>
Dialog({
type: 'prompt',
...formatProps(options),
theme: holderRef.current.theme,
theme: holderRef.current?.theme,
});
return [wrapAPI, <Popup key="dialog-holder" ref={holderRef} />] as [
DialogFunction,
Expand Down
25 changes: 24 additions & 1 deletion packages/bui-core/src/Dialog/__tests__/Dialog.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import React from 'react';
import { fireEvent, waitFor } from '@testing-library/react';
import { fireEvent, renderHook, waitFor } from '@testing-library/react';
import { Button } from '@bifrostui/react';
import { render, screen, act } from 'testing';
import Dialog from '../FunctionalDialog';
import '@testing-library/jest-dom/extend-expect';

describe('Dialog Functional Calls', () => {
const rootClass = 'bui-dialog';
let dialogHook;

beforeEach(() => {
document.body.innerHTML = '';
jest.useFakeTimers();
renderHook(() => {
dialogHook = Dialog.useDialog();
});
});

afterEach(() => {
Expand Down Expand Up @@ -142,4 +147,22 @@ describe('Dialog Functional Calls', () => {
fireEvent.click(screen.getByText('Delete'));
await waitFor(() => expect(promptPromise).resolves.toBe(false));
});

it.each(['confirm', 'prompt'])(
'should support basic api with useDialog',
async (type) => {
const dialog = dialogHook?.[0];
render(
<Button
onClick={() => {
dialog[type](`${type} message`);
}}
>
dialog button
</Button>,
);
fireEvent.click(screen.getByText('dialog button'));
expect(screen.getByText(`${type} message`)).toBeInTheDocument();
},
);
});
1 change: 1 addition & 0 deletions packages/bui-core/src/Input/Input.less
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
outline: none;
background-color: var(--background-color);
font-size: var(--bui-text-size-2);
.ellipsis();

&::placeholder {
color: var(--bui-color-fg-subtle);
Expand Down
Loading
Loading