|
| 1 | +import PropTypes from 'prop-types' |
1 | 2 | import { customPropTypes } from 'src/lib' |
2 | 3 |
|
3 | | -describe('suggest prop type', () => { |
4 | | - it('should throw error when non-array argument given', () => { |
5 | | - expect(() => customPropTypes.suggest('foo')).to.throw( |
6 | | - Error, |
7 | | - /Invalid argument supplied to suggest, expected an instance of array./, |
8 | | - ) |
| 4 | +import { consoleUtil, sandbox } from 'test/utils' |
| 5 | + |
| 6 | +/* eslint-disable no-console */ |
| 7 | + |
| 8 | +describe('customPropTypes', () => { |
| 9 | + beforeEach(() => { |
| 10 | + consoleUtil.disable() |
| 11 | + sandbox.spy(console, 'error') |
9 | 12 | }) |
10 | 13 |
|
11 | | - it('should return undefined when prop is valid', () => { |
12 | | - const propType = customPropTypes.suggest(['foo', 'bar', 'baz']) |
13 | | - expect(propType({ name: 'bar' }, 'name', 'FooComponent')).to.equal(undefined) |
| 14 | + describe('every', () => { |
| 15 | + it('should throw error when a non-array argument given', () => { |
| 16 | + PropTypes.checkPropTypes( |
| 17 | + { |
| 18 | + name: customPropTypes.every('foo'), |
| 19 | + }, |
| 20 | + { name: 'foo' }, |
| 21 | + 'name', |
| 22 | + 'FooComponent', |
| 23 | + ) |
| 24 | + |
| 25 | + console.error.should.have.been.calledWithMatch( |
| 26 | + /Invalid argument supplied to every, expected an instance of array/, |
| 27 | + ) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should throw error when a non-function argument given', () => { |
| 31 | + PropTypes.checkPropTypes( |
| 32 | + { |
| 33 | + name: customPropTypes.every([{}]), |
| 34 | + }, |
| 35 | + { name: 'foo' }, |
| 36 | + 'name', |
| 37 | + 'FooComponent', |
| 38 | + ) |
| 39 | + |
| 40 | + console.error.should.have.been.calledWithMatch( |
| 41 | + /argument "validators" should contain functions/, |
| 42 | + ) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should execute all validators', () => { |
| 46 | + PropTypes.checkPropTypes( |
| 47 | + { |
| 48 | + name: customPropTypes.every([PropTypes.string]), |
| 49 | + }, |
| 50 | + { name: 1 }, |
| 51 | + 'name', |
| 52 | + 'FooComponent', |
| 53 | + ) |
| 54 | + |
| 55 | + console.error.should.have.been.calledWithMatch( |
| 56 | + /Invalid name `name` of type `number` supplied/, |
| 57 | + ) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should execute all validators including custom', () => { |
| 61 | + PropTypes.checkPropTypes( |
| 62 | + { |
| 63 | + name: customPropTypes.every([customPropTypes.suggest(['foo']), PropTypes.number]), |
| 64 | + }, |
| 65 | + { name: 'bar' }, |
| 66 | + 'name', |
| 67 | + 'FooComponent', |
| 68 | + ) |
| 69 | + |
| 70 | + console.error.should.have.been.calledWithMatch(/Instead of `bar`, did you mean:/) |
| 71 | + }) |
14 | 72 | }) |
15 | 73 |
|
16 | | - it('should return Error with suggestions when prop is invalid', () => { |
17 | | - const propType = customPropTypes.suggest(['foo', 'bar', 'baz']) |
18 | | - const props = { name: 'bad', title: 'bat words' } |
| 74 | + describe('suggest', () => { |
| 75 | + it('should throw error when non-array argument given', () => { |
| 76 | + expect(() => customPropTypes.suggest('foo')).to.throw( |
| 77 | + Error, |
| 78 | + /Invalid argument supplied to suggest, expected an instance of array./, |
| 79 | + ) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should return undefined when prop is valid', () => { |
| 83 | + PropTypes.checkPropTypes( |
| 84 | + { |
| 85 | + name: customPropTypes.suggest(['foo', 'bar', 'baz']), |
| 86 | + }, |
| 87 | + { name: 'foo' }, |
| 88 | + 'name', |
| 89 | + 'FooComponent', |
| 90 | + ) |
19 | 91 |
|
20 | | - const resultFooComponent = propType(props, 'name', 'FooComponent') |
21 | | - expect(resultFooComponent).to.be.an.instanceof(Error) |
22 | | - expect(resultFooComponent.message).to |
23 | | - .equal(`Invalid prop \`name\` of value \`bad\` supplied to \`FooComponent\`. |
| 92 | + console.error.should.have.not.been.called() |
| 93 | + }) |
| 94 | + |
| 95 | + it('should return Error with suggestions when prop is invalid', () => { |
| 96 | + PropTypes.checkPropTypes( |
| 97 | + { |
| 98 | + name: customPropTypes.suggest(['foo', 'bar', 'baz']), |
| 99 | + }, |
| 100 | + { name: 'bad' }, |
| 101 | + 'name', |
| 102 | + 'FooComponent', |
| 103 | + ) |
| 104 | + |
| 105 | + console.error.should.have.been |
| 106 | + .calledWithMatch(`Invalid prop \`name\` of value \`bad\` supplied to \`FooComponent\`. |
24 | 107 |
|
25 | 108 | Instead of \`bad\`, did you mean: |
26 | 109 | - bar |
27 | 110 | - baz |
28 | 111 | - foo |
29 | 112 | `) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should return Error with suggestions when prop contains multiple words and is invalid', () => { |
| 116 | + PropTypes.checkPropTypes( |
| 117 | + { |
| 118 | + name: customPropTypes.suggest(['foo', 'bar', 'baz']), |
| 119 | + }, |
| 120 | + { name: 'bat words' }, |
| 121 | + 'name', |
| 122 | + 'FooComponent', |
| 123 | + ) |
30 | 124 |
|
31 | | - const resultBarComponent = propType(props, 'title', 'BarComponent') |
32 | | - expect(resultBarComponent).to.be.an.instanceof(Error) |
33 | | - expect(resultBarComponent.message).to |
34 | | - .equal(`Invalid prop \`title\` of value \`bat words\` supplied to \`BarComponent\`. |
| 125 | + console.error.should.have.been |
| 126 | + .calledWithMatch(`Invalid prop \`name\` of value \`bat words\` supplied to \`FooComponent\`. |
35 | 127 |
|
36 | 128 | Instead of \`bat words\`, did you mean: |
37 | 129 | - bar |
38 | 130 | - baz |
39 | 131 | - foo |
40 | 132 | `) |
| 133 | + }) |
41 | 134 | }) |
42 | 135 | }) |
0 commit comments