Skip to content

Commit

Permalink
Add a second unit test for RTL text plugin loading:
Browse files Browse the repository at this point in the history
 - Test that loading a style after loading the plugin works
 - Test that pluginBlobURL makes it all the way to the workers
  • Loading branch information
ChrisLoer committed Jun 22, 2017
1 parent 32481e0 commit 3bd1e86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/source/rtl_text_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ module.exports.registerForPluginAvailability = function(
return callback;
};

// Exposed so it can be stubbed out by tests
module.exports.createBlobURL = function(response) {
return window.URL.createObjectURL(new window.Blob([response.data]), {type: "text/javascript"});
};

module.exports.setRTLTextPlugin = function(pluginURL: string, callback: ErrorCallback) {
if (pluginRequested) {
throw new Error('setRTLTextPlugin cannot be called multiple times.');
Expand All @@ -33,9 +38,7 @@ module.exports.setRTLTextPlugin = function(pluginURL: string, callback: ErrorCal
if (err) {
callback(err);
} else {
pluginBlobURL =
window.URL.createObjectURL(new window.Blob([response.data]), {type: "text/javascript"});

pluginBlobURL = module.exports.createBlobURL(response);
module.exports.evented.fire('pluginAvailable', { pluginBlobURL: pluginBlobURL, errorCallback: callback });
}
});
Expand Down
3 changes: 3 additions & 0 deletions src/source/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class Worker {
try {
if (!globalRTLTextPlugin.applyArabicShaping && !globalRTLTextPlugin.processBidirectionalText) {
this.self.importScripts(pluginURL);
if (!globalRTLTextPlugin.applyArabicShaping || !globalRTLTextPlugin.processBidirectionalText) {
callback(`RTL Text Plugin failed to import scripts from ${pluginURL}`);
}
}
} catch (e) {
callback(e);
Expand Down
18 changes: 18 additions & 0 deletions test/unit/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ test('Style', (t) => {
});
});

t.test('loads plugin immediately if already registered', (t) => {
t.stub(rtlTextPlugin, 'createBlobURL').returns("data:text/javascript;base64,abc");
window.useFakeXMLHttpRequest();
window.server.respondWith('/plugin.js', "doesn't matter");
let firstError = true;
rtlTextPlugin.setRTLTextPlugin("/plugin.js", (error) => {
// Getting this error message shows the bogus URL was succesfully passed to the worker
// We'll get the error from all workers, only pay attention to the first one
if (firstError) {
t.deepEquals(error, 'RTL Text Plugin failed to import scripts from data:text/javascript;base64,abc');
t.end();
firstError = false;
}
});
window.server.respond();
new Style(createStyleJSON());
});

t.test('can be constructed from a URL', (t) => {
window.useFakeXMLHttpRequest();
window.server.respondWith('/style.json', JSON.stringify(require('../../fixtures/style')));
Expand Down

0 comments on commit 3bd1e86

Please sign in to comment.