-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
174 lines (139 loc) · 4.1 KB
/
index.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
var util = require('util');
var gutil = require('gulp-util');
var async = require('async');
var EventEmitter = require('events');
var _ = require('underscore');
// `GulpProtractorQA` sub-modules dependecies
var storeFileContent = require('./lib/store-file-content');
var findSelectors = require('./lib/find-selectors');
var findViewMatches = require('./lib/find-view-matches');
var consoleOutput = require('./lib/console-output');
var watchFilesChange = require('./lib/watch-files-change');
// Constant
var PLUGIN_NAME = 'gulp-protractor-qa';
// Define `GulpProtractorQA` class.
// @class
function GulpProtractorQA() {
this.testFiles = [];
this.viewFiles = [];
this.selectors = [];
this.watchIsRunning = 0;
this.isFirstLoad = 1;
}
// Inherit EventEmitter.
util.inherits(GulpProtractorQA, EventEmitter);
// Init application.
//
// @param {Object} options
// @param {string} options.testSrc - Glob pattern string point to test files
// @param {string} options.viewSrc - Glob pattern string point to view files
// @param {boolean} options.runOnce - Disable watch feature when `true`
GulpProtractorQA.prototype.init = function(options) {
this.options = options || {};
if (!this.options.testSrc) {
throw new gutil.PluginError(PLUGIN_NAME, '`testSrc` required');
}
if (!this.options.viewSrc) {
throw new gutil.PluginError(PLUGIN_NAME, '`viewSrc` required!');
}
readFiles.call(this);
}
// Read `testSrc` and `viewSrc` files content.
function readFiles() {
/* jshint validthis:true */
var self = this;
async.waterfall([
function(callback) {
storeFileContent.init(self.options.testSrc, callback);
},
function(data, callback) {
self.testFiles = data;
storeFileContent.init(self.options.viewSrc, callback);
},
function(data, callback) {
self.viewFiles = data;
callback(null, 'success');
}
], function() {
findElementSelectors.call(self);
});
}
// Loop through test files and find protractor
// selectors (e.g.: by.css('[href="/"]')).
function findElementSelectors() {
/* jshint validthis:true */
var self = this;
// reset selectors
this.selectors = [];
this.testFiles.forEach(function(item) {
self.selectors = self.selectors.concat(findSelectors.init(item));
});
checkSelectorViewMatches.call(this);
}
function checkSelectorViewMatches() {
/* jshint validthis:true */
var self = this;
// Set all selectors to not found
this.selectors.forEach(function(item) {
item.found = 0;
});
// Check if selectors are findable
this.viewFiles.forEach(function(item) {
findViewMatches(self.selectors, item.content);
});
outputResult.call(this);
}
function outputResult() {
/* jshint validthis:true */
var notFoundItems = _.filter(this.selectors, function(item) {
return (!item.found && !item.disabled);
});
consoleOutput.printFoundItems(notFoundItems);
// On first run warn watched selectors.
if (this.isFirstLoad) {
warnWatchedSelectors.call(this);
this.isFirstLoad = 0;
}
if (!this.options.runOnce && !this.watchIsRunning) {
startWatchingFiles.call(this);
}
}
function startWatchingFiles() {
/* jshint validthis:true */
this.watchIsRunning = 1;
// Init gaze.
watchFilesChange.call(this);
// Listen to change event
this.on('change', onFileChange.bind(this));
}
function onFileChange(data) {
/* jshint validthis:true */
if (data.fileType === 'test') {
updateSelectors.call(this, data);
}
checkSelectorViewMatches.call(this);
}
function updateSelectors(data) {
/* jshint validthis:true */
var filtered = _.filter(this.selectors, function(selector) {
return (selector.path !== data.path);
});
var updatedTestFile = this.testFiles[data.index];
var newSelectors;
if (updatedTestFile) {
newSelectors = findSelectors.init(updatedTestFile);
this.selectors = filtered.concat(newSelectors);
}
}
function warnWatchedSelectors() {
/* jshint validthis:true */
var total = this.selectors.length;
var filtered = _.filter(this.selectors, function(selector) {
return !selector.disabled;
});
// Output `X` out `total` are being watched.
consoleOutput.watchedLocators(filtered.length, total);
}
// Exporting the plugin main function
module.exports = new GulpProtractorQA();