Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Migrating to Electron 28 #1054

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
97 changes: 97 additions & 0 deletions __mocks__/Mock.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

class MockClass
{
/**
* Construct passing a dictionary of strings to Functions.
* @param {Dictionary<string, Function>} mocks
*/
constructor(mocks)
{
this._mocks = mocks;
this._mocked = {};
this._originals = {};
for (const [methodName, method] of Object.entries(this._mocks))
{
this._mocked[methodName] = false;
this._originals[methodName] = method;
}
}

/**
* Test related function to enable mocking exported methods
* @param {string} methodName
* @param {Function} stub
*/
mock(methodName, stub)
{
if (!(methodName in this._mocks))
{
throw Error('Mocking not set for this method');
}
else
{
this._mocks[methodName] = stub;
this._mocked[methodName] = true;
}
}

/**
* Test related function to get mocking exported methods
* @param {string} methodName
*/
getMock(methodName)
{
if (!(methodName in this._mocks))
{
throw Error('Mocking not set for this method');
}
else
{
if (!this._mocked[methodName])
{
throw Error('Method not mocked');
}
return this._mocks[methodName];
}
}

/**
* Restore a single mocked method
* @param {string} methodName
*/
restoreMock(methodName)
{
if (!(methodName in this._mocks))
{
throw Error('Mocking not set for this method');
}
else
{
if (!this._mocked[methodName])
{
throw Error('Method not mocked');
}
this._mocks[methodName] = this._originals[methodName];
this._mocked[methodName] = false;
}
}

/**
* Restore all mocked methods
*/
restoreAll()
{
for (const [methodName, isMocked] of Object.entries(this._mocked))
{
if (isMocked)
{
this.restoreMock(methodName);
}
}
}
}

export {
MockClass,
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import assert from 'assert';

import { getDateStr, getCurrentDateTimeStr, getMonthLength } from '../../js/date-aux.js';
import { getDateStr, getCurrentDateTimeStr, getMonthLength } from '../../js/date-aux.mjs';

describe('Date Functions', () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import {
importDatabaseFromFile,
migrateFixedDbToFlexible,
validEntry,
} from '../../js/import-export.js';
} from '../../js/import-export.mjs';

describe('Import export', function()
{
process.env.NODE_ENV = 'test';

// TODO: Regular store entries are still here to test import of old dbs. Please remove on the next release.
describe('validEntry(entry)', function()
{
Expand Down
Loading
Loading