All files / test/mocks twilio.ts

76.47% Statements 13/17
100% Branches 0/0
33.33% Functions 2/6
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                                  1x 1x 1x   1x       1x   1x   1x     1x   1x                   2x 1x   1x    
import { vi } from "vitest";
 
export type MockTwilioClient = {
	messages: {
		create: ReturnType<typeof vi.fn>;
		list: ReturnType<typeof vi.fn>;
		fetch?: ReturnType<typeof vi.fn>;
	};
	request?: ReturnType<typeof vi.fn>;
	messaging?: {
		v2: { channelsSenders: ReturnType<typeof vi.fn> };
		v1: { services: ReturnType<typeof vi.fn> };
	};
	incomingPhoneNumbers?: ReturnType<typeof vi.fn>;
};
 
export function createMockTwilio() {
	const messages = vi.fn((sid?: string) => ({ fetch: vi.fn() })) as any;
	messages.create = vi.fn();
	messages.list = vi.fn();
 
	const channelsSenders = vi.fn((sid?: string) => ({
		fetch: vi.fn(),
		update: vi.fn(),
	})) as any;
	channelsSenders.list = vi.fn();
 
	const services = vi.fn(() => ({ update: vi.fn(), fetch: vi.fn() }));
 
	const incomingPhoneNumbers = vi.fn((sid?: string) => ({
		update: vi.fn(),
	})) as any;
	incomingPhoneNumbers.list = vi.fn();
 
	const client: MockTwilioClient = {
		messages,
		request: vi.fn(),
		messaging: {
			v2: { channelsSenders },
			v1: { services },
		},
		incomingPhoneNumbers,
	};
 
	const factory = vi.fn(() => client) as any;
	(factory as any)._client = client;
 
	return { client, factory };
}