-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix composed components order after rerender (#124)
* fix rerenders * add test * use better api
- Loading branch information
Showing
5 changed files
with
64 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
import * as React from 'react' | ||
import TestRenderer from 'react-test-renderer' | ||
import { Compose, Counter, Toggle } from '../../src' | ||
import { lastCallArgs } from './utils' | ||
|
||
test('<Compose />', () => { | ||
let lastCallProps = null | ||
const renderFn = (...props) => { | ||
lastCallProps = props | ||
return null | ||
} | ||
const renderFn = jest.fn().mockReturnValue(null) | ||
|
||
TestRenderer.create( | ||
<Compose components={[Counter, Toggle]} render={renderFn} /> | ||
) | ||
|
||
expect(lastCallProps[0].count).toBe(0) | ||
expect(lastCallProps[1].on).toBe(false) | ||
expect(renderFn).toBeCalledTimes(1) | ||
expect(renderFn).lastCalledWith( | ||
expect.objectContaining({ count: 0 }), | ||
expect.objectContaining({ on: false }) | ||
) | ||
|
||
lastCallProps[0].inc() | ||
lastCallProps[0].incBy(3) | ||
lastCallProps[1].toggle() | ||
lastCallArgs(renderFn)[0].inc() | ||
lastCallArgs(renderFn)[0].incBy(3) | ||
lastCallArgs(renderFn)[1].toggle() | ||
|
||
expect(lastCallProps[0].count).toBe(4) | ||
expect(lastCallProps[1].on).toBe(true) | ||
expect(renderFn).toBeCalledTimes(4) | ||
expect(renderFn).lastCalledWith( | ||
expect.objectContaining({ count: 4 }), | ||
expect.objectContaining({ on: true }) | ||
) | ||
}) |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const last = arr => arr[Math.max(0, arr.length - 1)] | ||
|
||
export const lastCallArg = mockFn => last(mockFn.mock.calls)[0] | ||
export const lastCallArgs = mockFn => last(mockFn.mock.calls) |
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,36 @@ | ||
import * as React from 'react' | ||
import TestRenderer from 'react-test-renderer' | ||
import { compose, Counter, Toggle } from '../../src' | ||
|
||
test('rerender composed component', () => { | ||
const CounterToggle = compose(Counter, Toggle) | ||
const renderFn = jest.fn().mockReturnValue(null) | ||
let rerender = null | ||
|
||
TestRenderer.create( | ||
<Toggle> | ||
{({ on, toggle }) => { | ||
rerender = toggle | ||
|
||
return ( | ||
// hard rerender | ||
<CounterToggle key={on} render={renderFn} /> | ||
) | ||
}} | ||
</Toggle> | ||
) | ||
|
||
expect(renderFn).toBeCalledTimes(1) | ||
expect(renderFn).lastCalledWith( | ||
expect.objectContaining({ count: 0 }), | ||
expect.objectContaining({ on: false }) | ||
) | ||
|
||
rerender() | ||
|
||
expect(renderFn).toBeCalledTimes(2) | ||
expect(renderFn).lastCalledWith( | ||
expect.objectContaining({ count: 0 }), | ||
expect.objectContaining({ on: false }) | ||
) | ||
}) |