You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
5.0 KiB
150 lines
5.0 KiB
/**
|
|
* Tests for the Daily Notification plugin
|
|
*/
|
|
|
|
import { registerPlugin } from '@capacitor/core';
|
|
import { DailyNotificationPlugin, NotificationOptions, NotificationStatus, NotificationResponse, NotificationSettings } from '../src/definitions';
|
|
import { describe, it, expect, beforeEach, jest } from '@jest/globals';
|
|
|
|
const DailyNotification = registerPlugin<DailyNotificationPlugin>('DailyNotification');
|
|
|
|
describe('DailyNotification Plugin', () => {
|
|
const mockOptions: NotificationOptions = {
|
|
url: 'https://api.example.com/daily-content',
|
|
time: '08:00',
|
|
title: 'Test Notification',
|
|
body: 'This is a test notification'
|
|
};
|
|
|
|
beforeEach(() => {
|
|
// Reset mocks before each test
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
describe('scheduleDailyNotification', () => {
|
|
it('should schedule a basic notification', async () => {
|
|
await DailyNotification.scheduleDailyNotification(mockOptions);
|
|
// Verify the native implementation was called with correct parameters
|
|
expect(DailyNotification.scheduleDailyNotification).toHaveBeenCalledWith(mockOptions);
|
|
});
|
|
|
|
it('should handle network errors gracefully', async () => {
|
|
const errorOptions: NotificationOptions = {
|
|
...mockOptions,
|
|
url: 'https://invalid-url.com'
|
|
};
|
|
|
|
await expect(DailyNotification.scheduleDailyNotification(errorOptions))
|
|
.rejects
|
|
.toThrow('Network error');
|
|
});
|
|
|
|
it('should validate required parameters', async () => {
|
|
const invalidOptions = {
|
|
time: '08:00'
|
|
} as NotificationOptions;
|
|
|
|
await expect(DailyNotification.scheduleDailyNotification(invalidOptions))
|
|
.rejects
|
|
.toThrow('URL is required');
|
|
});
|
|
});
|
|
|
|
describe('getLastNotification', () => {
|
|
it('should return the last notification', async () => {
|
|
const mockResponse: NotificationResponse = {
|
|
title: 'Last Notification',
|
|
body: 'This was the last notification',
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
|
|
const result = await DailyNotification.getLastNotification();
|
|
expect(result).toEqual(mockResponse);
|
|
});
|
|
|
|
it('should return null when no notifications exist', async () => {
|
|
const result = await DailyNotification.getLastNotification();
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('cancelAllNotifications', () => {
|
|
it('should cancel all scheduled notifications', async () => {
|
|
await DailyNotification.cancelAllNotifications();
|
|
// Verify the native implementation was called
|
|
expect(DailyNotification.cancelAllNotifications).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('getNotificationStatus', () => {
|
|
it('should return current notification status', async () => {
|
|
const mockStatus: NotificationStatus = {
|
|
isScheduled: true,
|
|
nextNotificationTime: Date.now() + 86400000, // 24 hours from now
|
|
lastNotificationTime: Date.now(),
|
|
settings: {}
|
|
};
|
|
|
|
const result = await DailyNotification.getNotificationStatus();
|
|
expect(result).toEqual(mockStatus);
|
|
});
|
|
|
|
it('should handle error status', async () => {
|
|
const mockErrorStatus: NotificationStatus = {
|
|
isScheduled: false,
|
|
error: 'Failed to schedule notification',
|
|
lastNotificationTime: 0,
|
|
nextNotificationTime: 0,
|
|
settings: {}
|
|
};
|
|
|
|
const result = await DailyNotification.getNotificationStatus();
|
|
expect(result).toEqual(mockErrorStatus);
|
|
});
|
|
});
|
|
|
|
describe('updateSettings', () => {
|
|
it('should update notification settings', async () => {
|
|
const settings: NotificationSettings = {
|
|
sound: false,
|
|
priority: 'high',
|
|
timezone: 'UTC'
|
|
};
|
|
|
|
await DailyNotification.updateSettings(settings);
|
|
expect(DailyNotification.updateSettings).toHaveBeenCalledWith(settings);
|
|
});
|
|
});
|
|
|
|
describe('getBatteryStatus', () => {
|
|
it('should return battery status', async () => {
|
|
const result = await DailyNotification.getBatteryStatus();
|
|
expect(result).toHaveProperty('level');
|
|
expect(result).toHaveProperty('isCharging');
|
|
expect(result).toHaveProperty('powerState');
|
|
expect(result).toHaveProperty('isOptimizationExempt');
|
|
});
|
|
});
|
|
|
|
describe('requestBatteryOptimizationExemption', () => {
|
|
it('should request battery optimization exemption', async () => {
|
|
await DailyNotification.requestBatteryOptimizationExemption();
|
|
expect(DailyNotification.requestBatteryOptimizationExemption).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('setAdaptiveScheduling', () => {
|
|
it('should set adaptive scheduling', async () => {
|
|
await DailyNotification.setAdaptiveScheduling({ enabled: true });
|
|
expect(DailyNotification.setAdaptiveScheduling).toHaveBeenCalledWith({ enabled: true });
|
|
});
|
|
});
|
|
|
|
describe('getPowerState', () => {
|
|
it('should return power state', async () => {
|
|
const result = await DailyNotification.getPowerState();
|
|
expect(result).toHaveProperty('powerState');
|
|
expect(result).toHaveProperty('isOptimizationExempt');
|
|
});
|
|
});
|
|
});
|