Skip to content

Commit

Permalink
fix(Switch): should unClickable while diabled, close #4478
Browse files Browse the repository at this point in the history
  • Loading branch information
YSMJ1994 authored and eternalsky committed Oct 30, 2023
1 parent 3e0250e commit 28266ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/switch/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class Switch extends React.Component {
} else {
attrs = {
disabled: true,
onClick: undefined,
};
}

Expand Down
45 changes: 17 additions & 28 deletions test/switch/index-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import ReactTestUtils from 'react-dom/test-utils';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import sinon from 'sinon';
Expand Down Expand Up @@ -26,6 +27,18 @@ describe('Switch', () => {
wrapper.find('.next-switch').simulate('click');
assert(wrapper.find('.next-switch-on').length === 1);
});
it('should unClickable while disabled', () => {
let clicked = false;
const wrapper = mount(<Switch disabled onClick={() => (clicked = true)} />);
const el = wrapper.getDOMNode();
assert(el);
ReactTestUtils.Simulate.click(el);
assert(!clicked);
wrapper.setProps({ disabled: false });
ReactTestUtils.Simulate.click(el);
assert(clicked);
wrapper.unmount();
});
it('children setting and onChange', () => {
class App extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -59,13 +72,7 @@ describe('Switch', () => {
assert(wrapper.find('.next-switch-children').text() === '开');
//非受控的onChange
const onChange = sinon.spy();
const wrapper1 = mount(
<Switch
checkedChildren="开"
unCheckedChildren="关"
onChange={onChange}
/>
);
const wrapper1 = mount(<Switch checkedChildren="开" unCheckedChildren="关" onChange={onChange} />);
assert(wrapper1.find('.next-switch-children').text() === '关');
wrapper1.find('.next-switch').simulate('click');
assert(wrapper1.find('.next-switch-children').text() === '开');
Expand Down Expand Up @@ -125,35 +132,17 @@ describe('Switch', () => {
assert(wrapper.find('.next-switch-off').length === 1);
});
it('should renderPreview', () => {
const wrapper = mount(
<Switch
id="render-preview"
isPreview
renderPreview={() => 'preview switch'}
/>
);
const wrapper = mount(<Switch id="render-preview" isPreview renderPreview={() => 'preview switch'} />);

assert(wrapper.getDOMNode().innerText === 'preview switch');
});
it('should use checkedChildren if exist when on & preview', () => {
const wrapper = mount(
<Switch
isPreview
checked
checkedChildren="✓"
/>
);
const wrapper = mount(<Switch isPreview checked checkedChildren="✓" />);

assert(wrapper.getDOMNode().innerText === '✓');
});
it('should use unCheckedChildren if exist when off & preview', () => {
const wrapper = mount(
<Switch
isPreview
checked={false}
unCheckedChildren="✕"
/>
);
const wrapper = mount(<Switch isPreview checked={false} unCheckedChildren="✕" />);

assert(wrapper.getDOMNode().innerText === '✕');
});
Expand Down

0 comments on commit 28266ad

Please sign in to comment.