slint/api/node/__test__/eventloop.spec.mts
Simon Hausmann 84fd6dc08c Clean up Node.js port build system
- Instead of building index.js with esbuild and generating types with
  tsc, use tsc to build index.js and index.d.ts.
- Use ts-node instead of swc for typescript based tests, as that
  works with es modules.
- Remove "syntax_check" target from package.json as that's now
  implied with "compile".
- Sadly this requires one "as any" cast as tsc somehow fails to
  determine the right type info for node-fetch.

Unfortunately index.js can't be an ES module without breaking
compatiblity. It would imply that the Node.js port can't be used with
require() anymore. So even thought that would simplify things further,
it's not part of this PR.
2023-11-21 07:20:21 +01:00

80 lines
2 KiB
TypeScript

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
// Test that the Slint event loop processes libuv's events.
import test from 'ava'
import * as http from 'http';
import fetch from "node-fetch";
import { runEventLoop, quitEventLoop, private_api } from '../index.js'
test.serial('merged event loops with timer', async (t) => {
let invoked = false;
await runEventLoop(() => {
setTimeout(() => {
invoked = true;
quitEventLoop();
}, 2);
});
t.true(invoked)
})
test.serial('merged event loops with networking', async (t) => {
const listener = (request, result) => {
result.writeHead(200);
result.end("Hello World");
};
let received_response = "";
await runEventLoop(() => {
const server = http.createServer(listener);
server.listen(async () => {
let host = "localhost";
let port = (server.address() as any).port;
console.log(`server ready at ${host}:${port}`);
(fetch as any)(`http://${host}:${port}/`).then(async (response) => {
return response.text();
}).then((text) => {
received_response = text;
//console.log("received ", text);
quitEventLoop();
server.close();
});
});
})
t.is(received_response, "Hello World");
})
test.serial('quit event loop on last window closed', async (t) => {
let compiler = new private_api.ComponentCompiler;
let definition = compiler.buildFromSource(`
export component App inherits Window {
width: 300px;
height: 300px;
}`, "");
t.not(definition, null);
let instance = definition!.create() as any;
t.not(instance, null);
instance.window().show();
await runEventLoop(() => {
setTimeout(() => {
instance.window().hide();
}, 2);
});
})