This repository was archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathutil.js
More file actions
154 lines (135 loc) · 3.76 KB
/
util.js
File metadata and controls
154 lines (135 loc) · 3.76 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const Browser = require('lib/browser/new-browser');
const Promise = require('bluebird');
const State = require('lib/state');
const Suite = require('lib/suite');
const _ = require('lodash');
function makeBrowser(capabilities, config) {
config = _.merge({}, {
id: 'id',
desiredCapabilities: capabilities,
system: {
coverage: {}
}
}, config);
return new Browser(config);
}
function browserWithId(id) {
return new Browser({
id,
desiredCapabilities: {
browserName: id
},
system: {
coverage: {}
}
});
}
function makeSuiteStub(opts) {
opts = _.defaults(opts || {}, {
name: `some-default-name_${new Date().getTime()}`,
url: 'some-default-url',
states: []
});
const suite = Suite.create(opts.name, opts.parent);
suite.beforeActions = ['before', 'actions'];
suite.afterActions = ['after', 'actions'];
if (opts.browsers) {
suite.browsers = opts.browsers;
}
if (opts.path) {
suite.path = opts.path;
}
opts.states.forEach(function(state) {
suite.addState(state);
});
suite.url = opts.url;
suite.file = opts.file;
return suite;
}
function makeStateStub(suite, opts) {
suite = suite || makeSuiteStub();
opts = opts || {};
const state = new State(suite, opts.name || 'default-state-name');
state.shouldSkip = sinon.stub();
suite.states.push(state);
return state;
}
function makeStateResult(opts) {
opts = _.defaults(opts || {}, {
name: 'default-name',
updated: false,
equal: false
});
const suite = makeSuiteStub({
name: 'default-suite-name',
path: ['suite'],
file: 'default/path/file.js'
});
const state = makeStateStub(suite, {fullName: 'full-' + opts.name, name: opts.name});
return _.defaultsDeep(opts, {state, suite});
}
/**
* Makes suite tree
* @param {Object} sceleton, keys must be unique for all levels
* @param {Object} rootOpts options for root suite
* @return {Object} object, where each suite/state can be accessed by its name
*
* Example:
* var tree = makeSuiteTree({
* parent: {
* child: {
* grandchild: ['state1', 'state2']
* }
* }
* }, {browsers: ['b1', 'b2']});
*
* assert.equal(tree.grandchild.parent, tree.child);
* assert.equal(tree.state1.suite, tree.grandchild);
* assert.equal(tree.state1.browsers, ['b1', 'b2']);
*/
function makeSuiteTree(sceleton, rootOpts) {
rootOpts = _.defaults(rootOpts || {}, {
browsers: ['default-browser1', 'default-browser2']
});
const tree = {};
mkTree_(sceleton, makeSuiteStub(rootOpts));
return tree;
function mkTree_(sceleton, parent) {
_.forEach(sceleton, (val, name) => {
if (_.isString(val)) {
pushToTree_(val, makeStateStub(parent, {name: val}));
} else {
const suite = makeSuiteStub({parent: parent, name: name});
parent.addChild(suite);
pushToTree_(name, suite);
mkTree_(val, suite);
}
});
}
function pushToTree_(name, val) {
if (tree[name]) {
throw new Error('Can not make tree, names should be uniq');
}
tree[name] = val;
}
}
function rejectedPromise(error) {
if (!error) {
error = new Error('Rejected');
} else if (typeof error === 'string') {
error = new Error(error);
}
const promise = Promise.reject(error);
promise.catch(() => {});
return promise;
}
module.exports = {
makeBrowser,
browserWithId,
makeStateStub,
makeSuiteStub,
makeStateResult,
makeSuiteTree,
rejectedPromise
};