Skip to content

Commit

Permalink
Recheck functionality from - @dkowis (#67)
Browse files Browse the repository at this point in the history
* Only do karma tests on the test task

* Added Re-Checking to the thing

* playing with tests

* fixed remaining entry tests

* bumped version
  • Loading branch information
gambtho authored Jul 12, 2016
1 parent ebd4853 commit 3696fe9
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 83 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apply plugin: 'com.moowork.node'

jar {
baseName = 'willitconnect'
version = '1.0.5'
version = '1.0.6'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down Expand Up @@ -65,4 +65,4 @@ task wrapper(type: Wrapper) {

project.tasks.processResources.dependsOn('npmInstall')
project.tasks.processResources.dependsOn('webpack')
project.tasks.processResources.dependsOn('karma')
project.tasks.test.dependsOn('karma')
2 changes: 1 addition & 1 deletion manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
applications:
- name: willitconnect
memory: 1G
path: build/libs/willitconnect-1.0.5.jar
path: build/libs/willitconnect-1.0.6.jar
buildpack: https://github.com/cloudfoundry/java-buildpack#v3.8

128 changes: 93 additions & 35 deletions src/main/scripts/Entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@ export default class StatefulEntry extends React.Component {
constructor(props) {
super(props);
this.getLastChecked = this.getLastChecked.bind(this);
this.getResultString = this.getResultString.bind(this);
this.getData = this.getData.bind(this);
this.successFunc = this.successFunc.bind(this);
}
this.performCheck = this.performCheck.bind(this);

this.state = {
status: null,
connections: []
}
}
componentDidMount() {
this.performCheck();
}

performCheck() {

//Set the state to pending
this.setState({
status: null
});

var path = '/v2/willitconnect';
console.log(this.props.host);
//console.log(this.props.host);
jQuery.ajax({
url: path,
type: "POST",
Expand All @@ -25,8 +39,9 @@ export default class StatefulEntry extends React.Component {
success: this.successFunc
});
}
getLastChecked() {
var utcSeconds = parseInt(this.state.status.lastChecked);

getLastChecked(lastChecked) {
var utcSeconds = parseInt(lastChecked);
var date = new Date(utcSeconds);
var month = date.getMonth();
var day = date.getDay();
Expand All @@ -37,26 +52,31 @@ export default class StatefulEntry extends React.Component {
var seconds = "0" + date.getSeconds();
return (month + "-" + day + "-" + year + " " + hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2));
}
getResultString() {
var resultString = this.props.host;
if (this.props.port) {
resultString += ":" + this.props.port;
}
if (this.props.proxyHost) {
resultString += " proxied through " + this.props.proxyHost;
}
if (this.props.proxyPort) {
resultString += ":" + this.props.proxyPort;
}
return resultString;
}
successFunc(data) {
mixpanel.track("connection attempted", {
"canConnect": data.canConnect,
"httpStatus": data.httpStatus, "validHostName": data.validHostName,
"httpStatus": data.httpStatus,
"validHostName": data.validHostName,
"validUrl": data.validUrl
});
this.setState({status: data});
//Build a connections attempt object to store history
const history = {
guid: this.state.connections.length,
success: data.canConnect,
httpStatus: data.httpStatus,
time: data.lastChecked
};

//console.log("data", data);

const newConnections = this.state.connections.concat([history]);

this.setState({
status: data,
connections: newConnections
});

//console.log("state", this.state);
}
getData() {
//console.log({"target": this.props.host + ":" + this.props.port});
Expand All @@ -73,24 +93,35 @@ export default class StatefulEntry extends React.Component {

const pending = (this.state == null || this.state.status == null);
const success = !pending && this.state.status.canConnect;
if(!pending) {
//console.table(this.state.status)
}

return (<Result header={ this.getResultString() } pending={ pending } success={ success }>
{ !pending &&
//Build a list of all the historical connection attempts
//console.table(this.state.connections);
const attempts = this.state.connections.map(attempt => {
//console.log("creating attempt", attempt);
return (
<Entry
success={ success }
httpStatus={ this.state.status.httpStatus }
time={ this.getLastChecked() }
key={attempt.guid}
success={attempt.success}
httpStatus={attempt.httpStatus}
time={this.getLastChecked(attempt.time)}
/>
}
);
}).reverse();

const subProps = {
host: this.props.host,
port: this.props.port,
proxyHost: this.props.proxyHost,
proxyPort: this.props.proxyPort,
recheck: this.performCheck
};

return (<Result header={ ResultHeader(subProps) } pending={ pending } success={ success }>
<div>{attempts}</div>
</Result>);
}
}



function getPanelStyle(pending, success) {
if (success) {
return "success";
Expand All @@ -101,8 +132,35 @@ function getPanelStyle(pending, success) {
return "danger";
}

export const ResultHeader = (props) => {
var resultString = props.host;
if (props.port) {
resultString += ":" + props.port;
}
if (props.proxyHost) {
resultString += " proxied through " + props.proxyHost;
}
if (props.proxyPort) {
resultString += ":" + props.proxyPort;
}

const rightStyle = {
float:"right"
};

return (
<div>
{resultString}
<span style={rightStyle}>
<button onClick={props.recheck}>Re-check</button>
</span>
</div>
);
};


export const Result = ({success, pending, children, ...props}) =>
<Panel collapsible bsStyle={ getPanelStyle( pending, success ) } defaultExpanded { ...props }>
<Panel bsStyle={ getPanelStyle( pending, success ) } { ...props }>
{ pending && <ProgressBar active now={100}/> }
{ children }
</Panel>;
Expand All @@ -113,8 +171,8 @@ export const Entry = ({
time,
}) =>
<ul>
<li>I { success ? 'can' : 'cannot' } connect</li>
{ httpStatus != 0 && <li>Http Status: { httpStatus } </li> }
<li>Time checked: { time }</li>
<li>On {time}, I { success ? 'could' : 'could not' } connect.&nbsp;
{ httpStatus != 0 && <span>Http Status: { httpStatus }</span> }
</li>
</ul>;

87 changes: 42 additions & 45 deletions src/main/scripts/test/Entry.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,59 @@
'use strict';

import React from 'react';
import StatefulEntry from '../Entry';
import { shallow, mount} from 'enzyme';
import {shallow, mount} from 'enzyme';

describe('Entry', () => {

//var server;

// var mixpanel = {
// track: function() {}
// };
//
// beforeEach(function() {
// //server = sinon.fakeServer.create();
// window.mixpanel = mixpanel;
// });

it("displays the entry", function() {

it("displays the entry", function () {
const entry = shallow(<StatefulEntry />);
//console.log(entry.debug());
expect(entry.is('Result')).to.equal(true);
});

it("displays a host and port with successful connection", function() {
it("displays a host and port with successful connection", function () {
const props = {
host: "google.com",
port: "80"
};

const stateEntry = mount(<StatefulEntry {...props} />);
stateEntry.setState({status: {"canConnect": true, "lastChecked": 1460727927955} });

const result = stateEntry.find('Result');
expect(result.prop('header')).to.equal("google.com:80");
expect(result.prop('pending')).to.equal(false);
expect(result.prop('success')).to.equal(true);
const entry = stateEntry.find('Entry');
expect(entry.prop('success')).to.equal(true);
expect(entry.prop('httpStatus')).to.equal(undefined);
expect(entry.prop('time')).to.match(/3-5-2016/);
stateEntry.setState({status: {"canConnect": true, "lastChecked": 1460727927955}});
stateEntry.setState({connections: [{"entry": 0, "success": true, "time": 1460727927955}]});

expect(stateEntry.find('Result').prop('pending')).to.equal(false);
expect(stateEntry.find('Result').prop('success')).to.equal(true);


expect(stateEntry.find('div').at(2).prop('children')[0]).to.equal("google.com:80");
expect(stateEntry.find('Entry').first().prop('success')).to.equal(true);
expect(stateEntry.find('Entry').first().prop('httpStatus')).to.equal(undefined);
expect(stateEntry.find('Entry').first().prop('time')).to.match(/3-5-2016/);
});

it("displays a host and port with unsuccessful connection", function() {
it("displays a host and port with unsuccessful connection", function () {
const props = {
host: "test.com",
port: "80"
};

const stateEntry = mount(<StatefulEntry {...props} />);
stateEntry.setState({status: {"canConnect": false, "lastChecked": 1460727927955} });
stateEntry.setState({status: {"canConnect": false, "lastChecked": 1460727927955}});
stateEntry.setState({connections: [{"entry": 0, "success": false, "time": 1460727927955}]});

const result = stateEntry.find('Result');
expect(result.prop('header')).to.equal("test.com:80");
expect(result.prop('pending')).to.equal(false);
expect(result.prop('success')).to.equal(false);


expect(stateEntry.find('div').at(2).prop('children')[0]).to.equal("test.com:80");

const entry = stateEntry.find('Entry');
expect(entry.prop('success')).to.equal(false);
expect(entry.prop('httpStatus')).to.equal(undefined);
expect(entry.prop('time')).to.match(/3-5-2016/);
});

it("displays a host and port with successful proxy connection", function() {
it("displays a host and port with successful proxy connection", function () {
const props = {
host: "test.com",
port: "80",
Expand All @@ -72,20 +62,22 @@ describe('Entry', () => {
};

const stateEntry = mount(<StatefulEntry {...props} />);
stateEntry.setState({status: {"canConnect": true, "lastChecked": 1460727927955} });
stateEntry.setState({status: {"canConnect": true, "lastChecked": 1460727927955}});
stateEntry.setState({connections: [{"entry": 0, "success": true, "time": 1460727927955}]});

const result = stateEntry.find('Result');
expect(result.prop('header')).to.equal('test.com:80 proxied through testProxy.com:8080');
expect(result.prop('pending')).to.equal(false);
expect(result.prop('success')).to.equal(true);

expect(stateEntry.find('div').at(2).prop('children')[0]).to.equal("test.com:80 proxied through testProxy.com:8080");

const entry = stateEntry.find('Entry');
expect(entry.prop('success')).to.equal(true);
expect(entry.prop('httpStatus')).to.equal(undefined);
expect(entry.prop('time')).to.match(/3-5-2016/);
});

it("displays a host and port with unsuccessful proxy connection", function() {
it("displays a host and port with unsuccessful proxy connection", function () {
const props = {
host: "test.com",
port: "80",
Expand All @@ -94,52 +86,57 @@ describe('Entry', () => {
};

const stateEntry = mount(<StatefulEntry {...props} />);
stateEntry.setState({status: {"canConnect": false, "lastChecked": 1460727927955} });
stateEntry.setState({status: {"canConnect": false, "lastChecked": 1460727927955}});
stateEntry.setState({connections: [{"entry": 0, "success": false, "time": 1460727927955}]});

const result = stateEntry.find('Result');
expect(result.prop('header')).to.equal('test.com:80 proxied through testProxy.com:8081');
expect(result.prop('pending')).to.equal(false);
expect(result.prop('success')).to.equal(false);

expect(stateEntry.find('div').at(2).prop('children')[0]).to.equal("test.com:80 proxied through testProxy.com:8081");


const entry = stateEntry.find('Entry');
expect(entry.prop('success')).to.equal(false);
expect(entry.prop('httpStatus')).to.equal(undefined);
expect(entry.prop('time')).to.match(/3-5-2016/);
});

it("displays a host and port without a response", function() {
it("displays a host and port without a response", function () {
const props = {
host: "test.com",
port: "80"
};

const stateEntry = mount(<StatefulEntry {...props} />);

const result = stateEntry.find('Result');
expect(result.prop('header')).to.equal('test.com:80');
expect(result.prop('pending')).to.equal(true);
expect(result.prop('success')).to.equal(false);

expect(stateEntry.contains('Entry')).to.equal(false);
});

it("displays a host and port with a status code", function() {
it("displays a host and port with a status code", function () {
const props = {
host: "test.com",
port: "80"
};

const stateEntry = mount(<StatefulEntry {...props} />);
stateEntry.setState({status: {"canConnect": true, "httpStatus": "200", "lastChecked": 1360727327500} });
stateEntry.setState({status: {"canConnect": true, "httpStatus": "200", "lastChecked": 1360727327500}});
stateEntry.setState({connections: [{"entry": 0, "success": true, "httpStatus": 200, "time": 1360727327500}]});


const result = stateEntry.find('Result');
expect(result.prop('header')).to.equal('test.com:80');
expect(result.prop('pending')).to.equal(false);
expect(result.prop('success')).to.equal(true);

expect(stateEntry.find('div').at(2).prop('children')[0]).to.equal("test.com:80");

const entry = stateEntry.find('Entry');
expect(entry.prop('success')).to.equal(true);
expect(entry.prop('httpStatus')).to.equal('200');
expect(entry.prop('httpStatus')).to.equal(200);
expect(entry.prop('time')).to.match(/1-/);
});
});

0 comments on commit 3696fe9

Please sign in to comment.