-
-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mobx: Generic inject and observer (#1327)
- Loading branch information
Showing
2 changed files
with
88 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// @ts-check | ||
|
||
import { render } from 'inferno'; | ||
import { Component } from 'inferno-component'; | ||
import { inject, observer, Provider } from 'inferno-mobx'; | ||
import * as mobx from 'mobx'; | ||
|
||
describe('generic higher order components', () => { | ||
let container; | ||
|
||
beforeEach(function() { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(function() { | ||
render(null, container); | ||
container.innerHTML = ''; | ||
document.body.removeChild(container); | ||
}); | ||
|
||
it('injects and observes', done => { | ||
/** @type {<T>(x: T | null | undefined) => T} */ | ||
const nullthrows = (/** @type {any} */ x) => { | ||
if (!x) { | ||
throw new Error("Unexpected falsy value."); | ||
} | ||
|
||
return x; | ||
}; | ||
|
||
class ApiService { | ||
constructor() { | ||
this.foo = 'bar'; | ||
} | ||
} | ||
|
||
class TodoService { | ||
constructor() { | ||
this.baz = 'qux'; | ||
} | ||
} | ||
|
||
/** | ||
* @typedef IProps | ||
* @property {ApiService?} [apiService] | ||
* @property {TodoService?} [todoService] | ||
* | ||
* @extends Component<IProps> | ||
*/ | ||
class TodoView extends Component { | ||
render() { | ||
const { foo } = nullthrows(this.props.apiService); | ||
const { baz } = nullthrows(this.props.todoService); | ||
|
||
return <p>{foo}{baz}</p>; | ||
} | ||
} | ||
|
||
let Todo = inject("apiService", "todoService")(observer(TodoView)); | ||
|
||
// Legacy. | ||
Todo = observer(["apiService", "todoService"])(TodoView); | ||
Todo = observer(["apiService", "todoService"], TodoView); | ||
|
||
const services = { | ||
apiService: new ApiService(), | ||
todoService: new TodoService() | ||
}; | ||
|
||
const A = () => ( | ||
<Provider {...services}> | ||
<Todo /> | ||
</Provider> | ||
); | ||
|
||
render(<A />, container); | ||
expect(container.querySelector('p').textContent).toBe('barqux'); | ||
|
||
done(); | ||
}); | ||
}); |
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