|
| 1 | +import path from 'path' |
| 2 | +import fakeProps from '../src' |
| 3 | + |
| 4 | +const ComponentFile = path.join( |
| 5 | + __dirname, |
| 6 | + '../fixtures/typescript/Component.tsx' |
| 7 | +) |
| 8 | +const SimpleFile = path.join(__dirname, '../fixtures/typescript/Simple.tsx') |
| 9 | +const NoProps = require.resolve('../fixtures/typescript/NoProps.tsx') |
| 10 | +const Multiple = require.resolve('../fixtures/typescript/Multiple.tsx') |
| 11 | + |
| 12 | +describe('fakeProps', () => { |
| 13 | + it('should return an object with no props (snapshot)', () => { |
| 14 | + expect(fakeProps(NoProps)).toMatchSnapshot() |
| 15 | + }) |
| 16 | + |
| 17 | + it('should return an object with required props faked (snapshot)', () => { |
| 18 | + expect(fakeProps(ComponentFile)).toMatchSnapshot() |
| 19 | + }) |
| 20 | + |
| 21 | + it('should return an object with all props faked (snapshot)', () => { |
| 22 | + expect(fakeProps(ComponentFile, { optional: true })).toMatchSnapshot() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should return a person on calling getFriend prop function (test function generation)', () => { |
| 26 | + const allProps = fakeProps(ComponentFile, { optional: true }) |
| 27 | + const friend = allProps.getFriend() |
| 28 | + |
| 29 | + expect(friend.name).toBeDefined() |
| 30 | + expect(friend.name).toBe('getFriend.name') |
| 31 | + expect(friend.age).toBe(1) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should return more props with optional', () => { |
| 35 | + const allProps = fakeProps(ComponentFile, { optional: true }) |
| 36 | + const requiredProps = fakeProps(ComponentFile) |
| 37 | + |
| 38 | + const allPropsLength = Object.keys(allProps).length |
| 39 | + const requiredPropsLength = Object.keys(requiredProps).length |
| 40 | + |
| 41 | + expect(allPropsLength).toBeGreaterThan(requiredPropsLength) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should return an object with shape props faked', () => { |
| 45 | + const props = fakeProps(SimpleFile) |
| 46 | + const { requiredString, requiredObject } = props |
| 47 | + |
| 48 | + expect(props).toEqual({ |
| 49 | + requiredString, |
| 50 | + requiredObject: { |
| 51 | + requiredString: requiredObject.requiredString |
| 52 | + } |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should prefix shape props', () => { |
| 57 | + const props = fakeProps(SimpleFile) |
| 58 | + expect(props.requiredObject.requiredString).toBe( |
| 59 | + 'requiredObject.requiredString' |
| 60 | + ) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should work with multiple components', () => { |
| 64 | + const component = fakeProps(Multiple, { all: true }).find( |
| 65 | + ({ displayName }) => displayName === 'First' |
| 66 | + ) |
| 67 | + expect(component.props.requiredString).toBe('requiredString') |
| 68 | + }) |
| 69 | +}) |
0 commit comments