Skip to content

Commit e012ba3

Browse files
authored
Merge pull request #2227 from meiamsome/bugfix/testing
Promisisfy test/unit/core/core.js
2 parents e4a138d + df253e6 commit e012ba3

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

test/unit/core/core.js

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ suite('Core', function(){
127127

128128
suite('new p5() / global mode', function() {
129129
var BIND_TAG = '<script src="../js/bind.js"></script>';
130-
var P5_SCRIPT_URL = (!window.IS_MODULE) ? '../../lib/p5.js' : '../../lib/modules/p5.Core.js';
130+
var P5_SCRIPT_URL = '../../lib/p5.js';
131131
var P5_SCRIPT_TAG = '<script src="' + P5_SCRIPT_URL + '"></script>';
132132
var iframe;
133133

@@ -151,54 +151,61 @@ suite('Core', function(){
151151
}
152152
});
153153

154-
test('is triggered when "setup" is in window', function(done) {
155-
createP5Iframe();
156-
iframe.contentWindow.setup = function() {
157-
done();
158-
};
154+
test('is triggered when "setup" is in window', function() {
155+
return new Promise(function(resolve, reject) {
156+
createP5Iframe();
157+
iframe.contentWindow.setup = function() {
158+
resolve();
159+
};
160+
});
159161
});
160162

161-
test('is triggered when "draw" is in window', function(done) {
162-
createP5Iframe();
163-
iframe.contentWindow.draw = function() {
164-
done();
165-
};
163+
test('is triggered when "draw" is in window', function() {
164+
return new Promise(function(resolve, reject) {
165+
createP5Iframe();
166+
iframe.contentWindow.draw = function() {
167+
resolve();
168+
};
169+
});
166170
});
167171

168-
test('works when p5.js is loaded asynchronously', function(done) {
169-
createP5Iframe(BIND_TAG);
172+
test('works when p5.js is loaded asynchronously', function() {
173+
return new Promise(function(resolve, reject) {
174+
createP5Iframe(BIND_TAG);
170175

171-
iframe.contentWindow.addEventListener('load', function() {
172-
var win = iframe.contentWindow;
176+
iframe.contentWindow.addEventListener('load', function() {
177+
var win = iframe.contentWindow;
173178

174-
win.setup = done;
179+
win.setup = resolve;
175180

176-
var script = win.document.createElement('script');
177-
script.setAttribute('src', P5_SCRIPT_URL);
181+
var script = win.document.createElement('script');
182+
script.setAttribute('src', P5_SCRIPT_URL);
178183

179-
win.document.body.appendChild(script);
180-
}, false);
184+
win.document.body.appendChild(script);
185+
}, false);
186+
});
181187
});
182188

183-
test('works on-demand', function(done) {
184-
createP5Iframe([
185-
BIND_TAG,
186-
P5_SCRIPT_TAG,
187-
'<script>',
188-
'new p5();',
189-
'originalP5Instance = p5.instance',
190-
'myURL = p5.prototype.getURL();',
191-
'function setup() { setupCalled = true; }',
192-
'window.addEventListener("load", onDoneLoading, false);',
193-
'</script>'
194-
].join('\n'));
195-
iframe.contentWindow.onDoneLoading = function() {
189+
test('works on-demand', function() {
190+
return new Promise(function(resolve, reject) {
191+
createP5Iframe([
192+
BIND_TAG,
193+
P5_SCRIPT_TAG,
194+
'<script>',
195+
'new p5();',
196+
'originalP5Instance = p5.instance',
197+
'myURL = p5.prototype.getURL();',
198+
'function setup() { setupCalled = true; }',
199+
'window.addEventListener("load", onDoneLoading, false);',
200+
'</script>'
201+
].join('\n'));
202+
iframe.contentWindow.onDoneLoading = resolve;
203+
}).then(function() {
196204
var win = iframe.contentWindow;
197205
assert.equal(typeof(win.myURL), 'string');
198206
assert.strictEqual(win.setupCalled, true);
199207
assert.strictEqual(win.originalP5Instance, win.p5.instance);
200-
done();
201-
};
208+
});
202209
});
203210
});
204211

0 commit comments

Comments
 (0)