@ -1,14 +1,17 @@
/ * *
* Tests for the Daily Notification plugin
*
* @author Matthew Raymer
* /
import { registerPlugin } from '@capacitor/core ';
import { DailyNotification } from '../src/daily-notification ';
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' , ( ) = > {
let plugin : DailyNotification ;
let mockPlugin : jest.Mocked < DailyNotificationPlugin > ;
const mockOptions : NotificationOptions = {
url : 'https://api.example.com/daily-content' ,
time : '08:00' ,
@ -17,24 +20,46 @@ describe('DailyNotification Plugin', () => {
} ;
beforeEach ( ( ) = > {
// Create mock plugin with all required methods
mockPlugin = {
scheduleDailyNotification : jest.fn ( ) ,
getLastNotification : jest.fn ( ) ,
cancelAllNotifications : jest.fn ( ) ,
getNotificationStatus : jest.fn ( ) ,
updateSettings : jest.fn ( ) ,
getBatteryStatus : jest.fn ( ) ,
requestBatteryOptimizationExemption : jest.fn ( ) ,
setAdaptiveScheduling : jest.fn ( ) ,
getPowerState : jest.fn ( ) ,
checkPermissions : jest.fn ( ) ,
requestPermissions : jest.fn ( ) ,
} ;
// Create plugin instance with mock
plugin = new DailyNotification ( mockPlugin ) ;
// 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 ) ;
await plugi n. scheduleDailyNotification ( mockOptions ) ;
// Verify the mock was called with correct parameters
expect ( mockPlugi n. scheduleDailyNotification ) . toHaveBeenCalledWith ( mockOptions ) ;
} ) ;
it ( 'should handle network errors gracefully' , async ( ) = > {
mockPlugin . scheduleDailyNotification . mockRejectedValueOnce (
new Error ( 'Network error' )
) ;
const errorOptions : NotificationOptions = {
. . . mockOptions ,
url : 'https://invalid-url.com'
} ;
await expect ( DailyNotification . scheduleDailyNotification ( errorOptions ) )
await expect ( plugi n. scheduleDailyNotification ( errorOptions ) )
. rejects
. toThrow ( 'Network error' ) ;
} ) ;
@ -44,7 +69,7 @@ describe('DailyNotification Plugin', () => {
time : '08:00'
} as NotificationOptions ;
await expect ( DailyNotificatio n. scheduleDailyNotification ( invalidOptions ) )
await expect ( plugi n. scheduleDailyNotification ( invalidOptions ) )
. rejects
. toThrow ( 'URL is required' ) ;
} ) ;
@ -59,21 +84,25 @@ describe('DailyNotification Plugin', () => {
timestamp : Date.now ( )
} ;
const result = await DailyNotification . getLastNotification ( ) ;
mockPlugin . getLastNotification . mockResolvedValueOnce ( mockResponse ) ;
const result = await plugin . getLastNotification ( ) ;
expect ( result ) . toEqual ( mockResponse ) ;
} ) ;
it ( 'should return null when no notifications exist' , async ( ) = > {
const result = await DailyNotification . getLastNotification ( ) ;
mockPlugin . getLastNotification . mockResolvedValueOnce ( null ) ;
const result = await plugin . getLastNotification ( ) ;
expect ( result ) . toBeNull ( ) ;
} ) ;
} ) ;
describe ( 'cancelAllNotifications' , ( ) = > {
it ( 'should cancel all scheduled notifications' , async ( ) = > {
await DailyNotificatio n. cancelAllNotifications ( ) ;
// Verify the native implementation was called
expect ( DailyNotificatio n. cancelAllNotifications ) . toHaveBeenCalled ( ) ;
await plugi n. cancelAllNotifications ( ) ;
// Verify the mock was called
expect ( mockPlugi n. cancelAllNotifications ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
@ -86,7 +115,9 @@ describe('DailyNotification Plugin', () => {
settings : { }
} ;
const result = await DailyNotification . getNotificationStatus ( ) ;
mockPlugin . getNotificationStatus . mockResolvedValueOnce ( mockStatus ) ;
const result = await plugin . getNotificationStatus ( ) ;
expect ( result ) . toEqual ( mockStatus ) ;
} ) ;
@ -99,7 +130,9 @@ describe('DailyNotification Plugin', () => {
settings : { }
} ;
const result = await DailyNotification . getNotificationStatus ( ) ;
mockPlugin . getNotificationStatus . mockResolvedValueOnce ( mockErrorStatus ) ;
const result = await plugin . getNotificationStatus ( ) ;
expect ( result ) . toEqual ( mockErrorStatus ) ;
} ) ;
} ) ;
@ -112,40 +145,109 @@ describe('DailyNotification Plugin', () => {
timezone : 'UTC'
} ;
await DailyNotificatio n. updateSettings ( settings ) ;
expect ( DailyNotificatio n. updateSettings ) . toHaveBeenCalledWith ( settings ) ;
await plugi n. updateSettings ( settings ) ;
expect ( mockPlugi n. 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' ) ;
const mockBatteryStatus = {
level : 85 ,
isCharging : false ,
powerState : 1 ,
isOptimizationExempt : false
} ;
mockPlugin . getBatteryStatus . mockResolvedValueOnce ( mockBatteryStatus ) ;
const result = await plugin . getBatteryStatus ( ) ;
expect ( result ) . toEqual ( mockBatteryStatus ) ;
} ) ;
} ) ;
describe ( 'requestBatteryOptimizationExemption' , ( ) = > {
it ( 'should request battery optimization exemption' , async ( ) = > {
await DailyNotification . requestBatteryOptimizationExemption ( ) ;
expect ( DailyNotificatio n. requestBatteryOptimizationExemption ) . toHaveBeenCalled ( ) ;
await plugi n. requestBatteryOptimizationExemption ( ) ;
expect ( mockPlugi n. requestBatteryOptimizationExemption ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
describe ( 'setAdaptiveScheduling' , ( ) = > {
it ( 'should set adaptive scheduling' , async ( ) = > {
await DailyNotificatio n. setAdaptiveScheduling ( { enabled : true } ) ;
expect ( DailyNotificatio n. setAdaptiveScheduling ) . toHaveBeenCalledWith ( { enabled : true } ) ;
await plugi n. setAdaptiveScheduling ( { enabled : true } ) ;
expect ( mockPlugi n. 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' ) ;
const mockPowerState = {
powerState : 1 ,
isOptimizationExempt : false
} ;
mockPlugin . getPowerState . mockResolvedValueOnce ( mockPowerState ) ;
const result = await plugin . getPowerState ( ) ;
expect ( result ) . toEqual ( mockPowerState ) ;
} ) ;
} ) ;
describe ( 'validation' , ( ) = > {
it ( 'should validate URL format' , async ( ) = > {
const invalidOptions = {
. . . mockOptions ,
url : 'invalid-url'
} ;
await expect ( plugin . scheduleDailyNotification ( invalidOptions ) )
. rejects
. toThrow ( 'Invalid URL format' ) ;
} ) ;
it ( 'should validate time format' , async ( ) = > {
const invalidOptions = {
. . . mockOptions ,
time : '25:00'
} ;
await expect ( plugin . scheduleDailyNotification ( invalidOptions ) )
. rejects
. toThrow ( 'Invalid time format' ) ;
} ) ;
it ( 'should validate timezone format' , async ( ) = > {
const invalidOptions = {
. . . mockOptions ,
timezone : 'Invalid/Timezone'
} ;
await expect ( plugin . scheduleDailyNotification ( invalidOptions ) )
. rejects
. toThrow ( 'Invalid timezone' ) ;
} ) ;
it ( 'should validate retry count range' , async ( ) = > {
const invalidOptions = {
. . . mockOptions ,
retryCount : 15
} ;
await expect ( plugin . scheduleDailyNotification ( invalidOptions ) )
. rejects
. toThrow ( 'Retry count must be between 0 and 10' ) ;
} ) ;
it ( 'should validate retry interval range' , async ( ) = > {
const invalidOptions = {
. . . mockOptions ,
retryInterval : 50
} ;
await expect ( plugin . scheduleDailyNotification ( invalidOptions ) )
. rejects
. toThrow ( 'Retry interval must be between 100ms and 60s' ) ;
} ) ;
} ) ;
} ) ;