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

fix: activeKey 为数字0时无效修复 #204

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { CollapseProps, CollapsibleType } from './interface';
function getActiveKeysArray(activeKey: React.Key | React.Key[]) {
let currentActiveKey = activeKey;
if (!Array.isArray(currentActiveKey)) {
currentActiveKey = currentActiveKey ? [currentActiveKey] : [];
const activeKeyType = typeof currentActiveKey;
currentActiveKey =
activeKeyType === 'number' || activeKeyType === 'string' ? [currentActiveKey] : [];
}
return currentActiveKey.map((key) => String(key));
}
Expand Down
107 changes: 51 additions & 56 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Fragment } from 'react';
import { mount, ReactWrapper } from 'enzyme';
import type { ReactWrapper } from 'enzyme';
import { mount } from 'enzyme';

import KeyCode from 'rc-util/lib/KeyCode';

Expand All @@ -19,6 +20,7 @@ describe('collapse', () => {

function onChange(...args: any[]) {
if (changeHook) {
// eslint-disable-next-line @typescript-eslint/no-invalid-this
changeHook.apply(this, args);
}
}
Expand All @@ -36,12 +38,9 @@ describe('collapse', () => {

it('add className', () => {
const expectedClassName = 'rc-collapse-item important';
expect(
collapse
.find('.rc-collapse-item')
.at(2)
.getDOMNode().className,
).toBe(expectedClassName);
expect(collapse.find('.rc-collapse-item').at(2).getDOMNode().className).toBe(
expectedClassName,
);
});

it('create works', () => {
Expand All @@ -58,12 +57,9 @@ describe('collapse', () => {
});

it('should render custom arrow icon corrctly', () => {
expect(
collapse
.find('.rc-collapse-header')
.at(0)
.getDOMNode().textContent,
).toContain('test>');
expect(collapse.find('.rc-collapse-header').at(0).getDOMNode().textContent).toContain(
'test>',
);
});

it('default active works', () => {
Expand All @@ -78,10 +74,7 @@ describe('collapse', () => {

it('onChange works', () => {
changeHook = jest.fn();
collapse
.find('.rc-collapse-header')
.at(1)
.simulate('click');
collapse.find('.rc-collapse-header').at(1).simulate('click');
expect(changeHook.mock.calls[0][0]).toEqual(['2']);
});

Expand All @@ -94,12 +87,9 @@ describe('collapse', () => {
header.simulate('click');
jest.runAllTimers();
collapse.update();
expect(
collapse
.find('.rc-collapse-content-inactive')
.at(0)
.getDOMNode().innerHTML,
).toBe('<div class="rc-collapse-content-box">second</div>');
expect(collapse.find('.rc-collapse-content-inactive').at(0).getDOMNode().innerHTML).toBe(
'<div class="rc-collapse-content-box">second</div>',
);
expect(collapse.find('.rc-collapse-content-active').length).toBe(0);
});

Expand Down Expand Up @@ -211,6 +201,25 @@ describe('collapse', () => {

runNormalTest(element);
});
describe('it should support activeKey number 0', () => {
const collapse = mount(
<Collapse onChange={onChange} activeKey={0}>
<Panel header="collapse 0" key={0}>
zero
</Panel>
<Panel header="collapse 1" key={1}>
first
</Panel>
<Panel header="collapse 2" key={2}>
second
</Panel>
</Collapse>,
);

it('activeKey number 0, should open one item', () => {
expect(collapse.find('.rc-collapse-content-active').length).toBe(1);
});
});

describe('destroyInactivePanel', () => {
const collapse = mount(
Expand Down Expand Up @@ -356,9 +365,15 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);

jest.runAllTimers();
collapse.update();

expect(collapse.find('.rc-collapse-content').length).toBe(1);
expect(collapse.find('.rc-collapse-content-active').length).toBe(0);
expect(collapse.find('div.rc-collapse-content-hidden').length).toBe(1);
expect(collapse.find('div.rc-collapse-content-inactive').props().style).toEqual({
display: 'none',
});
});
});

Expand All @@ -383,44 +398,29 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
collapse
.find('.rc-collapse-header')
.at(2)
.simulate('keyPress', {
keyCode: KeyCode.ENTER,
});
collapse.find('.rc-collapse-header').at(2).simulate('keyPress', {
keyCode: KeyCode.ENTER,
});
jest.runAllTimers();
collapse.update();
expect(collapse.find('.rc-collapse-content-active').length).toBe(0);
collapse
.find('.rc-collapse-header')
.at(0)
.simulate('keyPress', {
keyCode: KeyCode.ENTER,
});
collapse.find('.rc-collapse-header').at(0).simulate('keyPress', {
keyCode: KeyCode.ENTER,
});
jest.runAllTimers();
collapse.update();
expect(collapse.find('.rc-collapse-content-active').length).toBe(1);
expect(
collapse
.find('.rc-collapse-content')
.at(0)
.hasClass('rc-collapse-content-active'),
collapse.find('.rc-collapse-content').at(0).hasClass('rc-collapse-content-active'),
).toBeTruthy();
collapse
.find('.rc-collapse-header')
.at(0)
.simulate('keyPress', {
keyCode: KeyCode.ENTER,
});
collapse.find('.rc-collapse-header').at(0).simulate('keyPress', {
keyCode: KeyCode.ENTER,
});
jest.runAllTimers();
collapse.update();
expect(collapse.find('.rc-collapse-content-active').length).toBe(0);
expect(
collapse
.find('.rc-collapse-content')
.at(0)
.hasClass('rc-collapse-content-active'),
collapse.find('.rc-collapse-content').at(0).hasClass('rc-collapse-content-active'),
).toBeFalsy();
});
});
Expand Down Expand Up @@ -456,12 +456,7 @@ describe('collapse', () => {
</Panel>
</Collapse>,
);
expect(
wrapper
.find('.rc-collapse-header')
.at(0)
.getDOMNode().childNodes.length,
).toBe(1);
expect(wrapper.find('.rc-collapse-header').at(0).getDOMNode().childNodes.length).toBe(1);
});

it('should support custom child', () => {
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "esnext",
"moduleResolution": "node",
"baseUrl": "./",
"jsx": "preserve",
"declaration": true,
"skipLibCheck": true,
"esModuleInterop": true,
"paths": {
"@/*": ["src/*"],
"@@/*": ["src/.umi/*"],
"rc-collapse": ["src/index.tsx"]
}
}
}