mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
30 lines
708 B
TypeScript
30 lines
708 B
TypeScript
import { assertEquals } from '@std/assert';
|
|
|
|
import { isWorker } from './worker.ts';
|
|
|
|
Deno.test('isWorker from the main thread returns false', () => {
|
|
assertEquals(isWorker(), false);
|
|
});
|
|
|
|
Deno.test('isWorker from a worker thread returns true', async () => {
|
|
const script = `
|
|
import { isWorker } from '@/utils/worker.ts';
|
|
postMessage(isWorker());
|
|
self.close();
|
|
`;
|
|
|
|
const worker = new Worker(
|
|
URL.createObjectURL(new Blob([script], { type: 'application/javascript' })),
|
|
{ type: 'module' },
|
|
);
|
|
|
|
const result = await new Promise<boolean>((resolve) => {
|
|
worker.onmessage = (e) => {
|
|
resolve(e.data);
|
|
};
|
|
});
|
|
|
|
worker.terminate();
|
|
|
|
assertEquals(result, true);
|
|
});
|