scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
99 lines (81 loc) • 2.69 kB
text/typescript
import {AbsWebView} from 'scriptable-abstract';
interface WebViewState {
url?: string;
html?: string;
baseURL?: string;
shouldAllowRequest: (request: Request) => boolean;
}
const DEFAULT_STATE: WebViewState = {
shouldAllowRequest: () => true,
};
export class MockWebView extends AbsWebView<WebViewState> {
constructor() {
super(DEFAULT_STATE);
}
/**
* Loads HTML and renders it.
*/
static async loadHTML(html: string, baseURL?: string, _preferredSize?: Size, fullscreen?: boolean): Promise<void> {
const webView = new MockWebView();
await webView.loadHTML(html, baseURL);
await webView.present(fullscreen);
}
/**
* Loads a file and renders it.
*/
static async loadFile(fileURL: string, _preferredSize?: Size, fullscreen?: boolean): Promise<void> {
const webView = new MockWebView();
await webView.loadFile(fileURL);
await webView.present(fullscreen);
}
/**
* Loads URL in web view and presents the web view.
*/
static async loadURL(url: string, _preferredSize?: Size, fullscreen?: boolean): Promise<void> {
const webView = new MockWebView();
await webView.loadURL(url);
await webView.present(fullscreen);
}
get shouldAllowRequest(): (request: Request) => boolean {
return this.state.shouldAllowRequest;
}
set shouldAllowRequest(value: (request: Request) => boolean) {
this.setState({shouldAllowRequest: value});
}
async loadURL(url: string): Promise<void> {
this.setState({url});
}
async loadHTML(html: string, baseURL?: string): Promise<void> {
this.setState({html, baseURL});
}
async loadRequest(request: Request): Promise<void> {
if (this.state.shouldAllowRequest(request)) {
this.setState({url: request.url});
}
}
async loadFile(path: string): Promise<void> {
this.setState({url: `file://${path}`});
}
async evaluateJavaScript(_javaScript: string, _useCallback?: boolean): Promise<any> {
// Mock implementation
// In a real implementation, we would:
// 1. If useCallback is true, wait for the completion function to be called
// 2. If useCallback is false, evaluate the last line and return its value
return null;
}
async getHTML(): Promise<string> {
return this.state.html ?? '';
}
async getURL(): Promise<string> {
return this.state.url ?? '';
}
present(_fullscreen?: boolean): Promise<void> {
// Mock implementation
// In a real implementation, this would present the web view UI
return Promise.resolve();
}
async waitForLoad(): Promise<void> {
// Mock implementation
// In a real implementation, this would wait for the web view to finish loading
}
}