scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
91 lines (78 loc) • 1.71 kB
text/typescript
import {AbsURLScheme} from 'scriptable-abstract';
interface URLSchemeState {
schemes: Set<string>;
lastOpenedURL: string | null;
}
const DEFAULT_STATE: URLSchemeState = {
schemes: new Set(),
lastOpenedURL: null,
};
/**
* Mock implementation of Scriptable's URLScheme global variable
* @implements URLScheme
*/
export class MockURLScheme extends AbsURLScheme<URLSchemeState> {
static get instance(): MockURLScheme {
return super.instance as MockURLScheme;
}
constructor() {
super(DEFAULT_STATE);
}
/**
* @inheritdoc
*/
async open(urlScheme: string): Promise<void> {
this.setState({lastOpenedURL: urlScheme});
}
/**
* @inheritdoc
*/
async fromURLString(urlString: string): Promise<string> {
return urlString;
}
/**
* @inheritdoc
*/
async forActionExtension(): Promise<string> {
return this.state.lastOpenedURL ?? '';
}
/**
* @inheritdoc
*/
async forNotification(): Promise<string> {
return this.state.lastOpenedURL ?? '';
}
/**
* @additional
* Register a URL scheme for testing
*/
registerScheme(scheme: string): void {
this.setState(state => ({
schemes: new Set([...state.schemes, scheme]),
}));
}
/**
* @additional
* Check if a URL scheme is registered
*/
isRegistered(scheme: string): boolean {
return this.state.schemes.has(scheme);
}
/**
* @additional
* Get the last opened URL
*/
getLastOpenedURL(): string | null {
return this.state.lastOpenedURL;
}
/**
* @additional
* Clear all registered schemes and last opened URL
*/
clear(): void {
this.setState({
schemes: new Set(),
lastOpenedURL: null,
});
}
}