Skip to content

Commit

Permalink
fix: no more using reactstrap defaultProps
Browse files Browse the repository at this point in the history
Seems like we were using implementation details of reactstrap that we probably should not have needed.
  • Loading branch information
davidacevedo committed Jun 16, 2023
1 parent cdf77ab commit d3c538e
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/components/Input/CreditCardNumber.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';
import { render, screen } from '@testing-library/react';
import { mount, shallow } from 'enzyme';
import React from 'react';
import sinon from 'sinon';
Expand Down Expand Up @@ -78,8 +79,7 @@ describe('<CreditCardNumber />', () => {
});

it('should remain type="text"', () => {
const component = mount(<CreditCardNumber type="wth" />);
const input = component.find('Input');
assert.equal(input.prop('type'), 'text');
render(<CreditCardNumber type="wth" />);
screen.getByRole('textbox', { type: 'text' });
});
});
6 changes: 0 additions & 6 deletions src/components/Input/CreditCardNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,12 @@ function typeToIconName(type = ''): IconName {
return ICONS[type.toLowerCase()] || 'credit-card';
}

function removeTypes(props: InputProps): Omit<InputProps, 'types'> {
delete props.types;
return props;
}

function isAllowedCardType(x: string | undefined, allowedTypes: CardType[]): x is CardType {
// ok to cast here because we are inside the type card that validates the cast
return allowedTypes.includes(x as CardType);
}

const defaultProps = {
...removeTypes(Input.defaultProps),
className: '',
types: Object.keys(ICONS) as CardType[],
onChange: () => {},
Expand Down
4 changes: 0 additions & 4 deletions src/components/Input/StaticInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ const StaticInput: FC<StaticInputProps> = ({ children, ...props }) => (

StaticInput.displayName = 'StaticInput';

StaticInput.defaultProps = {
...Input.defaultProps,
};

export default StaticInput;
12 changes: 4 additions & 8 deletions src/components/Modal/Modal.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import React from 'react';
import { Modal } from 'reactstrap';

Modal.defaultProps = {
...Modal.defaultProps,
backdrop: false,
fade: false,
zIndex: 10050,
};

export default Modal;
export default ({ backdrop = false, fade = false, zIndex = 10050, ...props }) => (
<Modal {...props} backdrop={backdrop} fade={fade} zIndex={zIndex} />
);
12 changes: 9 additions & 3 deletions src/components/Modal/Modal.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert';
import { mount } from 'enzyme';
import { render, screen } from '@testing-library/react';

Check warning on line 3 in src/components/Modal/Modal.spec.js

View workflow job for this annotation

GitHub Actions / test

`@testing-library/react` import should occur before import of `enzyme`

Check warning on line 3 in src/components/Modal/Modal.spec.js

View workflow job for this annotation

GitHub Actions / test

'screen' is defined but never used

Check failure on line 3 in src/components/Modal/Modal.spec.js

View workflow job for this annotation

GitHub Actions / test

'screen' is defined but never used
import React from 'react';
import Button from '../Button/Button';
import Modal from './Modal';
Expand All @@ -24,9 +25,14 @@ describe('<Modal />', () => {
}
});

it('should render correctly', () => {
component = mount(<Modal toggle={noop} />);
assert(component);
it('should render without modal backdrop by default', () => {
render(<Modal isOpen />);
expect(document.querySelector('.modal-backdrop')).not.toBeInTheDocument();
});

it('should render with modal backdrop if backdrop is set to true', () => {
render(<Modal isOpen backdrop />);
expect(document.querySelector('.modal-backdrop')).toBeInTheDocument();
});

describe('when autoFocus="true"', () => {
Expand Down
8 changes: 2 additions & 6 deletions src/components/Progress/Progress.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import React from 'react';
import { Progress } from 'reactstrap';

Progress.defaultProps = {
...Progress.defaultProps,
animated: true,
};

export default Progress;
export default ({ animated = true, ...props }) => <Progress {...props} animated={animated} />;
16 changes: 8 additions & 8 deletions src/components/Progress/Progress.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import assert from 'assert';
import { mount } from 'enzyme';
import { render, screen } from '@testing-library/react';
import React from 'react';
import Progress from './Progress';

describe('<Progress />', () => {
it('should render correctly', () => {
const component = mount(<Progress />);
assert(component);
it('should render with the correct default props', () => {
render(<Progress />);
const progressBar = screen.getByRole('progressbar');
expect(progressBar).toHaveClass('progress-bar-animated');
});

it('should render with the correct default props', () => {
const component = mount(<Progress />);
const props = component.props();
assert.equal(props.animated, true);
render(<Progress animated={false} />);
const progressBar = screen.getByRole('progressbar');
expect(progressBar).not.toHaveClass('progress-bar-animated');
});
});
2 changes: 0 additions & 2 deletions src/components/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const SpinnerWrapper = ({ type, label, ...props }: SpinnerProps) =>
);

SpinnerWrapper.defaultProps = {
// TODO: update, Spinner doesn't contain the defaultProps types so we cast as any
...(Spinner as any).defaultProps,
type: 'spin',
label: 'loading',
};
Expand Down
4 changes: 0 additions & 4 deletions src/components/Tooltip/UncontrolledTooltip.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { UncontrolledTooltip } from 'reactstrap';

UncontrolledTooltip.defaultProps = {
...UncontrolledTooltip.defaultProps,
};

export default UncontrolledTooltip;

0 comments on commit d3c538e

Please sign in to comment.