0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00
liabru-matter-js/test/Browser.spec.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-11-24 18:26:20 -05:00
/* eslint-env es6 */
"use strict";
jest.setTimeout(120 * 1000);
const puppeteer = require('puppeteer-core');
const Example = require('../examples/index');
const fs = require('fs');
2021-04-03 15:36:28 -04:00
const examples = Object.keys(Example);
2020-11-24 18:26:20 -05:00
const browserPath = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome';
const demoPagePath = 'http://localhost:8000/';
const totalUpdates = 5;
if (!fs.existsSync(browserPath)) {
// Skip if browser is missing
console.warn('Could not find browser. Browser tests skipped.');
describe.skip('Browser integration tests');
} else {
describe('Browser integration tests', () => {
let results;
// Set up the environment and run examples
beforeAll(async () => results = await runExamplesBrowser(totalUpdates));
2021-01-16 19:24:04 -05:00
it('all examples run without throwing error or warning', () => {
2020-11-24 18:26:20 -05:00
if (results.error) {
console.error(results.error);
2021-01-16 19:24:04 -05:00
expect(Boolean(results.error)).toBe(false);
}
if (results.warns) {
console.error(results.warns);
expect(results.warns.size).toBe(0);
}
if (!results.error && !results.warns) {
2020-11-24 18:26:20 -05:00
for (const example of examples) {
expect(results[example].id).toBe(example);
expect(results[example].timestamp).toBeGreaterThan(0);
}
}
});
});
}
const runExamplesBrowser = async updates => {
// Set up browser environment
const browser = await puppeteer.launch({ executablePath: browserPath });
const page = await browser.newPage();
const results = {};
2021-01-16 19:24:04 -05:00
let example;
2020-11-24 18:26:20 -05:00
// Load local demo page and catch errors
let pageError;
2021-01-16 19:24:04 -05:00
let pageWarns;
2020-11-24 18:26:20 -05:00
const onPageError = error => pageError = error;
2021-01-16 19:24:04 -05:00
const onPageConsole = async message => {
const type = message.type();
if (example && type === 'error' || type === 'warning') {
const log = `[${example}] ${message.type()} ${message.text()}`;
pageWarns = pageWarns || new Set();
pageWarns.add(log);
}
}
2020-11-24 18:26:20 -05:00
page.addListener('pageerror', onPageError);
2021-01-16 19:24:04 -05:00
page.addListener('console', onPageConsole);
2020-11-24 18:26:20 -05:00
await page.goto(demoPagePath).catch(onPageError);
// For every example
2021-01-16 19:24:04 -05:00
for (example of examples) {
2020-11-24 18:26:20 -05:00
// Bail on error
if (pageError) {
break;
}
// In the demo page context
results[example] = await page.evaluate(async (example, updates) => {
const demo = window.MatterDemoInstance;
2020-11-24 18:26:20 -05:00
// Set the current example
MatterTools.Demo.setExampleById(demo, example);
const instance = demo.example.instance;
2020-11-24 18:26:20 -05:00
let ticks = 0;
// Wait while running
await new Promise((resolve) => {
Matter.Events.on(instance.runner, 'tick', () => {
// Stop after enough updates
if (ticks >= updates) {
Matter.Runner.stop(instance.runner);
resolve();
}
ticks += 1;
});
});
// Return results
return {
id: demo.example.id,
2020-11-24 18:26:20 -05:00
timestamp: instance.engine.timing.timestamp
};
}, example, updates);
}
// Tear down
await browser.close();
2021-01-16 19:24:04 -05:00
results.error = pageError;
results.warns = pageWarns;
return results;
2020-11-24 18:26:20 -05:00
};