From eaf41075bb41d045cf70e8027e9cc36b474f1445 Mon Sep 17 00:00:00 2001 From: Oleksandr Fediashov Date: Mon, 28 Sep 2020 10:59:33 +0200 Subject: [PATCH 1/4] update unit tests --- test/specs/lib/customPropTypes-test.js | 120 ++++++++++++++++++++----- 1 file changed, 100 insertions(+), 20 deletions(-) diff --git a/test/specs/lib/customPropTypes-test.js b/test/specs/lib/customPropTypes-test.js index 1d7394529f..ae615d7091 100644 --- a/test/specs/lib/customPropTypes-test.js +++ b/test/specs/lib/customPropTypes-test.js @@ -1,42 +1,122 @@ +import PropTypes from 'prop-types' import { customPropTypes } from 'src/lib' -describe('suggest prop type', () => { - it('should throw error when non-array argument given', () => { - expect(() => customPropTypes.suggest('foo')).to.throw( - Error, - /Invalid argument supplied to suggest, expected an instance of array./, - ) +import { consoleUtil, sandbox } from 'test/utils' + +/* eslint-disable no-console */ + +describe.only('customPropTypes', () => { + beforeEach(() => { + consoleUtil.disable() + sandbox.spy(console, 'error') }) - it('should return undefined when prop is valid', () => { - const propType = customPropTypes.suggest(['foo', 'bar', 'baz']) - expect(propType({ name: 'bar' }, 'name', 'FooComponent')).to.equal(undefined) + describe('every', () => { + it('should throw error when a non-array argument given', () => { + PropTypes.checkPropTypes( + { + name: customPropTypes.every('foo'), + }, + { name: 'foo' }, + 'name', + 'FooComponent', + ) + + console.error.should.have.been.calledWithMatch( + /Invalid argument supplied to every, expected an instance of array/, + ) + }) + + it('should throw error when a non-function argument given', () => { + PropTypes.checkPropTypes( + { + name: customPropTypes.every([{}]), + }, + { name: 'foo' }, + 'name', + 'FooComponent', + ) + + console.error.should.have.been.calledWithMatch( + /argument "validators" should contain functions/, + ) + }) + + it('should execute all validators', () => { + PropTypes.checkPropTypes( + { + name: customPropTypes.every([PropTypes.string]), + }, + { name: 1 }, + 'name', + 'FooComponent', + ) + + console.error.should.have.been.calledWithMatch( + /Invalid name `name` of type `number` supplied/, + ) + }) }) - it('should return Error with suggestions when prop is invalid', () => { - const propType = customPropTypes.suggest(['foo', 'bar', 'baz']) - const props = { name: 'bad', title: 'bat words' } + describe('suggest', () => { + it('should throw error when non-array argument given', () => { + expect(() => customPropTypes.suggest('foo')).to.throw( + Error, + /Invalid argument supplied to suggest, expected an instance of array./, + ) + }) + + it('should return undefined when prop is valid', () => { + PropTypes.checkPropTypes( + { + name: customPropTypes.suggest(['foo', 'bar', 'baz']), + }, + { name: 'foo' }, + 'name', + 'FooComponent', + ) + + console.error.should.have.not.been.called() + }) - const resultFooComponent = propType(props, 'name', 'FooComponent') - expect(resultFooComponent).to.be.an.instanceof(Error) - expect(resultFooComponent.message).to - .equal(`Invalid prop \`name\` of value \`bad\` supplied to \`FooComponent\`. + it('should return Error with suggestions when prop is invalid', () => { + PropTypes.checkPropTypes( + { + name: customPropTypes.suggest(['foo', 'bar', 'baz']), + }, + { name: 'bad' }, + 'name', + 'FooComponent', + ) + + console.error.should.have.been + .calledWithMatch(`Invalid prop \`name\` of value \`bad\` supplied to \`FooComponent\`. Instead of \`bad\`, did you mean: - bar - baz - foo `) + }) + + it('should return Error with suggestions when prop contains multiple words and is invalid', () => { + PropTypes.checkPropTypes( + { + name: customPropTypes.suggest(['foo', 'bar', 'baz']), + }, + { name: 'bat words' }, + 'name', + 'FooComponent', + ) - const resultBarComponent = propType(props, 'title', 'BarComponent') - expect(resultBarComponent).to.be.an.instanceof(Error) - expect(resultBarComponent.message).to - .equal(`Invalid prop \`title\` of value \`bat words\` supplied to \`BarComponent\`. + console.error.should.have.been + .calledWithMatch(`Invalid prop \`name\` of value \`bat words\` supplied to \`FooComponent\`. Instead of \`bat words\`, did you mean: - bar - baz - foo `) + }) }) }) From 02f798ad9d1d5817bc06453791489778746c60f7 Mon Sep 17 00:00:00 2001 From: Oleksandr Fediashov Date: Mon, 28 Sep 2020 11:13:15 +0200 Subject: [PATCH 2/4] remove customPropTypes.some, rewrite propTypes to avoid lodash/fp --- src/collections/Form/FormField.js | 2 +- src/elements/Button/Button.js | 4 +- src/lib/customPropTypes.js | 112 ++++++++++--------------- test/specs/lib/customPropTypes-test.js | 13 +++ 4 files changed, 61 insertions(+), 70 deletions(-) diff --git a/src/collections/Form/FormField.js b/src/collections/Form/FormField.js index 9c90e5099d..b380860bd6 100644 --- a/src/collections/Form/FormField.js +++ b/src/collections/Form/FormField.js @@ -163,7 +163,7 @@ FormField.propTypes = { * Extra FormField props are passed to the control component. * Mutually exclusive with children. */ - control: customPropTypes.some([ + control: PropTypes.oneOfType([ PropTypes.func, PropTypes.oneOf(['button', 'input', 'select', 'textarea']), ]), diff --git a/src/elements/Button/Button.js b/src/elements/Button/Button.js index c8a5adddbf..4d37510153 100644 --- a/src/elements/Button/Button.js +++ b/src/elements/Button/Button.js @@ -247,7 +247,7 @@ Button.propTypes = { fluid: PropTypes.bool, /** Add an Icon by name, props object, or pass an . */ - icon: customPropTypes.some([ + icon: PropTypes.oneOfType([ PropTypes.bool, PropTypes.string, PropTypes.object, @@ -258,7 +258,7 @@ Button.propTypes = { inverted: PropTypes.bool, /** Add a Label by text, props object, or pass a