intergration.spec.js
4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
var path = require('path'),
express = require('express'), // use an actual instance of express to test
exphbs = require('express-handlebars'), // handlebars view engine
request = require('supertest'), // HTTP assertions made easy via superagent
nock = require('nock'), // HTTP mocking library, allows us to intercept http requests and return canned responses
cheerio = require('cheerio'), // used to convert raw html into jQuery style object to we can use css selectors
iframeReplacement = require('../index.js'), // Our express middleware under test
app = null, // global placeholder for our express instance (reset before each test)
sourceUrl = 'http://intercepted.com'; // fake source url which we'll intercept using nock
describe('intergration-tests', function () {
// before each test, setup an new enviroment
beforeEach(function() {
// create new express instance
app = express();
// setup default view engine
app.set('views', path.resolve(__dirname, 'views'));
app.engine('hbs', exphbs());
app.set('view engine', 'hbs');
// add our middleware
app.use(iframeReplacement);
// tell nock to intercept requests for http://intercepted.com and return our external.html content
nock(sourceUrl).get('/').replyWithFile(200, __dirname + '/data/external.html');
});
it('should have res.merge method', function (done) {
// rexpress route
app.get('/', function(req, res){
// as our middleware was added in the beforeEach, the req.merge sould exist!
expect(res.merge).toBeDefined();
res.status(200).end();
});
// execute the request
request(app).get('/').expect(200, done);
});
it('should merge view with external html', function (done) {
// express route
app.get('/', function(req, res){
// respond with merge request
res.merge('test-view', {
// source url will be intercepted using nock (above)
sourceUrl: sourceUrl,
// element where we want to inject our view content
sourcePlaceholder: 'main'
});
});
// execute the request
request(app).get('/').expect(200).end(function(err, res) {
// convert response into jquery object for testing
var $ = cheerio.load(res.text);
// check the title was modified
expect($('main h1').text()).toEqual('Hello World');
done();
});
});
it('should inject base tag', function (done) {
// express route
app.get('/', function(req, res){
// respond with merge request
res.merge('test-view', {
// source url will be intercepted using nock (above)
sourceUrl: sourceUrl
});
});
// execute the request
request(app).get('/').expect(200).end(function(err, res) {
// convert response into jquery object for testing
var $ = cheerio.load(res.text);
// check the base tag exists within the header and contains matching url
expect($('head base').attr("href")).toEqual(sourceUrl);
done();
});
});
it('should call transform if present', function (done) {
var modifiedPageTitle = 'Title was modified';
var transformer = function($, model){
// check the title matches that in our external.html file
expect($('title').text()).toEqual('Mock External HTML');
// modify the page title
$('title').text(modifiedPageTitle);
};
// express route
app.get('/', function(req, res){
// respond with merge request
res.merge('test-view', {
// source url will be intercepted using nock (above)
sourceUrl: sourceUrl,
// function we want to be called with the source html as a jquery object
transform: transformer
});
});
// execute the request
request(app).get('/').expect(200).end(function(err, res) {
// convert response into jquery object for testing
var $ = cheerio.load(res.text);
// check the title was modified
expect($('title').text()).toEqual(modifiedPageTitle);
done();
});
});
});