/*
IMPORTANT - Unit Tests defined here for [jsxLoader.js] may not cover all logic and code.
Until every line of code in [jsxLoader.js] is verified here all React demos must work when
testing locally without any code changes to the JSX code in the related files. It's likely
that if the Unit Tests pass then all demos will work but to be on the safe side they should
also be tested.
Run `npm start` at the root directory to start the server then filter for "react" and
"preact" demos. Additionally each page would have to be changed to use the
local version instead of the server version.
For most updates simply doing regression testing after publishing a new release
and updating the CDN version if HTML files is ok. If this Unit Tests pass all
demos and related pages are expected to test ok.
If major changes to the compiler occur also regression test all pages that use
jsxLoader from the following link after the update:
https://awesome-web-react.js.org/
*/
/* global jsxLoader, chai, describe, it, I18n */
var expect = chai.expect;
// Babel script count is based on DataFormsJS React Components are loaded in [unit-testing-react.htm]
var componentScripts = document.querySelectorAll('script[type="text/babel"][src*="src/react/es6"]').length;
var expectedScriptCount = {
addedToPage: 5 + componentScripts,
compiler: 4 + componentScripts,
compilerAndSrc: 1 + componentScripts,
};
// Get starting value because by the time Tests run this will be updated
var startingIsSupported = jsxLoader.isSupportedBrowser;
describe('jsxLoader.js', function() {
describe('API', function() {
it('should exists as window.jsxLoader', function() {
expect(window.jsxLoader).to.be.instanceof(Object);
});
it('should have jsxLoader.version with major version = 5', function() {
expect(jsxLoader.version).to.match(/^5.\d+.\d+$/);
});
it('should have jsxLoader.polyfillUrl', function() {
expect(jsxLoader).to.have.property('polyfillUrl', 'https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?version=4.8.0&features=Array.from,Array.isArray,Array.prototype.find,Array.prototype.findIndex,Object.assign,Object.keys,Object.values,URL,fetch,Promise,Promise.prototype.finally,String.prototype.endsWith,String.prototype.startsWith,String.prototype.includes,String.prototype.repeat,WeakSet,Symbol,Number.isInteger,String.prototype.codePointAt,String.fromCodePoint');
});
it('should have jsxLoader.babelUrl', function() {
expect(jsxLoader).to.have.property('babelUrl', 'https://cdn.jsdelivr.net/npm/@babel/standalone@7.12.12/babel.js');
});
it('should have jsxLoader.babelOptions', function() {
expect(jsxLoader.babelOptions).to.deep.equal({
presets: ['es2015', 'react'],
plugins: ['proposal-object-rest-spread'],
});
});
it('should have jsxLoader.fetchOptions', function() {
expect(jsxLoader.fetchOptions).to.deep.equal({
mode: 'cors',
cache: 'no-store',
credentials: 'same-origin',
});
});
it('should have jsxLoader.logCompileTime', function() {
expect(typeof jsxLoader.logCompileTime).to.equal('boolean');
});
it('should have jsxLoader.logCompileDetails', function() {
expect(typeof jsxLoader.logCompileDetails).to.equal('boolean');
});
it('should have jsxLoader.evalCode', function() {
expect(jsxLoader).to.have.property('evalCode', '"use strict"; class foo {}; const { id, ...other } = { id:123, test:456 };');
});
it('should have jsxLoader.jsUpdates', function() {
var list = [
{ find: /export function/g, replace: 'function' },
{ find: /export default class/g, replace: 'class' },
{ find: /import React from 'react';/g, replace: '' },
{ find: /import React from"react";/g, replace: '' },
{ find: /=>,/g, replace:'=>' },
];
if (window.preact !== undefined) {
list.push({ find: /ReactDOM\.render/g, replace: 'preact.render' });
list.push({ find: /React\.Component/g, replace: 'preact.Component' });
list.push({ find: /React\.Fragment/g, replace: 'preact.Fragment' });
list.push({ find: /React\.createElement/g, replace: 'preact.createElement' });
list.push({ find: /React\.cloneElement/g, replace: 'preact.cloneElement' });
list.push({ find: /React\.createRef/g, replace: 'preact.createRef' });
list.push({ find: /onChange:/g, replace: 'onInput:' });
list.push({ find: /import preact from 'preact';/g, replace: '' });
list.push({ find: /import preact from"preact";/g, replace: '' });
}
expect(jsxLoader.jsUpdates).to.deep.equal(list);
});
it('should have jsxLoader.globalNamespaces', function() {
var namesspaces = {
'react': 'React',
'react-dom': 'ReactDom',
};
if (window.preact !== undefined) {
namesspaces = {
'react': 'preact',
'react-dom': 'preact',
};
}
expect(jsxLoader.globalNamespaces).to.deep.equal(namesspaces);
});
it('should have React set to preact if calling [jsxLoader.usePreact()]', function() {
const usingPreact = (window.preact !== undefined);
const isValid = (usingPreact ? window.React === window.preact : true);
expect(isValid).to.equal(true);
});
it('should have jsxLoader.isSupportedBrowser start with null', function() {
expect(startingIsSupported).to.be.null;
});
it('should have jsxLoader.isSupportedBrowser set to a boolean', function() {
expect(typeof jsxLoader.isSupportedBrowser).to.equal('boolean');
});
it('should have jsxLoader.sourceMaps', function() {
expect(typeof jsxLoader.logCompileDetails).to.equal('boolean');
});
it('should have jsxLoader.needsPolyfill set to a boolean', function() {
expect(typeof jsxLoader.needsPolyfill).to.equal('boolean');
});
it('should have jsxLoader.compiler.maxRecursiveCalls', function() {
expect(jsxLoader.compiler).to.have.property('maxRecursiveCalls', 1000);
});
it('should have jsxLoader.compiler.pragma', function() {
var expected = (window.preact === undefined ? 'React.createElement' : 'preact.createElement');
expect(jsxLoader.compiler).to.have.property('pragma', expected);
});
it('should have jsxLoader.compiler.pragmaFrag', function() {
var expected = (window.preact === undefined ? 'React.Fragment' : 'preact.Fragment');
expect(jsxLoader.compiler).to.have.property('pragmaFrag', expected);
});
it('should have jsxLoader.compiler.addUseStrict', function() {
expect(jsxLoader.compiler).to.have.property('addUseStrict', true);
});
it('should have jsxLoader.hasPendingScripts() === false', function() {
expect(jsxLoader.hasPendingScripts()).to.equal(false);
});
it('should be able to call jsxLoader.setup() twice', function() {
var beforeCount = document.querySelectorAll('script[type="text/babel"]:not([data-added-to-page])').length;
jsxLoader.setup();
var afterCount = document.querySelectorAll('script[type="text/babel"]:not([data-added-to-page])').length;
expect(beforeCount).to.equal(afterCount);
});
it('should be able to load a script after the page loads using jsxLoaded.loadScript(element)', function(done) {
var script = document.querySelector('script[type="text/jsx"]');
jsxLoader.loadScript(script).then(function() {
var html = document.querySelector('.test-content.added-by-test').innerHTML;
expect(html).to.equal('
Hello World
');
done();
});
});
it('should have jsxLoader.addBabelPolyfills()', function() {
expect(jsxLoader.addBabelPolyfills).to.be.instanceof(Function);
});
it('should have jsxLoader.downloadScript()', function() {
expect(jsxLoader.downloadScript).to.be.instanceof(Function);
});
it('should have jsxLoader.addAdditionalPolyfills()', function() {
expect(jsxLoader.addAdditionalPolyfills).to.be.instanceof(Function);
});
it('should have jsxLoader.compiler', function() {
expect(jsxLoader.compiler).to.be.instanceof(Object);
});
it('should have jsxLoader.compiler.compile()', function() {
expect(jsxLoader.compiler.compile).to.be.instanceof(Function);
});
it('should have jsxLoader.compiler.isMinimized()', function() {
// The compiler has other available functions, but in general they
// would not be used by external apps or code.
expect(jsxLoader.compiler.isMinimized).to.be.instanceof(Function);
});
});
// Check content rendered from [test/js/unit-testing.jsx]
describe('Page Loaded and Content', function() {
it('should have content added to the page', function() {
var html = document.getElementById('root').innerHTML;
expect(html).does.not.equal('');
});
// The result number for this and a few tested depending on how is added to the calling page.
// When added as it will be processed
// by the [jsxLoader.js] before being added as JavaScript on the page.
it('should have correct number of babel scripts with [data-added-to-page]', function() {
var scripts = document.querySelectorAll('script[type="text/babel"][data-added-to-page]');
expect(scripts.length).to.equal(expectedScriptCount.addedToPage);
});
it('should have 2 error babel scripts with [data-added-to-page][data-error]', function() {
var scripts = document.querySelectorAll('script[type="text/babel"][data-added-to-page][data-error]');
expect(scripts.length).to.equal(2);
});
it('should have correct number of scripts with [data-compiler]', function() {
var scripts = document.querySelectorAll('script[data-compiler]');
expect(scripts.length).to.equal(expectedScriptCount.compiler);
});
it('should have correct number of scripts with [data-compiler][data-src]', function() {
var scripts = document.querySelectorAll('script[data-compiler][data-src]');
expect(scripts.length).to.equal(expectedScriptCount.compilerAndSrc);
});
it('should have
with `Unit Testing Content`', function() {
var el = document.querySelector('#root h1');
expect(el.textContent).to.equal('Unit Testing Content');
});
it('should code to use strict mode', function() {
var el = document.querySelector('.is-strict-mode');
expect(el.textContent).to.equal('use strict: true');
});
it('should have with `Hello World`', function() {
var el = document.querySelector('div#hello-1');
expect(el.textContent).to.equal('Hello World');
});
it('should have with `Hello Test`', function() {
var el = document.querySelector('div#hello-2');
expect(el.textContent).to.equal('Hello Test');
});
it('should have div-1', function() {
var el = document.querySelector('#div-1');
expect(el.textContent).to.equal(' div-1');
});
it('should have div-2', function() {
var el = document.querySelector('#div-2');
expect(el.textContent).to.equal(' div-2');
});
it('should have div-3', function() {
var el = document.querySelector('#div-3');
expect(el.textContent).to.equal('div-3');
});
it('should have div-4', function() {
var el = document.querySelector('#div-4');
expect(el.textContent).to.equal('Hello World');
});
it('should have div-5', function() {
var el = document.querySelector('#div-5');
expect(el.textContent).to.equal("123 & 456 '");
});
it('should have div-6', function() {
var el = document.querySelector('#div-6');
expect(el.textContent).to.equal(' Hello World ');
});
it('should have div-7 with from escape characters', function() {
var el = document.querySelector('#div-7');
expect(el.textContent).to.equal(' Hello World ');
});
it('should have div-8 with from characters (ASCII == 160)', function() {
var el = document.querySelector('#div-8');
expect(el.textContent).to.equal(' Hello World ');
});
it('should have div-9 with link text', function() {
var el = document.querySelector('#div-9');
expect(el.textContent).to.equal('https://www.dataformsjs.com/');
});
it('should have div-9 attribute', function() {
var el = document.querySelector('#div-9');
expect(el.getAttribute('data-selector')).to.equal('ul.link > li');
});
it('should have div-10', function() {
var el = document.querySelector('#div-10');
expect(el.textContent).to.equal('true');
});
it('should have div-11', function() {
var el = document.querySelector('#div-11');
expect(el.textContent).to.equal('true');
});
it('should have div-12', function() {
var el = document.querySelector('#div-12');
expect(el.textContent).to.equal('{"id":"div-12"}');
});
it('should have div-13', function() {
var el = document.querySelector('#div-13');
expect(el.textContent).to.equal('{"id":"div-13","test":true}');
});
it('should have
with 3 links', function() {
var links = document.querySelectorAll('ul.links li > a[target="_blank"]');
expect(links).to.be.lengthOf(3);
});
it('should have correct content in
for link 1', function() {
var link = document.querySelector('ul.links li:first-child > a');
expect(link.textContent).to.equal('https://www.dataformsjs.com/en/');
});
it('should have correct content in
for link 2', function() {
var link = document.querySelector('ul.links li:nth-child(2) > a');
expect(link.textContent).to.equal('https://www.dataformsjs.com/pt-BR/');
});
it('should have correct content in
for link 3', function() {
var link = document.querySelector('ul.links li:last-child > a');
expect(link.textContent).to.equal('https://www.dataformsjs.com/zh-CN/');
});
it('should have
with 4 elements', function() {
var links = document.querySelectorAll('ul.links2 li');
expect(links).to.be.lengthOf(4);
});
it('should have ', function() {
var input = document.querySelector('input#file-1[type="file"][accept="image/*"][multiple]');
expect(input).to.be.an.instanceof(HTMLInputElement);
});
it('should have