Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix observer and multiple observer for class components #16

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@umbrellio/observable",
"version": "1.4.2",
"version": "1.4.3",
"description": "Observable library",
"repository": "git@github.com:umbrellio/observable.git",
"author": "Aleksei Bespalov <nulldefiner@gmail.com>",
Expand Down
2 changes: 1 addition & 1 deletion src/multipleObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const multipleObserver = stores => WrappedComponent => {
}
}, {})

return WrappedComponent({ ...this.props, ...state })
return <WrappedComponent {...this.props} {...state} />
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const observer = (store, { key, map }) => WrappedComponent => {

render () {
const state = { [key]: map ? map(this.state) : this.state }
return WrappedComponent({ ...this.props, ...state })
return <WrappedComponent {...this.props} {...state} />
}
}
}
Expand Down
66 changes: 62 additions & 4 deletions tests/multipleObserver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,61 @@ const TestComponent = props => {
return <pre dangerouslySetInnerHTML={{ __html: JSON.stringify(props) }} />
}

const shallowComponent = observer => {
const ObservedComponent = observer(TestComponent)
class ClassTestComponent extends React.Component {
constructor (props) {
super(props)
this.state = {
count: 0,
}
}

handleClick = () => {
this.setState(prev => {
const next = prev === this.props.maxCount ? prev : prev + 1
return { count: next }
})
}

render () {
return (
<div>
<button onClick={this.handleClick}>+</button>
<span>{`Count: ${this.state.count}`}</span>
<span>{`Max: ${this.props.count.max}`}</span>
<span>{`Value: ${this.props.info.key}`}</span>
</div>
)
}
}

const shallowComponent = (observer, component) => {
const ObservedComponent = observer(component)
return Enzyme.shallow(<ObservedComponent />)
}

let store = null
let anotherStore = null
let countStore = null

beforeAll(() => {
Enzyme.configure({ adapter: new Adapter() })
store = observable({ key: "initial value" })
anotherStore = observable({ anotherKey: "another initial value" })
countStore = observable({ max: 10 })
})

beforeEach(() => {
store.reset()
anotherStore.reset()
countStore.reset()
})

it("wraps a component with the multiple observable", () => {
const observer = multipleObserver([
{ store, key: "value" },
{ store: anotherStore, key: "anotherValue" },
])
const component = shallowComponent(observer)
const component = shallowComponent(observer, TestComponent)
expect(component.html()).toEqual(
"<pre>" +
"{\"value\":{\"key\":\"initial value\"}," +
Expand All @@ -56,7 +86,7 @@ it("wraps a component with the multiple observable (mapped)", () => {
{ store, key: "value", map: state => state.key },
{ store: anotherStore, key: "anotherValue", map: state => state.anotherKey },
])
const component = shallowComponent(observer)
const component = shallowComponent(observer, TestComponent)
expect(component.html()).toEqual(
"<pre>" +
"{\"value\":\"initial value\"," +
Expand All @@ -74,3 +104,31 @@ it("wraps a component with the multiple observable (mapped)", () => {
)
component.unmount()
})

it("wraps a class component with the multiple observable", () => {
const observer = multipleObserver([
{ store, key: "info" },
{ store: countStore, key: "count" },
])
const component = shallowComponent(observer, ClassTestComponent)
expect(component.html()).toEqual(
"<div>" +
"<button>+</button>" +
"<span>Count: 0</span>" +
"<span>Max: 10</span>" +
"<span>Value: initial value</span>" +
"</div>",
)

store.set({ key: "new value" })
countStore.set({ max: 20 })
expect(component.html()).toEqual(
"<div>" +
"<button>+</button>" +
"<span>Count: 0</span>" +
"<span>Max: 20</span>" +
"<span>Value: new value</span>" +
"</div>",
)
component.unmount()
})
49 changes: 45 additions & 4 deletions tests/observer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,57 @@ const TestComponent = props => {
return <pre dangerouslySetInnerHTML={{ __html: JSON.stringify(props) }} />
}

const shallowComponent = observer => {
const ObservedComponent = observer(TestComponent)
class ClassTestComponent extends React.Component {
constructor (props) {
super(props)
this.state = {
count: 0,
}
}

handleClick = () => {
this.setState(prev => {
const next = prev === this.props.maxCount ? prev : prev + 1
return { count: next }
})
}

render () {
return (
<div>
<button onClick={this.handleClick}>+</button>
<span>{`Count: ${this.state.count}`}</span>
<span>{`Max: ${this.props.count.max}`}</span>
</div>
)
}
}

const shallowComponent = (observer, component) => {
const ObservedComponent = observer(component)
return Enzyme.shallow(<ObservedComponent />)
}

let store = null
let anotherStore = null
let countStore = null

beforeAll(() => {
Enzyme.configure({ adapter: new Adapter() })
store = observable({ key: "initial value" })
anotherStore = observable({ anotherKey: "another initial value" })
countStore = observable({ max: 10 })
})

beforeEach(() => {
store.reset()
anotherStore.reset()
countStore.reset()
})

it("wraps a component with the observable", () => {
const observer = store.observer({ key: "value" })
const component = shallowComponent(observer)
const component = shallowComponent(observer, TestComponent)
expect(component.html()).toEqual("<pre>{\"value\":{\"key\":\"initial value\"}}</pre>")

store.set({ key: "new value" })
Expand All @@ -39,10 +68,22 @@ it("wraps a component with the observable", () => {

it("wraps a component with the observable (mapped)", () => {
const observer = store.observer({ key: "value", map: state => state.key })
const component = shallowComponent(observer)
const component = shallowComponent(observer, TestComponent)
expect(component.html()).toEqual("<pre>{\"value\":\"initial value\"}</pre>")

store.set({ key: "new value" })
expect(component.html()).toEqual("<pre>{\"value\":\"new value\"}</pre>")
component.unmount()
})

it("wraps a class component with the observable", () => {
const observer = countStore.observer({ key: "count" })
const component = shallowComponent(observer, ClassTestComponent)
expect(component.html())
.toEqual("<div><button>+</button><span>Count: 0</span><span>Max: 10</span></div>")

countStore.set({ max: 20 })
expect(component.html())
.toEqual("<div><button>+</button><span>Count: 0</span><span>Max: 20</span></div>")
component.unmount()
})