Skip to content

Commit

Permalink
refactor home to controller and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Aug 19, 2018
1 parent 94b6a7b commit 0d3700d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 15 deletions.
27 changes: 27 additions & 0 deletions controllers/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

const express = require("express");


function home(req, res) {
let breach = null;
if (req.query.breach) {
const reqBreachName = req.query.breach.toLowerCase();
breach = req.app.locals.breaches.filter(breach => breach.Name.toLowerCase() === reqBreachName)[0];
}
res.render("monitor", {
title: "Firefox Monitor",
breach: breach,
});
}


function notFound(req, res) {
res.status(404)
res.render("404");
}

module.exports = {
home,
notFound,
}
20 changes: 5 additions & 15 deletions routes/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@

const express = require("express");

const router = express.Router();
const {home, notFound} = require("../controllers/home");


router.get("/", async (req, res) => {
let breach = null;
if (req.query.breach) {
const reqBreachName = req.query.breach.toLowerCase();
breach = req.app.locals.breaches.filter(breach => breach.Name.toLowerCase() === reqBreachName)[0];
}
res.render("monitor", {
title: "Firefox Monitor",
breach: breach,
});
});
const router = express.Router();

router.use(async (req, res) => {
res.status(404).render("404");
});
router.get("/", home);
router.use(notFound);

module.exports = router;
54 changes: 54 additions & 0 deletions tests/home.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict";

const httpMocks = require("node-mocks-http");

const home = require("../controllers/home");


function addBreachesToMockRequest(mockRequest) {
const mockBreaches = [
{Name: 'Test'},
{Name: 'DontShow'},
];
mockRequest.app = { locals: { breaches: mockBreaches } };
return mockRequest;
}

test("home GET without breach renders monitor without breach", () => {
let mockRequest = { query: { breach: null } };
mockRequest = addBreachesToMockRequest(mockRequest);
const mockResponse = { render: jest.fn() };

home.home(mockRequest, mockResponse);

const mockRenderCallArgs = mockResponse.render.mock.calls[0];
expect(mockRenderCallArgs[0]).toBe("monitor");
expect(mockRenderCallArgs[1].breach).toBe(null);
});


test("home GET with breach renders monitor with breach", () => {
const testBreach = {Name: 'Test'};
let mockRequest = { query: { breach: testBreach.Name } };
mockRequest = addBreachesToMockRequest(mockRequest);
const mockResponse = { render: jest.fn() };

home.home(mockRequest, mockResponse);

const mockRenderCallArgs = mockResponse.render.mock.calls[0];
expect(mockRenderCallArgs[0]).toBe("monitor");
expect(mockRenderCallArgs[1].breach).toEqual(testBreach);
});


test("notFound set status 404 and renders 404", () => {
const mockRequest = {};
const mockResponse = { status: jest.fn(), render: jest.fn() };

home.notFound(mockRequest, mockResponse);

const mockStatusCallArgs = mockResponse.status.mock.calls[0];
const mockRenderCallArgs = mockResponse.render.mock.calls[0];
expect(mockStatusCallArgs[0]).toBe(404);
expect(mockRenderCallArgs[0]).toBe("404");
});

0 comments on commit 0d3700d

Please sign in to comment.