Skip to content

Commit 6080783

Browse files
author
John Doherty
committed
added more intergration tests
1 parent b2be881 commit 6080783

File tree

3 files changed

+131
-9
lines changed

3 files changed

+131
-9
lines changed

test/data/external.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<<!doctype html>
2+
<html>
3+
<head>
4+
<title>Mock External HTML</title>
5+
</head>
6+
<body>
7+
<main></main>
8+
</body>
9+
</html>>

test/intergration.spec.js

Lines changed: 121 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,137 @@
11
'use strict';
22

3-
var express = require('express'),
4-
request = require('supertest'),
5-
nock = require('nock'),
6-
iframeReplacement = require('../index.js');
3+
var path = require('path'),
4+
express = require('express'), // use an actual instance of express to test
5+
exphbs = require('express-handlebars'), // handlebars view engine
6+
request = require('supertest'), // HTTP assertions made easy via superagent
7+
nock = require('nock'), // HTTP mocking library, allows us to intercept http requests and return canned responses
8+
cheerio = require('cheerio'), // used to convert raw html into jQuery style object to we can use css selectors
9+
iframeReplacement = require('../index.js'), // Our express middleware under test
10+
app = null, // global placeholder for our express instance (reset before each test)
11+
sourceUrl = 'http://intercepted.com'; // fake source url which we'll intercept using nock
712

8-
var app = express(); // create the express app
13+
describe('intergration-tests', function () {
914

10-
app.use(iframeReplacement); // add middleware to test
15+
// before each test, setup an new enviroment
16+
beforeEach(function() {
1117

12-
describe('intergration-tests', function () {
18+
// create new express instance
19+
app = express();
20+
21+
// setup default view engine
22+
app.set('views', path.resolve(__dirname, 'views'));
23+
app.engine('hbs', exphbs());
24+
app.set('view engine', 'hbs');
25+
26+
// add our middleware
27+
app.use(iframeReplacement);
28+
29+
// tell nock to intercept requests for http://intercepted.com and return our external.html content
30+
nock(sourceUrl).get('/').replyWithFile(200, __dirname + '/data/external.html');
31+
});
1332

14-
it('res.merge should exist', function (done) {
33+
it('should have res.merge method', function (done) {
1534

16-
// route with middleware
35+
// rexpress route
1736
app.get('/', function(req, res){
37+
38+
// as our middleware was added in the beforeEach, the req.merge sould exist!
1839
expect(res.merge).toBeDefined();
40+
1941
res.status(200).end();
2042
});
2143

2244
// execute the request
2345
request(app).get('/').expect(200, done);
2446
});
47+
48+
it('should merge view with external html', function (done) {
49+
50+
// express route
51+
app.get('/', function(req, res){
52+
53+
// respond with merge request
54+
res.merge('test-view', {
55+
// source url will be intercepted using nock (above)
56+
sourceUrl: sourceUrl,
57+
// element where we want to inject our view content
58+
sourcePlaceholder: 'main'
59+
});
60+
});
61+
62+
// execute the request
63+
request(app).get('/').expect(200).end(function(err, res) {
64+
65+
// convert response into jquery object for testing
66+
var $ = cheerio.load(res.text);
67+
68+
// check the title was modified
69+
expect($('main h1').text()).toEqual('Hello World');
70+
71+
done();
72+
});
73+
});
74+
75+
it('should inject base tag', function (done) {
76+
77+
// express route
78+
app.get('/', function(req, res){
79+
80+
// respond with merge request
81+
res.merge('test-view', {
82+
// source url will be intercepted using nock (above)
83+
sourceUrl: sourceUrl
84+
});
85+
});
86+
87+
// execute the request
88+
request(app).get('/').expect(200).end(function(err, res) {
89+
90+
// convert response into jquery object for testing
91+
var $ = cheerio.load(res.text);
92+
93+
// check the base tag exists within the header and contains matching url
94+
expect($('head base').attr("href")).toEqual(sourceUrl);
95+
96+
done();
97+
});
98+
});
99+
100+
it('should call transform if present', function (done) {
101+
102+
var modifiedPageTitle = 'Title was modified';
103+
104+
var transformer = function($, model){
105+
// check the title matches that in our external.html file
106+
expect($('title').text()).toEqual('Mock External HTML');
107+
108+
// modify the page title
109+
$('title').text(modifiedPageTitle);
110+
};
111+
112+
// express route
113+
app.get('/', function(req, res){
114+
115+
// respond with merge request
116+
res.merge('test-view', {
117+
// source url will be intercepted using nock (above)
118+
sourceUrl: sourceUrl,
119+
// function we want to be called with the source html as a jquery object
120+
transform: transformer
121+
});
122+
});
123+
124+
// execute the request
125+
request(app).get('/').expect(200).end(function(err, res) {
126+
127+
// convert response into jquery object for testing
128+
var $ = cheerio.load(res.text);
129+
130+
// check the title was modified
131+
expect($('title').text()).toEqual(modifiedPageTitle);
132+
133+
done();
134+
});
135+
});
136+
25137
});

test/views/test-view.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello World</h1>

0 commit comments

Comments
 (0)