, , . Redux react-redux , . , , , .
, key
KeyDisplay
. :
reducers.js
import { combineReducers } from 'redux';
export function key(state, { type, value }) {
switch(type) {
case 'SET_VAL': return value;
default: return state;
}
}
export default combineReducers({ key });
:
KeyDisplay.js
import React from 'react';
import { connect } from 'react-redux';
export function KeyDisplay({ keyProp }) {
return (
<div>The key is {keyProp}</div>
);
}
export default connect((state) => { keyProp: state.key })(KeyDisplay);
unit test key
:
keyReducer.test.js
import test from 'tape';
import { key } from './reducers.js';
test('key reducer', (t) => {
t.plan(1);
const output = key('old', { type: 'SET_VAL', value: 'new' });
t.equal(output, 'new', 'SET_VAL should override old value');
});
, connect
, un connect
ed , , :
KeyDisplay.test.js
import test from 'tape';
import { renderIntoDocument } from 'react-addons-test-utils';
import { KeyDisplay } from './KeyDisplay.js';
test('KeyDisplay', (t) => {
const component = renderIntoDocument(<KeyDisplay keyProp="test" />);
// test component
});