scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
204 lines (178 loc) • 4.54 kB
text/typescript
import {AbsMail} from 'scriptable-abstract';
interface MailState {
toRecipients: string[];
ccRecipients: string[];
bccRecipients: string[];
subject: string;
body: string;
isBodyHTML: boolean;
preferredSendingEmailAddress: string;
attachments: Array<{
type: 'image' | 'file' | 'data';
data: any;
mimeType?: string;
filename?: string;
}>;
}
const DEFAULT_STATE: MailState = {
toRecipients: [],
ccRecipients: [],
bccRecipients: [],
subject: '',
body: '',
isBodyHTML: false,
preferredSendingEmailAddress: '',
attachments: [],
};
/**
* Mock implementation of Scriptable's Mail.
* Provides functionality for composing and sending emails.
* @implements Mail
*/
export class MockMail extends AbsMail<MailState> {
private static _instance: MockMail | null = null;
static get instance(): MockMail {
if (!this._instance) {
this._instance = new MockMail();
}
return this._instance;
}
constructor() {
super(DEFAULT_STATE);
}
get toRecipients(): string[] {
return [...this.state.toRecipients];
}
set toRecipients(value: string[]) {
this.setState({toRecipients: [...value]});
}
get ccRecipients(): string[] {
return [...this.state.ccRecipients];
}
set ccRecipients(value: string[]) {
this.setState({ccRecipients: [...value]});
}
get bccRecipients(): string[] {
return [...this.state.bccRecipients];
}
set bccRecipients(value: string[]) {
this.setState({bccRecipients: [...value]});
}
get subject(): string {
return this.state.subject;
}
set subject(value: string) {
this.setState({subject: value});
}
get body(): string {
return this.state.body;
}
set body(value: string) {
this.setState({body: value});
}
get isBodyHTML(): boolean {
return this.state.isBodyHTML;
}
set isBodyHTML(value: boolean) {
this.setState({isBodyHTML: value});
}
get preferredSendingEmailAddress(): string {
return this.state.preferredSendingEmailAddress;
}
set preferredSendingEmailAddress(value: string) {
this.setState({preferredSendingEmailAddress: value});
}
/**
* Send the mail.
* @returns A promise that resolves when the mail is sent
*/
async send(): Promise<void> {
// In a mock environment, we'll simulate successful email sending
return Promise.resolve();
}
/**
* Adds an image attachment to the mail.
* @param image - Image to add to the mail
*/
addImageAttachment(image: Image): void {
this.setState(state => ({
attachments: [
...state.attachments,
{
type: 'image',
data: image,
},
],
}));
}
/**
* Adds a file attachment to the mail.
* @param filePath - Path of file to add to the mail
*/
addFileAttachment(filePath: string): void {
this.setState(state => ({
attachments: [
...state.attachments,
{
type: 'file',
data: filePath,
},
],
}));
}
/**
* Adds a data attachment to the mail.
* @param data - Data representation of file to add to the mail
* @param mimeType - MIME type of file represented by the data
* @param filename - Name of the file represented by the data
*/
addDataAttachment(data: Data, mimeType: string, filename: string): void {
this.setState(state => ({
attachments: [
...state.attachments,
{
type: 'data',
data: data,
mimeType,
filename,
},
],
}));
}
/**
* Get the last composed email details
* @returns The details of the last composed email
*/
getLastComposedEmail(): {
toRecipients: string[];
ccRecipients: string[];
bccRecipients: string[];
subject: string;
body: string;
isBodyHTML: boolean;
attachments: Array<{
type: 'image' | 'file' | 'data';
data: any;
mimeType?: string;
filename?: string;
}>;
preferredSendingEmailAddress: string;
} {
return {
toRecipients: [...this.state.toRecipients],
ccRecipients: [...this.state.ccRecipients],
bccRecipients: [...this.state.bccRecipients],
subject: this.state.subject,
body: this.state.body,
isBodyHTML: this.state.isBodyHTML,
attachments: [...this.state.attachments],
preferredSendingEmailAddress: this.state.preferredSendingEmailAddress,
};
}
/**
* Clear the last composed email details
*/
clear(): void {
this.setState(DEFAULT_STATE);
}
}