This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forwardProps): allow to forward innerRef (#351)
* Allow to forward innerRef for both normal glamorous and tiny glamorous * Add danielberndt to contributors * Fix url in danielberndt’s contributor profile * apply innerRef-forwarding logic to all cases * moved above now-irrelevant comment section * Move ref tests into its own refs.js file Also renamed nodeType to node within these tests
- Loading branch information
1 parent
0cc1006
commit 73cbc5f
Showing
8 changed files
with
211 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* eslint func-style:0, react/prop-types:0 */ | ||
import React from 'react' | ||
import {mount} from 'enzyme' | ||
import glamorous from '../' | ||
|
||
const nodeEnv = process.env.NODE_ENV | ||
|
||
jest.mock('../constants') | ||
|
||
afterEach(() => { | ||
process.env.NODE_ENV = nodeEnv | ||
}) | ||
|
||
test('should receive inner ref if specified', () => { | ||
const getRef = jest.fn() | ||
const Comp = glamorous.div({ | ||
marginLeft: '24px', | ||
}) | ||
|
||
mount(<Comp innerRef={getRef} />) | ||
|
||
expect(getRef).toHaveBeenCalled() | ||
}) | ||
|
||
test('should pass inner ref to underlying component if forwarded', () => { | ||
let node = null | ||
const getRef = n => (node = n) | ||
|
||
class InnerComp extends React.Component { | ||
render() { | ||
return ( | ||
<div> | ||
<span ref={this.props.innerRef} /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const Comp = glamorous(InnerComp, {forwardProps: ['innerRef']})({ | ||
marginLeft: '24px', | ||
}) | ||
|
||
mount(<Comp innerRef={getRef} />) | ||
|
||
expect(node).toBeInstanceOf(HTMLElement) | ||
expect(node.tagName).toBe('SPAN') | ||
}) | ||
|
||
test('should not pass inner ref to underlying component if not forwarded', () => { | ||
let node = null | ||
const getRef = n => (node = n) | ||
|
||
class InnerComp extends React.Component { | ||
render() { | ||
return ( | ||
<div> | ||
<span ref={this.props.innerRef} /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const Comp = glamorous(InnerComp)({ | ||
marginLeft: '24px', | ||
}) | ||
|
||
mount(<Comp innerRef={getRef} />) | ||
|
||
expect(node).toBeInstanceOf(InnerComp) | ||
}) | ||
|
||
test('should pass inner ref to underlying component if forwarded and rootEl is set', () => { | ||
let node = null | ||
const getRef = n => (node = n) | ||
|
||
class InnerComp extends React.Component { | ||
render() { | ||
return ( | ||
<div> | ||
<span ref={this.props.innerRef} /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const Comp = glamorous(InnerComp, { | ||
rootEl: 'div', | ||
forwardProps: ['innerRef'], | ||
})({ | ||
marginLeft: '24px', | ||
}) | ||
|
||
mount(<Comp innerRef={getRef} />) | ||
|
||
expect(node).toBeInstanceOf(HTMLElement) | ||
expect(node.tagName).toBe('SPAN') | ||
}) | ||
|
||
test('should pass inner ref to underlying component if forwarded and filterProps are specified', () => { | ||
let node = null | ||
const getRef = n => (node = n) | ||
|
||
class InnerComp extends React.Component { | ||
render() { | ||
return ( | ||
<div> | ||
<span ref={this.props.innerRef} /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const Comp = glamorous(InnerComp, { | ||
filterProps: ['noname'], | ||
forwardProps: ['innerRef'], | ||
})({ | ||
marginLeft: '24px', | ||
}) | ||
|
||
mount(<Comp innerRef={getRef} />) | ||
|
||
expect(node).toBeInstanceOf(HTMLElement) | ||
expect(node.tagName).toBe('SPAN') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters