Skip to content

Commit

Permalink
Migrating notification test
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoarthur0 committed Jan 13, 2024
1 parent 453c2ac commit 9c8c4af
Showing 1 changed file with 63 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
/* eslint-disable no-undef */
'use strict';

const { createNotification, createLeaveNotification, updateDismiss, getDismiss } = require('../../js/notification.js');
const { getUserPreferences, savePreferences, resetPreferences } = require('../../js/user-preferences.js');
const { getDateStr } = require('../../js/date-aux.js');
import assert from 'assert';
import { app } from 'electron';
import { stub } from 'sinon';

import { createNotification, createLeaveNotification, updateDismiss, getDismiss } from '../../js/notification.mjs';
import { getUserPreferences, savePreferences, resetPreferences } from '../../js/user-preferences.mjs';
import { getDateStr } from '../../js/date-aux.mjs';

function buildTimeString(now)
{
return `0${now.getHours()}`.slice(-2) + ':' + `0${now.getMinutes()}`.slice(-2);
}

process.env.NODE_ENV = 'test';

describe('Notifications', function()
{
describe('notify', () =>
{
beforeEach(() =>
{
// displays a notification in test fails if mocks are not restored
jest.restoreAllMocks();
});

test('displays a notification in test', (done) =>
it('displays a notification in test', (done) =>
{
process.env.NODE_ENV = 'test';
const notification = createNotification('test');
// On Win32 the notification uses a different specification, with toastXml
if (process.platform === 'win32')
{
expect(notification.toastXml).toMatch('<text>test</text>');
expect(notification.toastXml).toMatch('<text>Time to Leave</text>');
assert.match(notification.toastXml, RegExp('<text>test</text>'));
assert.match(notification.toastXml, RegExp('<text>Time to Leave</text>'));
}
else
{
expect(notification.body).toBe('test');
expect(notification.title).toBe('Time to Leave');
assert.strictEqual(notification.body, 'test');
assert.strictEqual(notification.title, 'Time to Leave');
}
notification.on('show', (event) =>
{
expect(event).toBeTruthy();
assert.strictEqual(typeof event === 'object', true);
// In Electron 25 the definition of Event changed and we can no longer
// check information about the event sender
notification.close();
Expand All @@ -61,24 +59,24 @@ describe('Notifications', function()
}
});

test('displays a notification in production', (done) =>
it('displays a notification in production', (done) =>
{
process.env.NODE_ENV = 'production';
const notification = createNotification('production');
// On Win32 the notification uses a different specification, with toastXml
if (process.platform === 'win32')
{
expect(notification.toastXml).toMatch('<text>production</text>');
expect(notification.toastXml).toMatch('<text>Time to Leave</text>');
assert.match(notification.toastXml, RegExp('<text>production</text>'));
assert.match(notification.toastXml, RegExp('<text>Time to Leave</text>'));
}
else
{
expect(notification.body).toBe('production');
expect(notification.title).toBe('Time to Leave');
assert.strictEqual(notification.body, 'production');
assert.strictEqual(notification.title, 'Time to Leave');
}
notification.on('show', (event) =>
{
expect(event).toBeTruthy();
assert.strictEqual(typeof event === 'object', true);
// In Electron 25 the definition of Event changed and we can no longer
// check information about the event sender
notification.close();
Expand All @@ -105,116 +103,116 @@ describe('Notifications', function()

describe('createLeaveNotification', () =>
{
test('Should fail when notifications are disabled', () =>
it('Should fail when notifications are disabled', () =>
{
const preferences = getUserPreferences();
preferences['notification'] = false;
savePreferences(preferences);
const notify = createLeaveNotification(true);
expect(notify).toBe(false);
assert.strictEqual(notify, false);
});

test('Should fail when leaveByElement is not found', () =>
it('Should fail when leaveByElement is not found', () =>
{
const notify = createLeaveNotification(undefined);
expect(notify).toBe(false);
assert.strictEqual(notify, false);
});

test('Should fail when notifications have been dismissed', () =>
it('Should fail when notifications have been dismissed', () =>
{
const now = new Date();
const dateToday = getDateStr(now);
updateDismiss(dateToday);
const notify = createLeaveNotification(true);
expect(notify).toBe(false);
assert.strictEqual(notify, false);
});

test('Should fail when time is not valid', () =>
it('Should fail when time is not valid', () =>
{
const notify = createLeaveNotification('33:90');
expect(notify).toBe(false);
assert.strictEqual(notify, false);
});

test('Should fail when time is in the future', () =>
it('Should fail when time is in the future', () =>
{
jest.restoreAllMocks();
const now = new Date();
now.setMinutes(now.getMinutes() + 1);
const notify = createLeaveNotification(buildTimeString(now));
expect(notify).toBe(false);
assert.strictEqual(notify, false);
});

test('Should fail when time is in the past', () =>
it('Should fail when time is in the past', () =>
{
const now = new Date();
now.setMinutes(now.getMinutes() - 9);
const notify = createLeaveNotification(buildTimeString(now));
expect(notify).toBe(false);
assert.strictEqual(notify, false);
});

test('Should fail when repetition is disabled', () =>
it('Should fail when repetition is disabled', () =>
{
const preferences = getUserPreferences();
preferences['repetition'] = false;
savePreferences(preferences);
const now = new Date();
now.setHours(now.getHours() - 1);
const notify = createLeaveNotification(buildTimeString(now));
expect(notify).toBe(false);
assert.strictEqual(notify, false);
});

test('Should pass when time is correct and dismiss action is pressed', () =>
it('Should pass when time is correct and dismiss action is pressed', () =>
{
const now = new Date();
const notify = createLeaveNotification(buildTimeString(now));
expect(notify).toBeTruthy();
expect(getDismiss()).toBe(null);
expect(notify.listenerCount('action')).toBe(1);
expect(notify.listenerCount('close')).toBe(1);
expect(notify.listenerCount('click')).toBe(1);
assert.notStrictEqual(notify, false);
assert.strictEqual(getDismiss(), null);
assert.strictEqual(notify.listenerCount('action'), 1);
assert.strictEqual(notify.listenerCount('close'), 1);
assert.strictEqual(notify.listenerCount('click'), 1);
notify.emit('action', 'dismiss');
expect(getDismiss()).toBe(getDateStr(now));
assert.strictEqual(getDismiss(), getDateStr(now));
});

test('Should pass when time is correct and other action is pressed', () =>
it('Should pass when time is correct and other action is pressed', () =>
{
const now = new Date();
const notify = createLeaveNotification(buildTimeString(now));
expect(notify).toBeTruthy();
expect(getDismiss()).toBe(null);
expect(notify.listenerCount('action')).toBe(1);
expect(notify.listenerCount('close')).toBe(1);
expect(notify.listenerCount('click')).toBe(1);
assert.notStrictEqual(notify, false);
assert.strictEqual(getDismiss(), null);
assert.strictEqual(notify.listenerCount('action'), 1);
assert.strictEqual(notify.listenerCount('close'), 1);
assert.strictEqual(notify.listenerCount('click'), 1);
notify.emit('action', '');
expect(getDismiss()).toBe(null);
assert.strictEqual(getDismiss(), null);
});

test('Should pass when time is correct and close is pressed', () =>
it('Should pass when time is correct and close is pressed', () =>
{
const now = new Date();
const notify = createLeaveNotification(buildTimeString(now));
expect(notify).toBeTruthy();
expect(getDismiss()).toBe(null);
expect(notify.listenerCount('action')).toBe(1);
expect(notify.listenerCount('close')).toBe(1);
expect(notify.listenerCount('click')).toBe(1);
assert.notStrictEqual(notify, false);
assert.strictEqual(getDismiss(), null);
assert.strictEqual(notify.listenerCount('action'), 1);
assert.strictEqual(notify.listenerCount('close'), 1);
assert.strictEqual(notify.listenerCount('click'), 1);
notify.emit('close');
expect(getDismiss()).toBe(getDateStr(now));
assert.strictEqual(getDismiss(), getDateStr(now));
});

test('Should pass when time is correct and close is pressed', (done) =>
it('Should pass when time is correct and close is pressed', (done) =>
{
jest.spyOn(app, 'emit').mockImplementation((key) =>
const appStub = stub(app, 'emit').callsFake((key) =>
{
expect(key).toBe('activate');
assert.strictEqual(key, 'activate');
appStub.restore();
done();
});
const now = new Date();
const notify = createLeaveNotification(buildTimeString(now));
expect(notify).toBeTruthy();
expect(notify.listenerCount('action')).toBe(1);
expect(notify.listenerCount('close')).toBe(1);
expect(notify.listenerCount('click')).toBe(1);
assert.notStrictEqual(notify, false);
assert.strictEqual(notify.listenerCount('action'), 1);
assert.strictEqual(notify.listenerCount('close'), 1);
assert.strictEqual(notify.listenerCount('click'), 1);
notify.emit('click', 'Clicked on notification');
});
});
Expand All @@ -225,4 +223,3 @@ describe('Notifications', function()
updateDismiss(null);
});
});

0 comments on commit 9c8c4af

Please sign in to comment.