forked from coatue-oss/react2angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
358 lines (358 loc) · 15.8 KB
/
test.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var angular_1 = require("angular");
require("angular-mocks");
var ngimport_1 = require("ngimport");
var PropTypes = require("prop-types");
var React = require("react");
var test_utils_1 = require("react-dom/test-utils");
var _1 = require("./");
var TestOne = /** @class */ (function (_super) {
__extends(TestOne, _super);
function TestOne() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestOne.prototype.render = function () {
var _this = this;
return React.createElement("div", null,
React.createElement("p", null,
"Foo: ",
this.props.foo),
React.createElement("p", null,
"Bar: ",
this.props.bar.join(',')),
React.createElement("p", { onClick: function () { return _this.props.baz(42); } }, "Baz"),
this.props.children);
};
TestOne.prototype.componentWillUnmount = function () { };
return TestOne;
}(React.Component));
var TestTwo = function (props) {
return React.createElement("div", null,
React.createElement("p", null,
"Foo: ",
props.foo),
React.createElement("p", null,
"Bar: ",
props.bar.join(',')),
React.createElement("p", { onClick: function () { return props.baz(42); } }, "Baz"),
props.children);
};
var TestThree = function () {
return React.createElement("div", null, "Foo");
};
var TestFour = /** @class */ (function (_super) {
__extends(TestFour, _super);
function TestFour() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestFour.prototype.render = function () {
return React.createElement("div", null, "Foo");
};
return TestFour;
}(React.Component));
var TestFive = /** @class */ (function (_super) {
__extends(TestFive, _super);
function TestFive() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestFive.prototype.render = function () {
var _this = this;
return React.createElement("div", null,
React.createElement("p", null,
"Foo: ",
this.props.foo),
React.createElement("p", null,
"Bar: ",
this.props.bar.join(',')),
React.createElement("p", { onClick: function () { return _this.props.baz(42); } }, "Baz"),
this.props.children);
};
TestFive.prototype.componentWillUnmount = function () { };
TestFive.propTypes = {
bar: PropTypes.array.isRequired,
baz: PropTypes.func.isRequired,
foo: PropTypes.number.isRequired
};
return TestFive;
}(React.Component));
var TestSixService = /** @class */ (function () {
function TestSixService($q) {
this.$q = $q;
}
TestSixService.prototype.foo = function () {
return this.$q.resolve('testSixService result');
};
return TestSixService;
}());
var TestSix = /** @class */ (function (_super) {
__extends(TestSix, _super);
function TestSix() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
elementText: '',
result: '',
testSixService: ''
};
return _this;
}
TestSix.prototype.render = function () {
return React.createElement("div", null,
React.createElement("p", null, this.state.result),
React.createElement("p", null, this.state.elementText),
React.createElement("p", null, this.state.testSixService),
React.createElement("p", null, this.props.foo),
React.createElement("span", null, "$element result"));
};
TestSix.prototype.componentDidMount = function () {
var _this = this;
this.setState({
elementText: this.props.$element.find('span').text()
});
this.props.$http.get('https://example.com/').then(function (_) {
return _this.setState({ result: _.data });
});
this.props.testSixService.foo().then(function (_) {
return _this.setState({ testSixService: _ });
});
};
return TestSix;
}(React.Component));
var TestSeven = /** @class */ (function (_super) {
__extends(TestSeven, _super);
function TestSeven() {
return _super !== null && _super.apply(this, arguments) || this;
}
// static propTypes = {
// foo: PropTypes.string.isRequired
// }
TestSeven.prototype.render = function () {
return React.createElement("p", null, this.props.foo);
};
return TestSeven;
}(React.Component));
var TestAngularOne = _1.react2angular(TestOne, ['foo', 'bar', 'baz']);
var TestAngularTwo = _1.react2angular(TestTwo, ['foo', 'bar', 'baz']);
var TestAngularThree = _1.react2angular(TestThree);
var TestAngularFour = _1.react2angular(TestFour);
var TestAngularSix = _1.react2angular(TestSix, ['foo'], ['$http', '$element', 'testSixService', 'foo']);
var TestAngularSeven = _1.react2angular(TestSeven, null, ['foo']);
angular_1.module('test', ['bcherny/ngimport'])
.component('testAngularOne', TestAngularOne)
.component('testAngularTwo', TestAngularTwo)
.component('testAngularThree', TestAngularThree)
.component('testAngularFour', TestAngularFour)
.service('testSixService', ['$q', TestSixService])
.constant('foo', 'CONSTANT FOO')
.component('testAngularSix', TestAngularSix)
.component('testAngularSeven', TestAngularSeven);
angular_1.bootstrap(angular_1.element(), ['test'], { strictDi: true });
describe('react2angular', function () {
var $compile;
beforeEach(function () {
angular_1.mock.module('test');
angular_1.mock.inject(function (_$compile_) {
$compile = _$compile_;
});
});
describe('initialization', function () {
it('should give an angular component', function () {
expect(TestAngularOne.bindings).not.toBe(undefined);
expect(TestAngularOne.controller).not.toBe(undefined);
});
it('should use the propTypes when present and no bindingNames were specified', function () {
var reactAngularComponent = _1.react2angular(TestFive);
expect(reactAngularComponent.bindings).toEqual({
bar: '<',
baz: '<',
foo: '<'
});
});
it('should use the bindingNames when present over the propTypes', function () {
var reactAngularComponent = _1.react2angular(TestFive, ['foo']);
expect(reactAngularComponent.bindings).toEqual({
foo: '<'
});
});
it('should have empty bindings when parameter is an empty array', function () {
var reactAngularComponent = _1.react2angular(TestFive, []);
expect(reactAngularComponent.bindings).toEqual({});
});
it('should have empty bindings when parameter is not passed', function () {
expect(_1.react2angular(TestThree).bindings).toEqual({});
});
it('should use the injectNames for DI', function () {
var defaultDi = _1.react2angular(TestThree).controller.slice(0, -1);
var injectedDi = _1.react2angular(TestThree, null, ['foo', 'bar']).controller.slice(0, -1);
expect(injectedDi).toEqual(defaultDi.concat(['foo', 'bar']));
});
it('should have default DI specifications if injectNames is empty', function () {
var defaultDi = _1.react2angular(TestThree).controller.slice(0, -1);
var injectedDi = _1.react2angular(TestThree, null, []).controller.slice(0, -1);
expect(injectedDi).toEqual(defaultDi);
});
});
describe('react classes', function () {
it('should render', function () {
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: function (value) { return value + 1; },
foo: 1
});
var element = angular_1.element("<test-angular-one foo=\"foo\" bar=\"bar\" baz=\"baz\"></test-angular-one>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
expect(element.find('p').length).toBe(3);
});
it('should render (even if the component takes no props)', function () {
var scope = ngimport_1.$rootScope.$new(true);
var element = angular_1.element("<test-angular-four></test-angular-four>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
expect(element.text()).toBe('Foo');
});
it('should update', function () {
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: function (value) { return value + 1; },
foo: 1
});
var element = angular_1.element("<test-angular-one foo=\"foo\" bar=\"bar\" baz=\"baz\"></test-angular-one>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
expect(element.find('p').eq(1).text()).toBe('Bar: true,false');
scope.$apply(function () {
return scope.bar = [false, true, true];
});
expect(element.find('p').eq(1).text()).toBe('Bar: false,true,true');
});
it('should destroy', function () {
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: function (value) { return value + 1; },
foo: 1
});
var element = angular_1.element("<test-angular-one foo=\"foo\" bar=\"bar\" baz=\"baz\"></test-angular-one>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
spyOn(TestOne.prototype, 'componentWillUnmount');
scope.$destroy();
expect(TestOne.prototype.componentWillUnmount).toHaveBeenCalled();
});
it('should take callbacks', function () {
var baz = jasmine.createSpy('baz');
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: baz,
foo: 1
});
var element = angular_1.element("<test-angular-one foo=\"foo\" bar=\"bar\" baz=\"baz\"></test-angular-one>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
test_utils_1.Simulate.click(element.find('p').eq(2)[0]);
expect(baz).toHaveBeenCalledWith(42);
});
// TODO: support children
it('should not support children', function () {
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: function (value) { return value + 1; },
foo: 1
});
var element = angular_1.element("<test-angular-one foo=\"foo\" bar=\"bar\" baz=\"baz\"><span>Transcluded</span></test-angular-one>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
expect(element.find('span').length).toBe(0);
});
it('should take injections, which override props', function () {
spyOn(ngimport_1.$http, 'get').and.returnValue(ngimport_1.$q.resolve({ data: '$http response' }));
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
foo: 'FOO'
});
var element1 = angular_1.element("<test-angular-six foo=\"foo\"></test-angular-six>");
$compile(element1)(scope);
var element2 = angular_1.element("<test-angular-seven foo=\"foo\"></test-angular-seven>");
$compile(element2)(scope);
ngimport_1.$rootScope.$apply();
expect(ngimport_1.$http.get).toHaveBeenCalledWith('https://example.com/');
expect(element1.find('p').eq(0).text()).toBe('$http response', '$http is injected');
expect(element1.find('p').eq(1).text()).toBe('$element result', '$element is injected');
expect(element1.find('p').eq(2).text()).toBe('testSixService result', 'testSixService is injected');
expect(element1.find('p').eq(3).text()).toBe('CONSTANT FOO', 'injections should override props');
expect(element2.find('p').text()).toBe('CONSTANT FOO', 'injections should override props');
});
});
describe('react stateless components', function () {
it('should render', function () {
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: function (value) { return value + 1; },
foo: 1
});
var element = angular_1.element("<test-angular-two foo=\"foo\" bar=\"bar\" baz=\"baz\"></test-angular-two>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
expect(element.find('p').length).toBe(3);
});
it('should render (even if the component takes no props)', function () {
var scope = ngimport_1.$rootScope.$new(true);
var element = angular_1.element("<test-angular-three></test-angular-three>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
expect(element.text()).toBe('Foo');
});
it('should update', function () {
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: function (value) { return value + 1; },
foo: 1
});
var element = angular_1.element("<test-angular-two foo=\"foo\" bar=\"bar\" baz=\"baz\"></test-angular-two>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
expect(element.find('p').eq(1).text()).toBe('Bar: true,false');
scope.$apply(function () {
return scope.bar = [false, true, true];
});
expect(element.find('p').eq(1).text()).toBe('Bar: false,true,true');
});
// TODO: figure out how to test this
xit('should destroy', function () { });
it('should take callbacks', function () {
var baz = jasmine.createSpy('baz');
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: baz,
foo: 1
});
var element = angular_1.element("<test-angular-two foo=\"foo\" bar=\"bar\" baz=\"baz\"></test-angular-two>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
test_utils_1.Simulate.click(element.find('p').eq(2)[0]);
expect(baz).toHaveBeenCalledWith(42);
});
// TODO: support children
it('should not support children', function () {
var scope = Object.assign(ngimport_1.$rootScope.$new(true), {
bar: [true, false],
baz: function (value) { return value + 1; },
foo: 1
});
var element = angular_1.element("<test-angular-two foo=\"foo\" bar=\"bar\" baz=\"baz\"><span>Transcluded</span></test-angular-two>");
$compile(element)(scope);
ngimport_1.$rootScope.$apply();
expect(element.find('span').length).toBe(0);
});
});
});
//# sourceMappingURL=test.js.map