Setup tests environment

This commit is contained in:
Alejandro Celaya 2018-06-14 19:42:42 +02:00
parent e150b35e33
commit 9ac0811200
8 changed files with 74 additions and 28 deletions

4
config/setupEnzyme.js Normal file
View file

@ -0,0 +1,4 @@
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });

View file

@ -1,7 +1,8 @@
{
"name": "shlink-web-client-react",
"description": "A React-based web client for shlink",
"version": "0.1.0",
"private": true,
"private": false,
"dependencies": {
"@fortawesome/fontawesome": "^1.1.8",
"@fortawesome/fontawesome-free-solid": "^5.0.13",
@ -19,6 +20,8 @@
"css-loader": "0.28.7",
"dotenv": "4.0.0",
"dotenv-expand": "4.2.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "4.10.0",
"eslint-config-react-app": "^2.1.0",
"eslint-loader": "1.9.0",
@ -66,11 +69,11 @@
"src/**/*.{js,jsx,mjs}"
],
"setupFiles": [
"<rootDir>/config/polyfills.js"
"<rootDir>/config/polyfills.js",
"<rootDir>/config/setupEnzyme.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}",
"<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"
"<rootDir>/tests/**/*.test.{js,jsx,mjs}"
],
"testEnvironment": "node",
"testURL": "http://localhost",

View file

@ -15,13 +15,8 @@ process.on('unhandledRejection', err => {
// Ensure environment variables are read.
require('../config/env');
// Make tests to be matched inside tests folder
const jest = require('jest');
let argv = process.argv.slice(2);
// Watch unless on CI or in coverage mode
if (!process.env.CI && argv.indexOf('--coverage') < 0) {
argv.push('--watch');
}
jest.run(argv);

View file

@ -1,9 +0,0 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
ReactDOM.unmountComponentAtNode(div);
});

View file

@ -1,5 +1,9 @@
import { combineReducers } from 'redux';
const rootReducer = combineReducers({});
import serversReducer from './servers';
const rootReducer = combineReducers({
servers: serversReducer
});
export default rootReducer;

15
src/reducers/servers.js Normal file
View file

@ -0,0 +1,15 @@
const FETCH_SERVERS = 'shlink/FETCH_SERVERS';
const CREATE_SERVER = 'shlink/FETCH_SERVERS';
export default function serversReducer(state = [{ name: 'bar' }], action) {
switch (action.type) {
case FETCH_SERVERS:
return action.servers;
case CREATE_SERVER:
return [ ...state, action.server ];
}
return state;
}

View file

@ -2,7 +2,19 @@ import React from 'react';
import { DropdownItem, DropdownMenu, DropdownToggle, UncontrolledDropdown } from 'reactstrap';
import { connect } from 'react-redux';
class ServersDropdown extends React.Component {
export class ServersDropdown extends React.Component {
renderServers = () => {
return this.props.servers.map(server => (
<DropdownItem key={server.name} onClick={() => this.selectServer(server)}>
{server.name}
</DropdownItem>
));
};
selectServer = server => {
// TODO
};
render() {
return (
<UncontrolledDropdown nav>
@ -11,16 +23,15 @@ class ServersDropdown extends React.Component {
</DropdownToggle>
<DropdownMenu>
<DropdownItem>
Server 1 foo
</DropdownItem>
<DropdownItem>
Server 2 foo
</DropdownItem>
{this.renderServers()}
</DropdownMenu>
</UncontrolledDropdown>
);
}
}
export default connect()(ServersDropdown);
const mapStateToProps = state => ({
servers: state.servers
});
export default connect(mapStateToProps)(ServersDropdown);

View file

@ -0,0 +1,23 @@
import React from 'react';
import { ServersDropdown } from '../../src/servers/ServersDropdown';
import { shallow } from 'enzyme';
describe('ServersDropdown', () => {
let wrapped;
const servers = [{ name: 'foo' }, { name: 'bar' }, { name: 'baz' }];
beforeEach(() => {
wrapped = shallow(<ServersDropdown servers={servers} />);
});
afterEach(() => {
wrapped.unmount();
});
it('contains the list of servers', () => {
expect(wrapped.find('DropdownItem').length).toEqual(servers.length);
});
it('contains a toggle with proper title', () => {
expect(wrapped.find('DropdownToggle').length).toEqual(1);
});
});