/
build_conditionals_test.js
205 lines (181 loc) · 5.61 KB
/
build_conditionals_test.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var path = require("path");
var fs = require("fs-extra");
var rmdir = require("rimraf");
var assert = require("assert");
var denodeify = require("pdenodeify");
var testHelpers = require("./helpers");
var multiBuild = require("../lib/build/multi");
describe("build app using steal-conditional", function() {
this.timeout(30000);
var find = testHelpers.find;
var open = testHelpers.open;
var prmdir = denodeify(rmdir);
var basePath = path.join(__dirname, "conditionals");
before(function() {
return copyDependencies();
});
it("simple substitution works", function(done) {
var bundles = path.join(basePath, "substitution", "dist", "bundles");
var config = {
config: path.join(basePath, "substitution", "package.json!npm")
};
prmdir(path.join(basePath, "substitution", "dist"))
.then(function() {
return multiBuild(config, { minify: false, quiet: true });
})
.then(function() {
// creates bundles for each possible string substitution
return exists(path.join(bundles, "message", "en.js"));
})
.then(function() {
// creates bundles for each possible string substitution
return exists(path.join(bundles, "message", "es.js"));
})
.then(function() {
var page = path.join(
"test", "conditionals", "substitution", "index.html"
);
open(page, function(browser, close) {
find(browser, "translations", function(translations) {
assert.equal(translations.en, "hello, world!");
assert.equal(translations.es, "hola, mundo!");
close();
}, close);
}, done);
})
.catch(done);
});
it("simple boolean conditional works", function(done) {
this.timeout(20000);
var bundles = path.join(basePath, "boolean", "dist", "bundles");
var config = {
config: path.join(basePath, "boolean", "package.json!npm")
};
prmdir(path.join(basePath, "boolean", "dist"))
.then(function() {
return multiBuild(config, { minify: false, quiet: true });
})
.then(function() {
// each module that might be conditionally loaded once the
// built app is run on the browser gets its own module
return exists(path.join(bundles, "foo.js"));
})
.then(function() {
return exists(path.join(bundles, "bar.js"));
})
.then(function() {
var page = path.join(
"test", "conditionals", "boolean", "index.html"
);
open(page, function(browser, close) {
find(browser, "variable", function(variable) {
assert.equal(variable, "foo");
close();
}, close);
}, done);
})
.catch(done);
});
it("substitution using `~` lookup scheme works", function(done) {
var config = {
config: path.join(basePath, "substitution-tilde", "package.json!npm")
};
var bundles = path.join(
basePath, "substitution-tilde", "dist", "bundles", "conditionals"
);
prmdir(path.join(basePath, "substitution-tilde", "dist"))
.then(function() {
return multiBuild(config, { minify: false, quiet: true });
})
.then(function() {
// should create bundle for `en` variation
return exists(path.join(bundles, "message", "en.js"));
})
.then(function() {
// should create bundle for `es` variation
return exists(path.join(bundles, "message", "es.js"));
})
.then(function() {
var page = path.join(
"test", "conditionals", "substitution-tilde", "index.html"
);
open(page, function(browser, close) {
find(browser, "translations", function(translations) {
assert.equal(translations.es, "hola, mundo!");
assert.ok(!translations.en,
"only the `es` translation should be loaded");
close();
}, close);
}, done);
})
.catch(done);
});
it("substitution and steal plugins (less, stache, etc...)", function() {
var base = path.join(basePath, "substitution-ext");
var bundle = path.join(base, "dist", "bundles", "conditionals");
return prmdir(path.join(base, "dist"))
.then(function() {
return multiBuild({
config: path.join(base, "package.json!npm")
}, {
quiet: true,
minify: false
});
})
.then(function() {
// make sure each variation is detected
return exists(path.join(bundle, "blue.css"));
})
.then(function() {
// make sure each variation is detected
return exists(path.join(bundle, "red.css"));
});
});
it("substitution and subfolders", function() {
var base = path.join(basePath, "substitution-folders");
var bundle = path.join(base, "dist", "bundles", "conditionals");
return prmdir(path.join(base, "dist"))
.then(function() {
return multiBuild({
config: path.join(base, "package.json!npm")
}, {
quiet: true,
minify: false
});
})
.then(function() {
// make sure each variation is detected
return exists(path.join(bundle, "en", "message.js"));
})
.then(function() {
// make sure each variation is detected
return exists(path.join(bundle, "es", "message.js"));
});
});
function copyDependencies() {
var copy = denodeify(fs.copy);
var src = path.join(__dirname, "..", "node_modules");
var folders = [
path.join(basePath, "boolean"),
path.join(basePath, "substitution"),
path.join(basePath, "substitution-ext"),
path.join(basePath, "substitution-tilde"),
path.join(basePath, "substitution-folders")
];
var promises = folders.map(function(dest) {
return copy(
path.join(src, "steal-conditional"),
path.join(dest, "node_modules", "steal-conditional")
);
});
return Promise.all(promises);
}
/**
* Check if the file path exists
* @param {string} path The file path
* @return {Promise} Resolves if the file exists, rejects otherwise.
*/
function exists(path) {
return denodeify(fs.access)(path, fs.F_OK);
}
});