Skip to content

Commit

Permalink
Merge pull request #1125 from araujoarthur0/no-mock-class
Browse files Browse the repository at this point in the history
Replacing MockClass with export classes and stubs
  • Loading branch information
tupaschoal authored Jan 15, 2025
2 parents eb7ddbc + dfbfacf commit 774787f
Show file tree
Hide file tree
Showing 36 changed files with 1,070 additions and 1,245 deletions.
97 changes: 0 additions & 97 deletions __mocks__/Mock.mjs

This file was deleted.

93 changes: 46 additions & 47 deletions __tests__/__main__/import-export.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,43 @@ import Store from 'electron-store';
import fs from 'fs';
import path from 'path';

import {
exportDatabaseToFile,
importDatabaseFromFile,
validEntry,
} from '../../js/import-export.mjs';
import ImportExport from '../../js/import-export.mjs';

describe('Import export', function()
{
const folder = fs.mkdtempSync('import-export');
const invalidEntriesFile = path.join(folder, 'invalid.ttldb');

const calendarStore = new Store({name: 'flexible-store'});
const waivedWorkdays = new Store({name: 'waived-workdays'});

before(() =>
{
calendarStore.clear();
const entries = {
'2020-3-1': {'values': ['08:00', '12:00', '13:00', '17:00']},
'2020-3-2': {'values': ['07:00', '11:00', '14:00', '18:00']}
};
calendarStore.set(entries);

waivedWorkdays.clear();
const waivedEntries = {
'2019-12-31': {reason: 'New Year\'s eve', hours: '08:00'},
'2020-01-01': {reason: 'New Year\'s Day', hours: '08:00'},
'2020-04-10': {reason: 'Good Friday', hours: '08:00'}
};
waivedWorkdays.set(waivedEntries);

const invalidEntriesContent =
`[{"type": "flexible", "date": "not-a-date", "data": "day-begin", "hours": "08:00"},
{"type": "waived", "date": "2020-01-01", "data": "example waiver 2", "hours": "not-an-hour"},
{"type": "flexible", "date": "not-a-date", "data": "day-end", "hours": "17:00"},
{"type": "flexible", "date": "not-a-date", "values": "not-an-array"},
{"type": "not-a-type", "date": "not-a-date", "data": "day-end", "hours": "17:00"}
]`;
fs.writeFileSync(invalidEntriesFile, invalidEntriesContent, 'utf8');
});

describe('validEntry(entry)', function()
{
const goodEntry = {'type': 'flexible', 'date': '2020-06-03', 'values': ['08:00', '12:00', '13:00', '14:00']};
Expand All @@ -22,66 +51,36 @@ describe('Import export', function()
const badWaivedEntry = {'type': 'regular', 'date': '2020-06-03', 'data': 'day-begin', 'hours': 'not-an-hour'};
it('should be valid', () =>
{
assert.strictEqual(validEntry(goodWaivedEntry), true);
assert.strictEqual(validEntry(goodEntry), true);
assert.strictEqual(ImportExport.validEntry(goodWaivedEntry), true);
assert.strictEqual(ImportExport.validEntry(goodEntry), true);
});

it('should not be valid', () =>
{
assert.strictEqual(validEntry(badWaivedEntry), false);
assert.strictEqual(validEntry(badEntry), false);
assert.strictEqual(validEntry(badEntry2), false);
assert.strictEqual(ImportExport.validEntry(badWaivedEntry), false);
assert.strictEqual(ImportExport.validEntry(badEntry), false);
assert.strictEqual(ImportExport.validEntry(badEntry2), false);
});
});

const calendarStore = new Store({name: 'flexible-store'});
const waivedWorkdays = new Store({name: 'waived-workdays'});

calendarStore.clear();
const entries = {
'2020-3-1': {'values': ['08:00', '12:00', '13:00', '17:00']},
'2020-3-2': {'values': ['07:00', '11:00', '14:00', '18:00']}
};
calendarStore.set(entries);

waivedWorkdays.clear();
const waivedEntries = {
'2019-12-31': {reason: 'New Year\'s eve', hours: '08:00'},
'2020-01-01': {reason: 'New Year\'s Day', hours: '08:00'},
'2020-04-10': {reason: 'Good Friday', hours: '08:00'}
};
waivedWorkdays.set(waivedEntries);

const folder = fs.mkdtempSync('import-export');

describe('exportDatabaseToFile', function()
{
it('Check that export works', () =>
{
assert.strictEqual(exportDatabaseToFile(path.join(folder, 'exported_file.ttldb')), true);
assert.strictEqual(exportDatabaseToFile('/not/a/valid/path'), false);
assert.strictEqual(ImportExport.exportDatabaseToFile(path.join(folder, 'exported_file.ttldb')), true);
assert.strictEqual(ImportExport.exportDatabaseToFile('/not/a/valid/path'), false);
});
});

const invalidEntriesContent =
`[{"type": "flexible", "date": "not-a-date", "data": "day-begin", "hours": "08:00"},
{"type": "waived", "date": "2020-01-01", "data": "example waiver 2", "hours": "not-an-hour"},
{"type": "flexible", "date": "not-a-date", "data": "day-end", "hours": "17:00"},
{"type": "flexible", "date": "not-a-date", "values": "not-an-array"},
{"type": "not-a-type", "date": "not-a-date", "data": "day-end", "hours": "17:00"}
]`;
const invalidEntriesFile = path.join(folder, 'invalid.ttldb');
fs.writeFileSync(invalidEntriesFile, invalidEntriesContent, 'utf8');

describe('importDatabaseFromFile', function()
{
it('Check that import works', () =>
{
assert.strictEqual(importDatabaseFromFile([path.join(folder, 'exported_file.ttldb')])['result'], true);
assert.strictEqual(importDatabaseFromFile(['/not/a/valid/path'])['result'], false);
assert.strictEqual(importDatabaseFromFile(['/not/a/valid/path'])['failed'], 0);
assert.strictEqual(importDatabaseFromFile([invalidEntriesFile])['result'], false);
assert.strictEqual(importDatabaseFromFile([invalidEntriesFile])['failed'], 5);
assert.strictEqual(ImportExport.importDatabaseFromFile([path.join(folder, 'exported_file.ttldb')])['result'], true);
assert.strictEqual(ImportExport.importDatabaseFromFile(['/not/a/valid/path'])['result'], false);
assert.strictEqual(ImportExport.importDatabaseFromFile(['/not/a/valid/path'])['failed'], 0);
assert.strictEqual(ImportExport.importDatabaseFromFile([invalidEntriesFile])['result'], false);
assert.strictEqual(ImportExport.importDatabaseFromFile([invalidEntriesFile])['failed'], 5);
});
});

Expand Down
44 changes: 21 additions & 23 deletions __tests__/__main__/main-window.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from 'assert';
import { app, BrowserWindow, ipcMain } from 'electron';
import { match, spy, stub, useFakeTimers } from 'sinon';

import { notificationMock } from '../../js/notification.mjs';
import Notification from '../../js/notification.mjs';
import { savePreferences, getDefaultPreferences, resetPreferences } from '../../js/user-preferences.mjs';

import {
Expand All @@ -15,9 +15,7 @@ import {
triggerStartupDialogs
} from '../../js/main-window.mjs';

import { updateManagerMock } from '../../js/update-manager.mjs';
updateManagerMock.mock('checkForUpdates', stub());
updateManagerMock.mock('shouldCheckForUpdates', stub());
import UpdateManager from '../../js/update-manager.mjs';

// Mocking USER_DATA_PATH for tests below
ipcMain.handle('USER_DATA_PATH', () =>
Expand Down Expand Up @@ -186,29 +184,29 @@ describe('main-window.mjs', () =>
const mainWindow = getMainWindow();
mainWindow.on('ready-to-show', () =>
{
notificationMock.mock('createLeaveNotification', stub().callsFake(() =>
stub(Notification, 'createLeaveNotification').callsFake(() =>
{
return false;
}));
});
ipcMain.emit('RECEIVE_LEAVE_BY', {}, undefined);
assert.strictEqual(notificationMock.getMock('createLeaveNotification').calledOnce, true);
notificationMock.restoreMock('createLeaveNotification');
assert.strictEqual(Notification.createLeaveNotification.calledOnce, true);
Notification.createLeaveNotification.restore();
done();
});
});

it('Should show notification', (done) =>
{
notificationMock.mock('createLeaveNotification', stub().callsFake(() =>
stub(Notification, 'createLeaveNotification').callsFake(() =>
{
return {
show: () =>
{
notificationMock.restoreMock('createLeaveNotification');
Notification.createLeaveNotification.restore();
done();
}
};
}));
});
createWindow();
/**
* @type {BrowserWindow}
Expand Down Expand Up @@ -383,28 +381,28 @@ describe('main-window.mjs', () =>
{
it('Should check for updates and try to migrate', () =>
{
updateManagerMock.mock('shouldCheckForUpdates', stub().returns(true));
updateManagerMock.mock('checkForUpdates', stub());
stub(UpdateManager, 'shouldCheckForUpdates').returns(true);
stub(UpdateManager, 'checkForUpdates');

triggerStartupDialogs();
assert.strictEqual(updateManagerMock.getMock('shouldCheckForUpdates').calledOnce, true);
assert.strictEqual(updateManagerMock.getMock('checkForUpdates').calledOnce, true);
assert.strictEqual(UpdateManager.shouldCheckForUpdates.calledOnce, true);
assert.strictEqual(UpdateManager.checkForUpdates.calledOnce, true);

updateManagerMock.restoreMock('shouldCheckForUpdates');
updateManagerMock.restoreMock('checkForUpdates');
UpdateManager.shouldCheckForUpdates.restore();
UpdateManager.checkForUpdates.restore();
});

it('Should not check for updates when shouldCheck returns falseZ', () =>
{
updateManagerMock.mock('shouldCheckForUpdates', stub().returns(false));
updateManagerMock.mock('checkForUpdates', stub());
stub(UpdateManager, 'shouldCheckForUpdates').returns(false);
stub(UpdateManager, 'checkForUpdates');

triggerStartupDialogs();
assert.strictEqual(updateManagerMock.getMock('shouldCheckForUpdates').calledOnce, true);
assert.strictEqual(updateManagerMock.getMock('checkForUpdates').calledOnce, false);
assert.strictEqual(UpdateManager.shouldCheckForUpdates.calledOnce, true);
assert.strictEqual(UpdateManager.checkForUpdates.calledOnce, false);

updateManagerMock.restoreMock('shouldCheckForUpdates');
updateManagerMock.restoreMock('checkForUpdates');
UpdateManager.shouldCheckForUpdates.restore();
UpdateManager.checkForUpdates.restore();
});
});

Expand Down
Loading

0 comments on commit 774787f

Please sign in to comment.