From b357344ddd8b1dcac3fb848b7dd72341c0ec9ff3 Mon Sep 17 00:00:00 2001 From: fand Date: Thu, 2 Nov 2017 12:31:00 +0900 Subject: [PATCH] Test if props in injectedProps are injected correctly --- test.tsx | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/test.tsx b/test.tsx index bd19ef4..6fc0317 100644 --- a/test.tsx +++ b/test.tsx @@ -1,6 +1,6 @@ import { bootstrap, element as $, ICompileService, mock, module } from 'angular' import 'angular-mocks' -import { $rootScope } from 'ngimport' +import { $rootScope, $http } from 'ngimport' import * as React from 'react' import { Simulate } from 'react-dom/test-utils' import { react2angular } from './' @@ -52,16 +52,56 @@ class TestFive extends React.Component { componentWillUnmount() { } } +class TestSixService { + constructor(private $q: any) {} + + foo() { + return this.$q.resolve('testSixService result') + } +} + +class TestSix extends React.Component { + state = { + $http: '', + $element: '', + testSixService: '', + } + + render() { + return
+

{this.state.$http}

+

{this.state.$element}

+

{this.state.testSixService}

+ $element result +
+ } + + componentDidMount() { + this.props.$http.get('https://example.com/').then((result: string) => { + this.setState({ $http: result }) + }) + this.setState({ + $element: this.props.$element.find('span').text(), + }) + this.props.testSixService.foo().then((result: string) => { + this.setState({ testSixService: result }) + }) + } +} + const TestAngularOne = react2angular(TestOne, ['foo', 'bar', 'baz']) const TestAngularTwo = react2angular(TestTwo, ['foo', 'bar', 'baz']) const TestAngularThree = react2angular(TestThree) const TestAngularFour = react2angular(TestFour) +const TestAngularSix = react2angular(TestSix, null, ['$http', '$element', 'testSixService']) module('test', ['bcherny/ngimport']) .component('testAngularOne', TestAngularOne) .component('testAngularTwo', TestAngularTwo) .component('testAngularThree', TestAngularThree) .component('testAngularFour', TestAngularFour) + .service('testSixService', ['$q', TestSixService]) + .component('testAngularSix', TestAngularSix) bootstrap($(), ['test'], { strictDi: true }) @@ -207,6 +247,23 @@ describe('react2angular', () => { expect(element.find('span').length).toBe(0) }) + it('should take injected props', (done) => { + const scope = $rootScope.$new(true) + spyOn($http, 'get').and.returnValue(Promise.resolve('$http response')) + + const element = $(``) + $compile(element)(scope) + $rootScope.$apply() + + setTimeout(() => { + expect($http.get).toHaveBeenCalledWith('https://example.com/') + expect(element.find('p').eq(0).text()).toBe('$http response', '$http is injected') + expect(element.find('p').eq(1).text()).toBe('$element result', '$element is injected') + expect(element.find('p').eq(2).text()).toBe('testSixService result', 'testSixService is injected') + done() + }, 0) + }) + }) describe('react stateless components', () => {