Skip to content

Commit 467a3a3

Browse files
author
Carlos Atencio
committed
Refactoring and completing plugin functionality and tests
1 parent 402d7ff commit 467a3a3

File tree

10 files changed

+143
-51
lines changed

10 files changed

+143
-51
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = function(grunt) {
5050
test: {
5151
src: 'tests/bundle/httpMock.js',
5252
options: {
53-
specs: 'tests/plugin.test.js',
53+
specs: 'tests/*.test.js',
5454
vendor: [
5555
'http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.js'
5656
],

example/app/app.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ angular
4949
city: city
5050
}
5151
});
52+
},
53+
getThroughPlugin: function(){
54+
return $http.get('/users', {
55+
plugin: {}
56+
});
5257
}
5358
};
5459
})
@@ -258,4 +263,14 @@ angular
258263
})
259264
.controller('HttpDefaultsController', function($http){
260265
this.hasHttpDefaults = !!$http.defaults;
266+
})
267+
.controller('PluginsController', function(userService){
268+
var self = this;
269+
270+
self.get = function(){
271+
return userService.getThroughPlugin().then(function(response){
272+
console.log('HERE', response);
273+
self.result = response.data;
274+
});
275+
}
261276
});

example/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
<pre id="http-defaults" ng-show="ctrl.hasHttpDefaults">$http defaults are set</pre>
7272
</div>
7373

74+
<div ng-controller="PluginsController as ctrl">
75+
<button id="plugin-request" ng-click="ctrl.get()">Plugin</button>
76+
<span id="plugin-result">{{ ctrl.result }}</span>
77+
</div>
78+
7479
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
7580
<script src="app/app.js"></script>
7681
</body>

example/protractor-conf.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ var config = {
77
dir: 'mocks',
88
default: ['default']
99
},
10+
httpMockPlugins: {
11+
default: ['protractor-http-mock-sample-plugin']
12+
},
1013
onPrepare: function(){
1114
require('../index').config = {
1215
rootDirectory: __dirname

example/spec/plugin.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var mock = require('../../index'), //substitute for require('protractor-http-mock')
2+
get = require('./get');
3+
4+
describe('requests made', function(){
5+
6+
afterEach(function(){
7+
mock.teardown();
8+
});
9+
10+
beforeEach(function(){
11+
mock([
12+
{
13+
request: {
14+
path: '/users',
15+
method: 'GET',
16+
plugin: {
17+
check: true
18+
}
19+
},
20+
response: {
21+
data: 'from sample plugin match'
22+
}
23+
},
24+
{
25+
request: {
26+
path: '/users',
27+
method: 'GET',
28+
plugin: {
29+
check: false
30+
}
31+
},
32+
response: {
33+
data: 'my plugin should not match here!'
34+
}
35+
}
36+
]);
37+
38+
get();
39+
});
40+
41+
it('can match through plugin', function(){
42+
element(by.id('plugin-request')).click();
43+
expect(element(by.id('plugin-result')).getText()).toBe('from sample plugin match');
44+
});
45+
});

lib/defaultConfig.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
module.exports = {
2-
moduleConfig: {
3-
rootDirectory: process.cwd(),
4-
protractorConfig: 'protractor-conf.js'
5-
},
6-
mocksConfig: {
2+
rootDirectory: process.cwd(),
3+
protractorConfig: 'protractor-conf.js',
4+
5+
mocks: {
76
dir: 'mocks',
87
default: []
8+
},
9+
10+
plugins: {
11+
default: []
912
}
1013
};

lib/initData.js

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,50 @@ var httpMock = require('./httpMock'),
44
path = require('path'),
55
defaultConfig = require('./defaultConfig');
66

7-
function readDataModule(mockDirectory, mock){
8-
return require(path.join(mockDirectory, mock));
9-
}
10-
11-
function getDefaultKeys(mocksConfig){
12-
return mocksConfig.default && mocksConfig.default.length > 0 ? mocksConfig.default : [];
13-
}
14-
15-
function readModuleConfig(propertyName){
16-
return module.exports.config[propertyName] || defaultConfig.moduleConfig[propertyName];
17-
}
18-
19-
function getModuleConfig(){
20-
var moduleConfig = {};
7+
function getConfig(){
8+
var config = defaultConfig;
219

2210
if(module.exports.config){
23-
moduleConfig.rootDirectory = readModuleConfig('rootDirectory');
24-
moduleConfig.protractorConfig = readModuleConfig('protractorConfig');
25-
}else{
26-
moduleConfig = defaultConfig.moduleConfig;
11+
config.rootDirectory = module.exports.config['rootDirectory'] || config.module.rootDirectory;
2712
}
2813

29-
return moduleConfig;
30-
}
14+
var protractorConfigFile = path.join(config.rootDirectory, module.exports.config['protractorConfig'] || config.protractorConfig);
15+
var protractorConfig = require(protractorConfigFile).config;
3116

32-
function getMocksConfig(moduleConfig){
33-
var protractorConfigFile = path.join(moduleConfig.rootDirectory, moduleConfig.protractorConfig);
34-
var mocksConfig = require(protractorConfigFile).config.mocks;
17+
config.mocks = protractorConfig.mocks || defaultConfig.mocks;
18+
// TODO: add validation check
19+
config.mocks.default = config.mocks.default || [];
3520

36-
if(!mocksConfig){
37-
mocksConfig = defaultConfig.mocksConfig;
38-
}
21+
config.plugins = protractorConfig.httpMockPlugins || defaultConfig.plugins;
22+
// TODO: add validation check
23+
config.plugins.default = config.plugins.default || [];
3924

40-
return mocksConfig;
25+
return config;
4126
}
4227

43-
function getProtractorInstance(){
44-
return protractor.getInstance ? protractor.getInstance() : browser;
28+
function readMockFile(mockDirectory, mock){
29+
return require(path.join(mockDirectory, mock));
4530
}
4631

47-
module.exports = function(mocks, skipDefaults){
32+
function getDefaultKeys(mocksConfig){
33+
return mocksConfig.default && mocksConfig.default.length > 0 ? mocksConfig.default : [];
34+
}
35+
36+
function buildMocks(mocks, skipDefaults){
4837
var data = [],
49-
dataModule,
50-
moduleConfig = getModuleConfig(),
51-
ptor = getProtractorInstance(),
52-
mocksConfig = getMocksConfig(moduleConfig),
53-
mockDirectory = path.join(moduleConfig.rootDirectory, mocksConfig.dir);
38+
config = getConfig(),
39+
mockDirectory = path.join(config.rootDirectory, config.mocks.dir);
5440

5541
mocks = mocks || [];
5642

5743
if(!skipDefaults){
58-
mocks = getDefaultKeys(mocksConfig).concat(mocks);
44+
mocks = config.mocks.default.concat(mocks);
5945
}
6046

6147
for(var i = 0; i < mocks.length; i++){
62-
dataModule = typeof mocks[i] === 'string' ? readDataModule(mockDirectory, mocks[i]) : mocks[i];
48+
// TODO: add validation check
49+
var dataModule = typeof mocks[i] === 'string' ? readMockFile(mockDirectory, mocks[i]) : mocks[i];
50+
6351
if(Array.isArray(dataModule)){
6452
data = data.concat(dataModule);
6553
}else{
@@ -68,7 +56,38 @@ module.exports = function(mocks, skipDefaults){
6856

6957
}
7058

71-
ptor.addMockModule('httpMock', httpMock(data));
59+
return data;
60+
}
61+
62+
function buildPlugins(plugins, skipDefaults){
63+
var data = [],
64+
config = getConfig();
65+
66+
plugins = plugins || [];
67+
68+
if(!skipDefaults){
69+
plugins = config.plugins.default.concat(plugins);
70+
}
71+
72+
for(var i = 0; i < plugins.length; i++){
73+
// TODO: add validation check
74+
var plugin = typeof plugins[i] === 'string' ? require(plugins[i]) : plugins[i];
75+
data.push(plugin);
76+
}
77+
78+
return data;
79+
}
80+
81+
function getProtractorInstance(){
82+
return protractor.getInstance ? protractor.getInstance() : browser;
83+
}
84+
85+
module.exports = function(mocks, plugins, skipDefaults){
86+
var mocks = buildMocks(mocks, skipDefaults),
87+
plugins = buildPlugins(plugins);
88+
89+
var ptor = getProtractorInstance();
90+
ptor.addMockModule('httpMock', httpMock(mocks, plugins));
7291
};
7392

7493
module.exports.teardown = function(){
@@ -106,4 +125,4 @@ module.exports.remove = function(mocks){
106125
var callback = arguments[arguments.length - 1]
107126
callback(JSON.stringify(true));
108127
}, mocks);
109-
};
128+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"grunt-contrib-jshint": "~0.10.0",
1919
"grunt-jasmine-node": "~0.2.1",
2020
"grunt-protractor-runner": "^1.1.4",
21-
"phantomjs-prebuilt": "2.1.3"
21+
"phantomjs-prebuilt": "2.1.3",
22+
"protractor-http-mock-sample-plugin": "0.0.1"
2223
},
2324
"repository": {
2425
"type": "git",

tests/plugin.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ describe('plugins', function(){
88
it('plugins can match', function(done){
99
http({
1010
method: 'GET',
11-
url: '/plugin'
11+
url: '/plugin',
12+
plugin: {}
1213
}).success(function(data, status){
1314
expect(data).toBe('plugin match works!');
1415
expect(status).toBe(200);

tests/setup.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
request: {
248248
path: '/plugin',
249249
method: 'get',
250-
form: {
250+
plugin: {
251251
check: true
252252
}
253253
},
@@ -259,11 +259,11 @@
259259

260260
var plugins = [
261261
{
262-
match: function(expectationRequest, config){
262+
match: function(mockRequest, requestConfig){
263263
var match = true;
264264

265-
if(config.url.indexOf('plugin') > 0 && expectationRequest.path == '/plugin'){
266-
return expectationRequest.form.check;
265+
if(requestConfig.plugin && mockRequest.plugin){
266+
return mockRequest.plugin.check;
267267
}
268268

269269
return match;

0 commit comments

Comments
 (0)