Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
update and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Dec 10, 2018
1 parent 5f9b334 commit 1a6c0bd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
20 changes: 11 additions & 9 deletions src/lib/factories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ShorthandRenderer,
} from '../../types/utils'
import { mergeStyles } from './mergeThemes'
import { isForwardRefComponent } from './forwardRefFactory/componentUtils'

type HTMLTag = 'iframe' | 'img' | 'input'
type ShorthandProp = 'children' | 'src' | 'type'
Expand Down Expand Up @@ -81,12 +82,17 @@ export function createShorthand(
* @returns {function} A shorthand factory function waiting for `val` and `defaultProps`.
*/
export function createShorthandFactory(Component: React.ReactType, mappedProp?: string) {
// TODO: Add assert
// if (typeof Component !== 'function' && typeof Component !== 'string') {
// throw new Error('createShorthandFactory() Component must be a string or function.')
// }
if (
typeof Component === 'function' ||
typeof Component === 'string' ||
isForwardRefComponent(Component)
) {
return (val, options) => createShorthand(Component, mappedProp, val, options)
}

return (val, options) => createShorthand(Component, mappedProp, val, options)
throw new Error(
'createShorthandFactory() Component must be a string, a function or to be wrapped with forwardRef().',
)
}

// ============================================================
Expand All @@ -99,10 +105,6 @@ function createShorthandFromValue(
value?: ShorthandValue,
options: CreateShorthandOptions = CREATE_SHORTHAND_DEFAULT_OPTIONS,
) {
// TODO: Add assert
// if (typeof Component !== 'function' && typeof Component !== 'string') {
// throw new Error('createShorthand() Component must be a string or function.')
// }
// short circuit noop values
const valIsNoop = _.isNil(value) || typeof value === 'boolean'
if (valIsNoop && !options.render) return null
Expand Down
29 changes: 6 additions & 23 deletions test/specs/lib/factories-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ describe('factories', () => {
expect(goodUsage).not.toThrowError()
})

test('does not throw if passed a Component is wrapped with forwardRef()', () => {
const goodUsage = () => createShorthandFactory(React.forwardRef(() => <div />), '')

expect(goodUsage).not.toThrowError()
})

test('throw if passed Component that is not a string nor function', () => {
consoleUtil.disableOnce()
const badComponents: any = [undefined, null, true, false, [], {}, 123]
Expand All @@ -181,29 +187,6 @@ describe('factories', () => {
expect(typeof createShorthand).toBe('function')
})

test('does not throw if passed a function Component', () => {
const goodUsage = () => createShorthand(() => <div />, '')

expect(goodUsage).not.toThrowError()
})

test('does not throw if passed a string Component', () => {
const goodUsage = () => createShorthand('div', '')

expect(goodUsage).not.toThrowError()
})

test('throw if passed Component that is not a string nor function', () => {
consoleUtil.disableOnce()
const badComponents: any[] = [undefined, null, true, false, [], {}, 123]

_.each(badComponents, badComponent => {
const badUsage = () => createShorthand(badComponent, '')

expect(badUsage).toThrowError()
})
})

describe('render callback', () => {
test('returns the same React element as if shorthand value would be passed directly', () => {
const createShorthandElement = valueOrRenderCallback =>
Expand Down
30 changes: 30 additions & 0 deletions test/specs/lib/forwardRefFactory/componentUtils-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isForwardRefComponent, supportsRef } from 'src/lib/forwardRefFactory/componentUtils'
import { DOMClass, DOMFunction, ForwardedRef } from '../../components/Ref/fixtures'

describe('componentUtils', () => {
test('isForwardRefComponent', () => {
it('returns "false" when passed component do not support ref forwarding', () => {
[DOMClass, DOMFunction, 'div'].map(Component => {
expect(isForwardRefComponent(Component)).toBe(false)
})
})

it('returns "true" when passed component supports ref forwarding', () => {
expect(isForwardRefComponent(ForwardedRef)).toBe(false)
})
})

describe('supportsRef', () => {
it('returns "false" when passed component do not support "ref" prop', () => {
[DOMClass, DOMFunction, 'div'].map(Component => {
expect(supportsRef(Component)).toBe(false)
})
})

it('returns "true" when passed component supports "ref" prop', () => {
['div', ForwardedRef].map(Component => {
expect(supportsRef(Component)).toBe(true)
})
})
})
})

0 comments on commit 1a6c0bd

Please sign in to comment.