Fix issue where HLJS was not configured when preloaded

Change-Id: Ie8c3d2aaae16ae4361f46372a5ed24ead24fe82e
This commit is contained in:
Thomas Shafer
2017-10-01 04:41:02 -07:00
parent d2becd17f9
commit 1848817498
2 changed files with 24 additions and 11 deletions

View File

@@ -26,6 +26,7 @@
// NOTE: intended singleton.
value: {
configured: false,
loading: false,
callbacks: [],
},
@@ -60,12 +61,13 @@
},
_getHighlightLib() {
return window.hljs;
},
const lib = window.hljs;
if (lib && !this._state.configured) {
this._state.configured = true;
_configureHighlightLib() {
this._getHighlightLib().configure(
{classPrefix: 'gr-diff gr-syntax gr-syntax-'});
lib.configure({classPrefix: 'gr-diff gr-syntax gr-syntax-'});
}
return lib;
},
_getLibRoot() {
@@ -93,10 +95,8 @@
}
script.src = src;
script.onload = function() {
this._configureHighlightLib();
resolve();
}.bind(this);
script.onload = resolve;
script.onerror = reject;
Polymer.dom(document.head).appendChild(script);
});
},

View File

@@ -55,6 +55,7 @@ limitations under the License.
loadStub.restore();
// Because the element state is a singleton, clean it up.
element._state.configured = false;
element._state.loading = false;
element._state.callbacks = [];
});
@@ -88,8 +89,13 @@ limitations under the License.
});
suite('preloaded', () => {
let hljsStub;
setup(() => {
window.hljs = 'test-object';
hljsStub = {
configure: sinon.stub(),
};
window.hljs = hljsStub;
});
teardown(() => {
@@ -101,7 +107,14 @@ limitations under the License.
element.get().then(firstCallHandler);
flush(() => {
assert.isTrue(firstCallHandler.called);
assert.isTrue(firstCallHandler.calledWith('test-object'));
assert.isTrue(firstCallHandler.calledWith(hljsStub));
done();
});
});
test('configures hljs', done => {
element.get().then(() => {
assert.isTrue(window.hljs.configure.calledOnce);
done();
});
});