From 9c8c4af6ec715cbe8f159358a51c2e5871cb84cb Mon Sep 17 00:00:00 2001 From: Arthur Araujo Date: Fri, 12 Jan 2024 21:21:23 -0300 Subject: [PATCH] Migrating notification test --- .../{notification.js => notification.mjs} | 129 +++++++++--------- 1 file changed, 63 insertions(+), 66 deletions(-) rename __tests__/__main__/{notification.js => notification.mjs} (57%) diff --git a/__tests__/__main__/notification.js b/__tests__/__main__/notification.mjs similarity index 57% rename from __tests__/__main__/notification.js rename to __tests__/__main__/notification.mjs index bfb94778..5310b214 100644 --- a/__tests__/__main__/notification.js +++ b/__tests__/__main__/notification.mjs @@ -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('test'); - expect(notification.toastXml).toMatch('Time to Leave'); + assert.match(notification.toastXml, RegExp('test')); + assert.match(notification.toastXml, RegExp('Time to Leave')); } 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(); @@ -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('production'); - expect(notification.toastXml).toMatch('Time to Leave'); + assert.match(notification.toastXml, RegExp('production')); + assert.match(notification.toastXml, RegExp('Time to Leave')); } 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(); @@ -105,54 +103,53 @@ 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; @@ -160,61 +157,62 @@ describe('Notifications', function() 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'); }); }); @@ -225,4 +223,3 @@ describe('Notifications', function() updateDismiss(null); }); }); -