-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcomponent.js
83 lines (73 loc) · 2.64 KB
/
component.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
// Copyright 2014-2015 François de Campredon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
var testUtils = require('./testUtils');
var test = require('tape');
var React = require('react');
var Rx = require('rx');
var assign = require('react/lib/Object.assign');
var RxReactComponent = require('../').Component;
var sinon = require('sinon');
test('Component', function (t) {
function renderComponent(spec, props) {
function Component() {
RxReactComponent.apply(this, arguments);
}
Component.prototype = Object.create(RxReactComponent.prototype);
assign(Component.prototype, spec);
Component.prototype.constructor = Component;
Component.__proto__ = RxReactComponent;
return testUtils.render(React.createElement(Component, props));
}
function isObservable(obj) {
return obj && typeof obj.subscribe === 'function';
}
t.test('extends React Component', function (t) {
var component = renderComponent({
render: function () {
return null;
}
});
t.ok(component instanceof React.Component, 'RxReact Components should extends React.Component');
t.end();
});
t.test('propsStream', function (t) {
var props = { foo: 'bar'};
var props2 = { hello: 'world'};
var spy = sinon.spy();
var component = renderComponent({
render: function () {
return null;
}
}, props);
t.ok(isObservable(component.propsStream), 'RxReact Components should expose propsStream observable');
component.propsStream.subscribe(spy);
testUtils.render(React.createElement(component.constructor, props2));
t.ok(spy.calledWith(props) && spy.calledWith(props2), 'propsStream should be bounds to props');
t.end();
});
t.test('stateStream', function (t) {
var state = { foo: 'bar'};
var component = renderComponent({
render: function () {
return null;
},
getStateStream: function () {
return Rx.Observable.of(state);
}
});
t.deepEquals(component.state, state, 'state should be bounds to the observable returned by `getStateStream` if implemented');
t.end();
});
});