Run the PDF.js-viewer API unit-test in Node.js environments (PR 16592 follow-up)

It occurred to me that we can actually run this unit-test in Node.js environments by making use of the preprocessor to stub out the browser globals there.
This commit is contained in:
Jonas Jenwald 2023-06-26 09:30:13 +02:00
parent ccb72073b0
commit 5f5db4b160
4 changed files with 15 additions and 5 deletions

View File

@ -30,12 +30,14 @@
- Replaces one `Node.insertBefore()` with `Element.prepend()`. - Replaces one `Node.insertBefore()` with `Element.prepend()`.
- Removes `fireL10nReadyEvent` since the "localized" event it dispatches - Removes `fireL10nReadyEvent` since the "localized" event it dispatches
is unused and may clash with an identically named event in the viewer. is unused and may clash with an identically named event in the viewer.
- Use preprocessor statements for browser globals (removed during building),
to enable more unit-testing in Node.js environments.
*/ */
/*jshint browser: true, devel: true, es5: true, globalstrict: true */ /*jshint browser: true, devel: true, es5: true, globalstrict: true */
'use strict'; 'use strict';
document.webL10n = (function(window, document, undefined) { (typeof PDFJSDev !== "undefined" && PDFJSDev.test("LIB") ? {} : document).webL10n = (function(window, document) {
var gL10nData = {}; var gL10nData = {};
var gTextData = ''; var gTextData = '';
var gTextProp = 'textContent'; var gTextProp = 'textContent';
@ -1026,4 +1028,7 @@ document.webL10n = (function(window, document, undefined) {
} }
} }
}; };
}) (window, document); }) (
typeof PDFJSDev !== "undefined" && PDFJSDev.test("LIB") ? {} : window,
typeof PDFJSDev !== "undefined" && PDFJSDev.test("LIB") ? {} : document
);

View File

@ -1566,7 +1566,12 @@ function buildLib(defines, dir) {
{ base: "src/" } { base: "src/" }
), ),
gulp.src( gulp.src(
["examples/node/domstubs.js", "web/*.js", "!web/{pdfjs,viewer}.js"], [
"examples/node/domstubs.js",
"external/webL10n/l10n.js",
"web/*.js",
"!web/{pdfjs,viewer}.js",
],
{ base: "." } { base: "." }
), ),
gulp.src("test/unit/*.js", { base: "." }), gulp.src("test/unit/*.js", { base: "." }),

View File

@ -36,6 +36,7 @@
"pdf_find_utils_spec.js", "pdf_find_utils_spec.js",
"pdf_history_spec.js", "pdf_history_spec.js",
"pdf_spec.js", "pdf_spec.js",
"pdf_viewer.component_spec.js",
"pdf_viewer_spec.js", "pdf_viewer_spec.js",
"primitives_spec.js", "primitives_spec.js",
"stream_spec.js", "stream_spec.js",

View File

@ -18,8 +18,6 @@
import "../external/webL10n/l10n.js"; import "../external/webL10n/l10n.js";
import { getL10nFallback } from "./l10n_utils.js"; import { getL10nFallback } from "./l10n_utils.js";
const { webL10n } = document;
const PARTIAL_LANG_CODES = { const PARTIAL_LANG_CODES = {
en: "en-US", en: "en-US",
es: "es-ES", es: "es-ES",
@ -47,6 +45,7 @@ function fixupLangCode(langCode) {
*/ */
class GenericL10n { class GenericL10n {
constructor(lang) { constructor(lang) {
const { webL10n } = document;
this._lang = lang; this._lang = lang;
this._ready = new Promise((resolve, reject) => { this._ready = new Promise((resolve, reject) => {
webL10n.setLanguage(fixupLangCode(lang), () => { webL10n.setLanguage(fixupLangCode(lang), () => {