Skip to content

Commit

Permalink
Doing a single dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoarthur0 committed Dec 31, 2024
1 parent 2f22f8b commit 2149f30
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions __tests__/__renderer__/preferences.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,17 @@ function resetPreferenceFile()

const testPreferences = Object.assign({}, defaultPreferences);

// Functions from preferences.js that will be imported dynamically
let convertTimeFormat;
let listenerLanguage;
let populateLanguages;
let refreshContent;
let renderPreferencesWindow;
let resetContent;

describe('Test Preferences Window', () =>
{
before(() =>
before(async() =>
{
// APIs from the preload script of the preferences window
window.mainApi = preferencesApi;
Expand All @@ -93,27 +101,29 @@ describe('Test Preferences Window', () =>
window.mainApi.getLanguageDatePromise = () => {};
window.mainApi.showDialogSync = () => { return new Promise((resolve) => resolve({ response: 0 })); };

// TODO: add the necessary values here for the reset test to work
// window.mainApi.getLanguageDataPromise = () => { return new Promise((resolve) => resolve({
// 'language': 'en',
// 'data': {}
// })); };

resetPreferenceFile();

// Using dynamic imports because when the file is imported a $() callback is triggered and
// methods must be mocked before-hand
const file = await import('../../src/preferences.js');
convertTimeFormat = file.convertTimeFormat;
listenerLanguage = file.listenerLanguage;
populateLanguages = file.populateLanguages;
refreshContent = file.refreshContent;
renderPreferencesWindow = file.renderPreferencesWindow;
resetContent = file.resetContent;
});

describe('Changing values of items in window', () =>
{
beforeEach(async function()
{
// For some reason this test takes longer when running the whole testsuite. My suspicion is that
// import is taking longer after many tests write to the file.
// Thus, increasing the timeout.
this.timeout(15000);

// Using dynamic imports because when the file is imported a $() callback is triggered and
// methods must be mocked before-hand
const {
listenerLanguage,
populateLanguages,
refreshContent,
renderPreferencesWindow,
} = await import('../../src/preferences.js');

await prepareMockup();
await refreshContent();
renderPreferencesWindow();
Expand Down Expand Up @@ -235,12 +245,6 @@ describe('Test Preferences Window', () =>

describe('Check if configure hours per day conversion function', () =>
{
let convertTimeFormat;
before(async() =>
{
convertTimeFormat = (await import('../../src/preferences.js')).convertTimeFormat;
});

it('should convert single digit hour to HH:MM format', () =>
{
assert.strictEqual(convertTimeFormat('6'), '06:00');
Expand Down Expand Up @@ -286,19 +290,6 @@ describe('Test Preferences Window', () =>
{
beforeEach(async function()
{
// For some reason this test takes longer when running the whole testsuite. My suspicion is that
// import is taking longer after many tests write to the file.
// Thus, increasing the timeout.
this.timeout(15000);

// Using dynamic imports because when the file is imported a $() callback is triggered and
// methods must be mocked before-hand
const {
listenerLanguage,
populateLanguages,
resetContent
} = await import('../../src/preferences.js');

await prepareMockup();
resetContent();
populateLanguages();
Expand All @@ -312,6 +303,7 @@ describe('Test Preferences Window', () =>
const resetTextAfterClick = $('#reset-button').text();
assert.notStrictEqual(resetText, resetTextAfterClick);
});

it('Click reset and check that setting was restored', () =>
{
changeItemValue('count-today', true);
Expand Down

0 comments on commit 2149f30

Please sign in to comment.