Skip to content

Commit ca15456

Browse files
Merge pull request cerebral#107 from cerebral/preserveComponentName
feat(overmind-react): preserve component name on connect
2 parents 4f73f03 + 2367167 commit ca15456

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/node_modules/overmind-react/src/index.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react'
22
import * as renderer from 'react-test-renderer'
3-
import App, { TConfig, TAction } from 'overmind'
3+
4+
import App, { TAction, TConfig } from 'overmind'
45
import createConnect, { TConnect } from './'
56

67
describe('React', () => {
@@ -94,6 +95,7 @@ describe('React', () => {
9495
expect(didCallAction).toBe(true)
9596
expect(tree).toMatchSnapshot()
9697
})
98+
9799
test('should connect reactions to components', () => {
98100
expect.assertions(2)
99101
let reactionCount = 0
@@ -146,4 +148,23 @@ describe('React', () => {
146148
app.actions.doThis()
147149
expect(reactionCount).toBe(1)
148150
})
151+
152+
test('should preserve component name', () => {
153+
const app = new App({})
154+
const connect = createConnect(app)
155+
156+
class FooComponent extends React.Component<TConnect<typeof app>> {
157+
render() {
158+
return <h1>hop</h1>
159+
}
160+
}
161+
function BarComponent() {
162+
return <div />
163+
}
164+
165+
const ConnectedFoo = connect(FooComponent)
166+
const ConnectedBar = connect(BarComponent)
167+
expect(ConnectedFoo.name).toBe('FooComponent')
168+
expect(ConnectedBar.name).toBe('BarComponent')
169+
})
149170
})

packages/node_modules/overmind-react/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react'
2+
23
import OvermindApp, { EventType } from 'overmind'
34

45
export type IReactComponent<P = any> =
@@ -97,7 +98,7 @@ export default <App extends OvermindApp<any, any>>(app: App) => {
9798
}
9899
}
99100

100-
return class extends React.PureComponent<
101+
const klass = class extends React.PureComponent<
101102
Omit<Props & TConnect<App>, keyof TConnect<App>>
102103
> {
103104
__mutationListener: any
@@ -191,5 +192,8 @@ export default <App extends OvermindApp<any, any>>(app: App) => {
191192
return this.renderStatelessComponent()
192193
}
193194
}
195+
196+
Object.defineProperties(klass, { name: { value: Component.displayName || Component.name || '' } })
197+
return klass
194198
}
195199
}

0 commit comments

Comments
 (0)