?????????????
import React from 'react';
import {expect} from 'chai';
import {shallow} from 'enzyme';
import TestComponent from '../components/TestComponent';
describe('Test TestComponent'?? () => {
// ???????????????
const wrapper = shallow(
<TestComponent num={10} />/
);
/*
* ??????????:
* ???wrapper.state()????????state
* ???wrapper.instance()??????????????????????????
* ???wrapper.find()??????????????
* ???????????wrapper.props()????????props
*/
// ??????????????class
it('should render with currect wrapper'?? () => {
expect(wrapper.is('.test_component')).to.equal(true);
});
// ???????????????state
it('should render with currect state'?? () => {
expect(wrapper.state()).to.deep.equal({
clickNum: 10
});
});
// ????????????
it('should add one'?? () => {
wrapper.instance().handleClick();
expect(wrapper.state()).to.deep.equal({
clickNum: 11
});
});
});
????Test Redux
????redux??????????????????mocha???в???
// ????actions
import * as ACTIONS from '../redux/actions';
describe('test actions'?? () => {
it('should return an action to create a todo'?? () => {
let expectedAction = {
type: ACTIONS.NEW_TODO??
todo: 'this is a new todo'
};
expect(ACTIONS.addNewTodo('this is a new todo')).to.deep.equal(expectedAction);
});
});
// ????reducer
import * as REDUCERS from '../redux/reducers';
import * as ACTIONS from '../redux/actions';
describe('todos'?? () => {
let todos = [];
it('should add a new todo'?? () => {
todos.push({
todo: 'new todo'??
complete: false
});
expect(REDUCERS.todos(todos?? {
type: ACTIONS.NEW_TODO??
todo: 'new todo'
})).to.deep.equal([
{
todo: 'new todo'??
complete: false
}
]);
});
});
// ???????store????
import { createStore?? applyMiddleware?? combineReducers } from 'redux';
import thunk from 'redux-thunk';
import chai from 'chai';
import thunkMiddleware from 'redux-thunk';
import * as REDUCERS from '../redux/reducers';
import defaultState from '../redux/ConstValues';
import * as ACTIONS from '../redux/actions'
const appReducers = combineReducers(REDUCERS);
const AppStore = createStore(appReducers?? defaultState?? applyMiddleware(thunk));
let state = Object.assign({}?? AppStore.getState());
// ?????????????state?仯
const subscribeListener = (result?? done) => {
return AppStore.subscribe(() => {
expect(AppStore.getState()).to.deep.equal(result);
done();
});
};
describe('use store in unittest'?? () => {
it('should create a todo'?? (done) => {
// ?????????????????
state.todos.append({
todo: 'new todo'??
complete: false
});
// ???state????
let unsubscribe = subscribeListener(state?? done);
AppStore.dispatch(ACTIONS.addNewTodo('new todo'));
// ??????????????
unsubscribe();
});
});
???????? phantomjs ??selenium??UI UnitTest
????PhantomJS ?????????webkit?????????JavaScript API??????????????????????????webkit??????????????????????????????????????????????DOM???????????е??????????????UI??????????????????????????????????????????????
?????? Selenium ??????Web??ó????д?????????????????????????????????С? Selenium ??????????????????????棬????????????????????? PhantomJS ???????????????????
????· open ??? url
????· ???? onload ???
????· ?????????? sendEvent ???? api ??????? DOM ??????? point
????· ????????
????· ???? UI ??????? ??? setTimeout ??????????????????????????? sendEvent ???????