Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nock testing & dummy json #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions test/dummyResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"status": "ok",
"totalResults": 2,
"articles": [
{
"source": {
"id": "the-guardian-uk",
"name": "The Guardian (UK)"
},
"author": "Graeme Wearden",
"title": "UK construction growth hits 14-month high ahead of interest rate decision - business live",
"description": "All the day’s economic and financial news, as Britain’s central bankers announce whether interest rates are rising to a nine-year high",
"url": "https://www.theguardian.com/business/live/2018/aug/02/bank-of-england-interest-rate-decision-mark-carney-sterling-brexit-business-live",
"urlToImage": "https://i.guim.co.uk/img/media/d97a923a2846feae1223fb3b6d2adacc8804d307/0_199_3500_2100/master/3500.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly9hc3NldHMuZ3VpbS5jby51ay9pbWFnZXMvb3ZlcmxheXMvZmQ2MTg4YTJlMWFhNThmNWM5Y2VkODlkNjQxOWU1OTgvdGctbGl2ZS5wbmc=&s=84215700c7d3423bc91a0c8aa8bb1e08",
"publishedAt": "2018-08-02T10:35:02Z"
},
{
"source": {
"id": "the-guardian-uk",
"name": "The Guardian (UK)"
},
"author": "Steven Morris",
"title": "Chances missed to save Cardiff toddler killed by father, review finds",
"description": "Doctor and social workers failed to spot or record injuries to Elsie Scully-Hicks, says report",
"url": "https://www.theguardian.com/society/2018/aug/02/professionals-missed-chances-to-protect-toddler-killed-by-father-elsie-scully-hicks",
"urlToImage": "https://i.guim.co.uk/img/media/bc4b7a1e4a79275461abf611069766b2f89759ed/0_282_1475_884/master/1475.jpg?w=1200&h=630&q=55&auto=format&usm=12&fit=crop&crop=faces%2Centropy&bm=normal&ba=bottom%2Cleft&blend64=aHR0cHM6Ly9hc3NldHMuZ3VpbS5jby51ay9pbWFnZXMvb3ZlcmxheXMvZDM1ODZhNWVmNTc4MTc1NmQyMWEzYjYzNWU1MTcxNDEvdGctZGVmYXVsdC5wbmc=&s=6fcb7989dd74850d5eb2da19e69d1acb",
"publishedAt": "2018-08-02T10:34:53Z"
}
]
}
204 changes: 144 additions & 60 deletions test/router.test.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,165 @@
const test = require('tape');
const router = require('../src/router');
const supertest = require('supertest');
const test = require('tape');
const router = require('../src/router');
const supertest = require('supertest');
const nock = require('nock');
const path = require('path');
const dummy = require('./dummyResponse.json');

test('Check tape is working', (t) => {
const num = 2;
t.equal(num, 2, 'Should return 2');
t.end();
});

test('Check tape is working', (t)=> {
let num = 2;
t.equal(num, 2, 'Should return 2');
t.end();
});
// HOME ROUTE '/', STATIC ROUTE '/public' & 404

test('Check status code for home route is 200', (t)=>{
test('Check status code for home route is 200', (t) => {
supertest(router)
.get("/")
.expect(200)
.expect('Content-Type', 'text/html')
.end((err, res) =>{
t.error(err);
.get('/')
.expect(200)
.expect('Content-Type', 'text/html')
.end((err, res) => {
t.error(err);
t.end();
});
});
});
});

test('Check status code for static route is 200', (t)=>{
supertest(router)
.get("/public/style.css")
test('Check status code for static route is 200', (t) => {
supertest(router)
.get('/public/style.css')
.expect(200)
.expect('Content-Type', 'text/css')
.end((err, res) =>{
t.error(err);
t.end();
});
});

test('Check 404 error for router', (t)=>{
supertest(router)
.end((err, res) => {
t.error(err);
t.end();
});
});

test('Check 404 error for router', (t) => {
supertest(router)
.get('/giulia')
.expect(404)
.expect('Content-Type', 'text/html')
.end((err, res) =>{
t.error(err);
t.equal(res.text, "Page not found", "404 on router message should be 'Page not found'")
t.end();
});
});

test('Check status code for static route is 200', (t)=>{
supertest(router)
.get("/public/style.css")
.end((err, res) => {
t.error(err);
t.equal(
res.text,
'Page not found',
"404 on router message should be 'Page not found'",
);
t.end();
});
});

test('Check status code for static route is 200', (t) => {
supertest(router)
.get('/public/style.css')
.expect(200)
.expect('Content-Type', 'text/css')
.end((err, res) =>{
t.error(err);
t.end();
});
});

test('Check 404 error for router', (t)=>{
supertest(router)
.end((err, res) => {
t.error(err);
t.end();
});
});

test('Check 404 error for router', (t) => {
supertest(router)
.get('/public/giulia.com')
.expect(500)
.expect('Content-Type', 'text/html')
.end((err, res) =>{
t.error(err);
t.equal(res.text, "Sorry we can't find the static file", "500 on router message should be 'Sorry we can't find the static file'")
t.end();
});
});
.end((err, res) => {
t.error(err);
t.equal(
res.text,
"Sorry we can't find the static file",
"500 on router message should be 'Sorry we can't find the static file'",
);
t.end();
});
});

// LATEST ROUTE '/latest'

test('Check /latest route is working', (t)=>{
supertest(router)
.get("/latest")
test('Check /latest route is working', (t) => {
nock(/https:\/\/newsapi\.org/)
.get(/\/v2\/top-headlines\?/)
.reply(200, dummy);
// .persist(); - add this if you want it to last for more than one interception

supertest(router)
.get('/latest')
.expect(200)
.expect('Content-Type', 'application/json')
.end((err, res) =>{
t.error(err);
t.end();
});
});
.end((err, res) => {
t.error(err);
t.end();
});
});

// SEARCH ROUTE '/search'

test('Check /search route is working with valid query', (t) => {
nock(/https:\/\/newsapi\.org/)
.get(/\/v2\/everything\?/)
.reply(200, dummy);

supertest(router)
.get('/search?q=test')
.expect(200)
.expect('Content-Type', 'application/json')
.end((err, res) => {
t.error(err);
t.end();
});
});

test('Check /search error handling for API response error is working', (t) => {
nock(/https:\/\/newsapi\.org/)
.get(/\/v2\/everything\?/)
.reply(503, 'NewsAPI server down');

supertest(router)
.get('/search?q=test')
.expect(500)
.expect('Content-Type', 'application/json')
.end((err, res) => {
t.error(err);
t.end();
});
});

test('Check /search error handling is working for missing query', (t) => {
supertest(router)
.get('/search')
.expect(400)
.expect('Content-Type', 'application/json')
.end((err, res) => {
t.error(err);
t.equal(
JSON.parse(res.text).error,
'Search call with empty or missing query',
"Response to client on unparsable API response should read 'Search call with empty or missing query'",
);
t.end();
});
});

test('Check /search error handling is working for unparsable API response', (t) => {
nock(/https:\/\/newsapi\.org/)
.get(/\/v2\/everything\?/)
.reply(200, '');

supertest(router)
.get('/search?q=test')
.expect(500)
.expect('Content-Type', 'application/json')
.end((err, res) => {
t.error(err);
console.log('this is res: ', JSON.parse(res.text));
t.equal(
JSON.parse(res.text).error,
'Something went wrong',
"Response to client on unparsable API response should read 'Something went wrong'",
);
t.end();
});
});