2025-02-28 19:11:12 +02:00
import { beforeAll , vi } from "vitest" ;
import $ from "jquery" ;
2025-02-28 19:12:19 +02:00
injectGlobals ( ) ;
2025-02-28 19:11:12 +02:00
beforeAll ( ( ) = > {
2025-02-28 19:13:33 +02:00
vi . mock ( "../services/ws.js" , mockWebsocket ) ;
vi . mock ( "../services/server.js" , mockServer ) ;
2025-02-28 19:11:12 +02:00
} ) ;
function injectGlobals() {
const uncheckedWindow = window as any ;
uncheckedWindow . $ = $ ;
uncheckedWindow . WebSocket = ( ) = > { } ;
uncheckedWindow . glob = {
isMainWindow : true
} ;
}
function mockWebsocket() {
return {
default : {
subscribeToMessages ( callback : ( message : unknown ) = > void ) {
// Do nothing.
}
}
}
}
function mockServer() {
return {
default : {
async get ( url : string ) {
if ( url === "options" ) {
return { } ;
}
if ( url === "keyboard-actions" ) {
return [ ] ;
}
if ( url === "tree" ) {
return {
branches : [ ] ,
notes : [ ] ,
attributes : [ ]
}
}
2025-02-28 19:55:02 +02:00
} ,
2025-02-28 22:02:41 +02:00
async post ( url : string , data : object ) {
2025-02-28 19:55:02 +02:00
if ( url === "tree/load" ) {
2025-02-28 22:02:41 +02:00
throw new Error ( ` A module tried to load from the server the following notes: ${ ( ( data as any ) . noteIds || [ ] ) . join ( "," ) } \ nThis is not supported, use Froca mocking instead and ensure the note exist in the mock. ` )
2025-02-28 19:55:02 +02:00
}
2025-02-28 19:11:12 +02:00
}
}
} ;
}