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

Add injectNames to use DI in react2angular #49

Merged
merged 7 commits into from
Dec 11, 2017
Merged

Conversation

fand
Copy link
Contributor

@fand fand commented Oct 30, 2017

Currently react2angular cannot use the DI system of AngularJS.
In this PR I added injectNames to react2angular to specify injectables to converted components.

When I do this:

angular.component(react2angular(ReactComponent, null, ['$window', '$location']))

then injected items will be passed to the props like this:

const ReactComponent = ({ $window, $location }) => (
  <ul>
    <li>window size : {$window.innerWidth} x {$window.innerHeight}</li>
    <li>location.href : {$location.href}</li>  
  </ul>
)

This PR is based on #48 in order to run tests locally.
Commits before 1a3ae9f is not necessary for this PR.

Copy link
Contributor

@bcherny bcherny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @fand! This is a fantastic idea! I was using this lib with ngimport, but for those users that aren't this is really helpful!

Please address the few comments I left, then I'm happy to merge this. After merge I'll update the docs, and will work on making the DI typesafe (ie. if I pass $http to a React component, $http should be well-typed and not an any).

package.json Outdated
@@ -57,7 +57,7 @@
"@types/angular": ">=1.5",
"@types/lodash.frompairs": "^4.0.3",
"@types/react": "^16.0.0",
"@types/react-dom": "^16.0.0",
"@types/react-dom": "16.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be ^16.0.1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. When I install @types/[email protected] or later version it breaks the build, so we have to specify 16.0.1 😇

index.tsx Outdated
): IComponentOptions {
const names = bindingNames
|| (Class.propTypes && Object.keys(Class.propTypes))
|| []

return {
bindings: fromPairs(names.map(_ => [_, '<'])),
controller: ['$element', class extends NgComponent<Props> {
constructor(private $element: IAugmentedJQuery) {
controller: ['$element', ...(injectNames as any), class extends NgComponent<Props> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the as any here?

Copy link
Contributor Author

@fand fand Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 9bcc7c5

index.tsx Outdated
}
render() {
// TODO: rm any when https://github.com/Microsoft/TypeScript/pull/13288 is merged
render(<Class {...(this.props as any)} />, this.$element[0])
render(<Class {...(this.props as any)} {...this.injectedProps} />, this.$element[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider reversing the order of {...this.injectedProps} and {...(this.props as any)}, so props take priority.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, okay. Fixed the order and added some tests!

it('should use the injectNames for DI', () => {
const defaultDi = (react2angular(TestThree).controller as any).slice(0, -1)
const injectedDi = (react2angular(TestThree, null, ['foo', 'bar']).controller as any).slice(0, -1)
expect(injectedDi).toEqual(defaultDi.concat(['foo', 'bar']))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of testing arity, please add broader tests. For example, you might consider injecting $http, spying on $http.get, and then calling it from the controller.

Also please add a test for a custom (not built-in) service: register it with angular.service, inject it, spy on it, and similarly make sure it gets called.

Copy link
Contributor Author

@fand fand Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests using $http, $q, $element and a custom service.
8e03472
Also added tests for checking the priority of bindingNames, propTypes and injectedProps.
893bdc5

@fand fand changed the title [WIP] Add injectNames to use DI in react2angular Add injectNames to use DI in react2angular Nov 8, 2017
@bcherny bcherny mentioned this pull request Nov 28, 2017
@bcherny
Copy link
Contributor

bcherny commented Dec 3, 2017

@fand I missed your updates earlier - is this PR ready to go?

@inakiarroyo
Copy link

This is cool feature for those who are not using ngImport, sorry @bcherny, but do you have any update about if the conflicts are going to be resolved soon?

@bcherny
Copy link
Contributor

bcherny commented Dec 6, 2017

See above comment - if @fand believes this is ready to go, I'll go ahead and merge it. If they don't respond in a day or two I'll make the judgement myself.

@fand
Copy link
Contributor Author

fand commented Dec 7, 2017

@bcherny
Sorry for late reply!
This is ready to merge, but had conflits to master.
I rebased & push -f ed, please review recent commits 🙏

@bcherny
Copy link
Contributor

bcherny commented Dec 11, 2017

Looks great - thanks for the contribution @fand!

@bcherny bcherny merged commit 2a3ddd8 into coatue-oss:master Dec 11, 2017
@bcherny
Copy link
Contributor

bcherny commented Dec 11, 2017

+ [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants