Change tests for new Typescript change

This commit is contained in:
Samer Albahra
2019-05-13 20:54:03 -05:00
parent e94f8bf8ff
commit caf381ab73
9 changed files with 198 additions and 6040 deletions

20
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,20 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--require", "ts-node/register",
"-u", "tdd",
"--timeout", "999999",
"--colors", "--recursive",
"${workspaceFolder}/**/*.spec.ts"
],
"internalConsoleOptions": "openOnSessionStart"
}
]
}

4220
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,7 +4,7 @@
"version": "1.0.2",
"repository": "https://github.com/OpenSprinkler/Weather-Weather",
"scripts": {
"test": "mocha --exit test",
"test": "mocha --exit --require ts-node/register **/*.spec.ts",
"start": "node js/server",
"dev": "node scripts/serve",
"bundle": "npm run compile && zip weather.zip -r js package.json package-lock.json",
@@ -20,20 +20,23 @@
"suncalc": "^1.8.0"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/cors": "^2.8.5",
"@types/cron": "^1.3.0",
"@types/dotenv": "^6.1.1",
"@types/express": "^4.16.1",
"@types/mocha": "^5.2.6",
"@types/moment-timezone": "^0.5.12",
"@types/node": "^10.14.6",
"@types/suncalc": "^1.8.0",
"chai": "^4.2.0",
"hippie": "^0.5.2",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"nock": "^9.6.1",
"mock-express-request": "^0.2.2",
"mock-express-response": "^0.2.2",
"nock": "^10.0.6",
"node-watch": "^0.6.2",
"nodemon": "^1.19.0",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
}
}

View File

@@ -1,8 +1,6 @@
import * as express from "express";
import { CronJob } from "cron";
import * as server from "../server";
const count = { temp: 0, humidity: 0 };
let today: PWSStatus = {},
@@ -45,7 +43,7 @@ export const captureWUStream = function( req: express.Request, res: express.Resp
};
export const useLocalWeather = function(): boolean {
return server.pws !== "none" ? true : false;
return process.env.PWS ? true : false;
};
export const getLocalWeather = function(): LocalWeather {

48
routes/weather.spec.ts Normal file
View File

@@ -0,0 +1,48 @@
import { expect } from 'chai';
import * as nock from 'nock';
import * as MockExpressRequest from 'mock-express-request';
import * as MockExpressResponse from 'mock-express-response';
import { getWateringData } from './weather';
const expected = require( '../test/expected.json' );
const replies = require( '../test/replies.json' );
const location = '01002';
describe('/:method endpoint', () => {
beforeEach(() => {
nock( 'http://api.openweathermap.org' )
.filteringPath( function() { return "/"; } )
.get( "/" )
.reply( 200, replies[location].OWMData );
});
it('Information lookup without weather lookup', async () => {
const expressMocks = createExpressMocks(location);
await getWateringData(expressMocks.request, expressMocks.response);
expect( expressMocks.response._getJSON() ).to.eql( expected.noWeather[location] );
});
});
function createExpressMocks(location: string) {
const request = new MockExpressRequest({
method: 'GET',
url: '/0?loc=' + location,
query: {
loc: location,
format: 'json'
},
params: [ 0 ],
headers: {
'x-forwarded-for': '127.0.0.1'
}
});
return {
request,
response: new MockExpressResponse({
request
})
}
}

View File

@@ -5,8 +5,6 @@ import * as cors from "cors";
import * as weather from "./routes/weather";
import * as local from "./routes/local";
const packageJson = require( "../package.json" );
let host = process.env.HOST || "127.0.0.1",
port = parseInt( process.env.PORT ) || 3000;
@@ -29,7 +27,7 @@ if ( pws === "WU" ) {
}
app.get( "/", function( req, res ) {
res.send( packageJson.description + " v" + packageJson.version );
res.send( process.env.npm_package_description + " v" + process.env.npm_package_version );
} );
// Handle 404 error
@@ -40,9 +38,9 @@ app.use( function( req, res ) {
// Start listening on the service port
app.listen( port, host, function() {
console.log( "%s now listening on %s:%d", packageJson.description, host, port );
console.log( "%s now listening on %s:%d", process.env.npm_package_description, host, port );
if (pws !== "none" ) {
console.log( "%s now listening for local weather stream", packageJson.description );
console.log( "%s now listening for local weather stream", process.env.npm_package_description );
}
} );

View File

@@ -1,95 +0,0 @@
var hippie = require( "hippie" ),
nock = require( "nock" ),
expect = require( "chai" ).expect,
replies = require( "./replies" ),
expected = require( "./expected" ),
server = require( "../server" ).app;
describe( "Weather API", function() {
describe( "/:method endpoint", function() {
it( "Weather Underground Source Test", function( done ) {
for ( var test in expected.WU ) {
if ( expected.WU.hasOwnProperty( test ) ) {
apiTest( {
method: 1,
loc: test,
key: process.env.WU_API_KEY,
expected: expected.WU[test],
callback: function() {
done();
}
} );
}
}
} );
it( "Information lookup without weather lookup", function( done ) {
for ( var test in expected.noWeather ) {
if ( expected.noWeather.hasOwnProperty( test ) ) {
apiTest( {
method: 0,
loc: test,
expected: expected.noWeather[test],
callback: function() {
done();
}
} );
}
}
} );
} );
} );
function apiTest( opt ) {
opt = extend( {}, {
method: 0,
key: "",
format: "json"
}, opt );
var url = "/" + opt.method + "?loc=" + opt.loc + "&key=" + opt.key + "&format=" + opt.format;
setupMocks( opt.loc );
hippie( server )
.json()
.get( url )
.expectStatus( 200 )
.end( function( err, res, body ) {
if ( err ) {
throw err;
}
expect( body ).to.eql( opt.expected );
opt.callback( body );
} );
}
function setupMocks( location ) {
nock.cleanAll();
nock( "http://autocomplete.wunderground.com" )
.filteringPath( function() { return "/"; } )
.get( "/" )
.reply( 200, replies[location].WUautoComplete );
nock( "http://api.wunderground.com" )
.filteringPath( function() { return "/"; } )
.get( "/" )
.reply( 200, replies[location].WUyesterday );
nock( "http://api.openweathermap.org" )
.filteringPath( function() { return "/"; } )
.get( "/" )
.reply( 200, replies[location].OWMData );
}
function extend( target ) {
var sources = [].slice.call( arguments, 1 );
sources.forEach( function( source ) {
for ( var prop in source ) {
target[prop] = source[prop];
}
} );
return target;
}

View File

@@ -1,22 +1,18 @@
{
"WU": {
"01002": {
"scale": 0,
"rd": -1,
"tz": 28,
"sunrise": 387,
"sunset": 1060,
"eip": null
}
},
"noWeather": {
"01002": {
"scale": -1,
"rd": -1,
"tz": 28,
"sunrise": 387,
"sunset": 1060,
"eip": null
"tz": 32,
"sunrise": 332,
"sunset": 1203,
"eip": 2130706433,
"rawData": {
"h": 98.5,
"p": 1.09,
"t": 70.8,
"raining": 1
}
}
}
}

File diff suppressed because it is too large Load Diff