Skip to content

Commit f9238ef

Browse files
committed
make createP5Iframe available for all test suites
1 parent c7d6e62 commit f9238ef

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

test/.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"sinon": false,
88
"expect": false,
99
"promisedSketch": true,
10-
"testSketchWithPromise": true
10+
"testSketchWithPromise": true,
11+
"createP5Iframe": true,
12+
"P5_SCRIPT_URL": true,
13+
"P5_SCRIPT_TAG": true
1114
}
1215
}

test/js/p5_helpers.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function promisedSketch(sketch_fn) {
1010
myInstance.remove();
1111
});
1212
return promise;
13-
};
13+
}
1414

1515
function testSketchWithPromise(name, sketch_fn) {
1616
var test_fn = function() {
@@ -20,4 +20,27 @@ function testSketchWithPromise(name, sketch_fn) {
2020
return sketch_fn.toString();
2121
};
2222
return test(name, test_fn);
23-
};
23+
}
24+
25+
var P5_SCRIPT_URL = '../../lib/p5.js';
26+
var P5_SCRIPT_TAG = '<script src="' + P5_SCRIPT_URL + '"></script>';
27+
28+
function createP5Iframe(html) {
29+
html = html || P5_SCRIPT_TAG;
30+
31+
var elt = document.createElement('iframe');
32+
33+
document.body.appendChild(elt);
34+
elt.setAttribute('style', 'visibility: hidden');
35+
36+
elt.contentDocument.open();
37+
elt.contentDocument.write(html);
38+
elt.contentDocument.close();
39+
40+
return {
41+
elt: elt,
42+
teardown: function() {
43+
elt.parentNode.removeChild(elt);
44+
}
45+
};
46+
}

test/unit/core/core.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,51 +127,36 @@ suite('Core', function() {
127127
});
128128

129129
suite('new p5() / global mode', function() {
130-
var P5_SCRIPT_URL = '../../lib/p5.js';
131-
var P5_SCRIPT_TAG = '<script src="' + P5_SCRIPT_URL + '"></script>';
132130
var iframe;
133131

134-
function createP5Iframe(html) {
135-
html = html || P5_SCRIPT_TAG;
136-
137-
iframe = document.createElement('iframe');
138-
139-
document.body.appendChild(iframe);
140-
iframe.setAttribute('style', 'visibility: hidden');
141-
142-
iframe.contentDocument.open();
143-
iframe.contentDocument.write(html);
144-
iframe.contentDocument.close();
145-
}
146-
147132
teardown(function() {
148133
if (iframe) {
149-
iframe.parentNode.removeChild(iframe);
134+
iframe.teardown();
150135
iframe = null;
151136
}
152137
});
153138

154139
test('is triggered when "setup" is in window', function() {
155140
return new Promise(function(resolve, reject) {
156-
createP5Iframe();
157-
iframe.contentWindow.setup = function() {
141+
iframe = createP5Iframe();
142+
iframe.elt.contentWindow.setup = function() {
158143
resolve();
159144
};
160145
});
161146
});
162147

163148
test('is triggered when "draw" is in window', function() {
164149
return new Promise(function(resolve, reject) {
165-
createP5Iframe();
166-
iframe.contentWindow.draw = function() {
150+
iframe = createP5Iframe();
151+
iframe.elt.contentWindow.draw = function() {
167152
resolve();
168153
};
169154
});
170155
});
171156

172157
test('works when p5.js is loaded asynchronously', function() {
173158
return new Promise(function(resolve, reject) {
174-
createP5Iframe(`
159+
iframe = createP5Iframe(`
175160
<script>
176161
window.onload = function() {
177162
var script = document.createElement('script');
@@ -181,13 +166,13 @@ suite('Core', function() {
181166
}
182167
</script>`);
183168

184-
iframe.contentWindow.setup = resolve;
169+
iframe.elt.contentWindow.setup = resolve;
185170
});
186171
});
187172

188173
test('works on-demand', function() {
189174
return new Promise(function(resolve, reject) {
190-
createP5Iframe(
175+
iframe = createP5Iframe(
191176
[
192177
P5_SCRIPT_TAG,
193178
'<script>',
@@ -199,9 +184,9 @@ suite('Core', function() {
199184
'</script>'
200185
].join('\n')
201186
);
202-
iframe.contentWindow.onDoneLoading = resolve;
187+
iframe.elt.contentWindow.onDoneLoading = resolve;
203188
}).then(function() {
204-
var win = iframe.contentWindow;
189+
var win = iframe.elt.contentWindow;
205190
assert.equal(typeof win.myURL, 'string');
206191
assert.strictEqual(win.setupCalled, true);
207192
assert.strictEqual(win.originalP5Instance, win.p5.instance);

0 commit comments

Comments
 (0)