UNPKG

smsir_mh_ts

Version:

This package is for using SMS service from sms.ir should get API and templetId from sms.ir. TypeScript support

55 lines (39 loc) 1.65 kB
import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import { SendAlertSMSIR } from './smsir_mh_ts'; // Create a mock instance of axios const mock = new MockAdapter(axios); describe('SendAlertSMSIR', () => { const to = '1234567890'; const message = 'Test Message'; const api_key = 'test_api_key'; const templateId = 'test_template_id'; beforeEach(() => { // Reset mock adapter between tests mock.reset(); }); test('should send SMS successfully', async () => { // Mock successful response mock.onPost('https://api.sms.ir/v1/send/verify').reply(200, { success: true }); // Spy on console.info const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation(() => {}); SendAlertSMSIR(to, message, api_key, templateId); // Wait for axios request to be processed await new Promise(process.nextTick); expect(consoleInfoSpy).toHaveBeenCalledWith(JSON.stringify({ success: true })); // Restore console.info consoleInfoSpy.mockRestore(); }); test('should handle error response', async () => { // Mock error response mock.onPost('https://api.sms.ir/v1/send/verify').reply(500, { error: 'Internal Server Error' }); // Spy on console.error const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); SendAlertSMSIR(to, message, api_key, templateId); // Wait for axios request to be processed await new Promise(process.nextTick); expect(consoleErrorSpy).toHaveBeenCalled(); // Restore console.error consoleErrorSpy.mockRestore(); }); });