-
Notifications
You must be signed in to change notification settings - Fork 25
/
test.tsx
145 lines (129 loc) · 3.98 KB
/
test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { auto, element as $, mock, module } from 'angular'
import 'angular-mocks'
import * as React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import { angular2react } from './'
// Angular component
class FooController {
bazMoo1Boo(_value: number) {
return true
}
foo = 'foo'
fooBar = 1
_fooBar = 2
constructor(private fooService: FooService) {
this.fooService.add42(3) // prevent "unused" error
}
onClick() {
if (this.bazMoo1Boo) {
this.bazMoo1Boo(this.fooBar)
}
}
$onDestroy() {}
}
const FooBarBaz = {
bindings: {
bazMoo1Boo: '<',
foo: '<',
fooBar: '<'
},
controller: FooController,
template: `
<span>{{ $ctrl.foo }}</span>
<span>{{ $ctrl.fooBar*12 }}</span>
<span>{{ $ctrl.fooService.add42($ctrl.fooBar) }}</span>
<div ng-click="$ctrl.onClick()">Click Me</div>
<ng-transclude></ng-transclude>
`,
transclude: true
}
class FooService {
add42(n: number) { return n + 42 }
}
module('test', [])
.component('fooBarBaz', FooBarBaz)
.service('fooService', FooService)
let $injector: any
beforeEach(() => {
mock.module('test')
mock.inject(function(_$injector_: auto.IInjectorService) {
$injector = _$injector_
})
})
it('should give a react component', () => {
const Foo2 = compile($injector)
const foo2 = new Foo2({
foo: 'foo',
fooBar: 1
})
expect(foo2 instanceof React.Component).toBe(true)
})
it('should render', () => {
const Foo2 = compile($injector)
const element = document.createElement('div')
render(<Foo2 foo='hello' fooBar={42} />, element)
expect($(element).find('span').eq(0).text()).toBe('hello')
expect($(element).find('span').eq(1).text()).toBe('504')
unmountComponentAtNode(element)
})
it('should update', () => {
const Foo2 = compile($injector)
const element = document.createElement('div')
render(<Foo2 foo='hello' fooBar={42} />, element)
expect($(element).find('span').eq(1).text()).toBe('504')
unmountComponentAtNode(element)
render(<Foo2 foo='hello' fooBar={43} />, element)
expect($(element).find('span').eq(1).text()).toBe('516')
unmountComponentAtNode(element)
})
it('should destroy', () => {
const Foo2 = compile($injector)
const element = document.createElement('div')
render(<Foo2 foo='hello' fooBar={42} />, element)
spyOn(FooController.prototype, '$onDestroy')
const unmounted = unmountComponentAtNode(element)
expect(unmounted).toBeTruthy()
expect(FooController.prototype.$onDestroy).toHaveBeenCalled()
expect(element.childNodes.length).toBe(0)
})
it('should take callbacks', () => {
const Foo2 = compile($injector)
const element = document.createElement('div')
const cb = jasmine.createSpy('bazMoo1Boo')
render(<Foo2 foo='hello' fooBar={42} bazMoo1Boo={cb} />, element)
$(element).find('div').triggerHandler('click')
expect(cb).toHaveBeenCalledWith(42)
unmountComponentAtNode(element)
})
it('should work with dependency injected code', () => {
const Foo2 = compile($injector)
const element = document.createElement('div')
render(<Foo2 foo='hello' fooBar={42} />, element)
expect($(element).find('span').eq(2).text()).toBe('84')
unmountComponentAtNode(element)
})
// TODO: support children
it('should not support children', () => {
const Foo2 = compile($injector)
const element = document.createElement('div')
render(<Foo2 foo='hello' fooBar={42}>
<span>Child</span>
</Foo2>, element)
expect($(element).find('ng-transclude').html()).toBe('')
unmountComponentAtNode(element)
})
it('should use the angular component as the root component', () => {
const Foo2 = compile($injector)
const element = document.createElement('div')
render(<Foo2 foo='hello' fooBar={42} />, element)
expect($(element).find('foo-bar-baz')[0].parentElement).toBe(element)
unmountComponentAtNode(element)
})
interface Props {
bazMoo1Boo?(value: number): any
foo: string
fooBar: number
}
function compile($injector: auto.IInjectorService) {
return angular2react<Props>('fooBarBaz', FooBarBaz, $injector)
}