var test = require('ava');
var {exec} = require('child_process');var fs = require('fs');var jsdom = require('jsdom');var path = require('path');var walkSync = require('walk-sync');
var EXAMPLE_ROOT = path.normalize(path.join(__dirname, '../../../examples/'));
function runExampleAsync(relativePath) { return new Promise(function (resolve, reject) { runExample(relativePath, resolve); });}
function runExample(relativePath, cb) { var errors = []; jsdom.env({ file: path.join(EXAMPLE_ROOT, relativePath), features: { FetchExternalResources: ['script', 'img', 'css', 'frame', 'iframe', 'link'],
SkipExternalResources: /^http/ }, created: function (error, window) { if (error) { cb([error]); } else { jsdom.getVirtualConsole(window).sendTo(console); window.addEventListener('error', function (evt) { console.error( 'In ' + evt.filename + '\n' + 'at line ' + evt.lineno + ' (relative to start of script), col ' + evt.colno + ':' ); errors.push(evt.error); }); } }, onload: function (window) { if (window.test) window.test(); window.close(); cb(errors); } });}
function rebuildIfModified() { var srcEntries = walkSync.entries(path.join(__dirname, '../src')); var mtimes = srcEntries.map(function (entry) { return entry.mtime; }); mtimes.sort(function (a, b) { return a - b; });
var srcDate = new Date(mtimes.pop()); var bundleDate = fs.statSync(path.join(__dirname, '../dist/ohm.js')).mtime;
if (bundleDate < srcDate) { var p = exec('pnpm build'); p.stdout.on('data', function () { }); p.stderr.pipe(process.stderr); }}
test('math example', function (t) { rebuildIfModified(); return runExampleAsync('math/index.html').then(function (errors) { t.deepEqual(errors, [], 'runs without errors'); });});
test('viz example', function (t) { rebuildIfModified(); return runExampleAsync('viz/index.html').then(function (errors) { t.deepEqual(errors, [], 'runs without errors'); });});
test('csv example', function (t) { t.notThrows(function () { require(path.join(EXAMPLE_ROOT, 'csv', 'index.js')); });});