test: use node helpers for events.once and setTimeout promise (#37374)

This commit is contained in:
Jeremy Rose 2023-02-23 15:53:53 -08:00 committed by GitHub
parent 46c8b9c728
commit a3e3efe4c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 932 additions and 927 deletions

View file

@ -1,11 +1,12 @@
import { expect } from 'chai';
import { nativeTheme, systemPreferences, BrowserWindow, ipcMain } from 'electron/main';
import { once } from 'events';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import { setTimeout } from 'timers/promises';
import { delay, ifdescribe } from './lib/spec-helpers';
import { emittedOnce } from './lib/events-helpers';
import { ifdescribe } from './lib/spec-helpers';
import { closeAllWindows } from './lib/window-helpers';
describe('nativeTheme module', () => {
@ -19,7 +20,7 @@ describe('nativeTheme module', () => {
afterEach(async () => {
nativeTheme.themeSource = 'system';
// Wait for any pending events to emit
await delay(20);
await setTimeout(20);
closeAllWindows();
});
@ -37,10 +38,10 @@ describe('nativeTheme module', () => {
it('should emit the "updated" event when it is set and the resulting "shouldUseDarkColors" value changes', async () => {
nativeTheme.themeSource = 'light';
let updatedEmitted = emittedOnce(nativeTheme, 'updated');
let updatedEmitted = once(nativeTheme, 'updated');
nativeTheme.themeSource = 'dark';
await updatedEmitted;
updatedEmitted = emittedOnce(nativeTheme, 'updated');
updatedEmitted = once(nativeTheme, 'updated');
nativeTheme.themeSource = 'light';
await updatedEmitted;
});
@ -48,14 +49,14 @@ describe('nativeTheme module', () => {
it('should not emit the "updated" event when it is set and the resulting "shouldUseDarkColors" value is the same', async () => {
nativeTheme.themeSource = 'dark';
// Wait a few ticks to allow an async events to flush
await delay(20);
await setTimeout(20);
let called = false;
nativeTheme.once('updated', () => {
called = true;
});
nativeTheme.themeSource = 'dark';
// Wait a few ticks to allow an async events to flush
await delay(20);
await setTimeout(20);
expect(called).to.equal(false);
});
@ -83,15 +84,15 @@ describe('nativeTheme module', () => {
.addEventListener('change', () => require('electron').ipcRenderer.send('theme-change'))
`);
const originalSystemIsDark = await getPrefersColorSchemeIsDark(w);
let changePromise: Promise<any[]> = emittedOnce(ipcMain, 'theme-change');
let changePromise: Promise<any[]> = once(ipcMain, 'theme-change');
nativeTheme.themeSource = 'dark';
if (!originalSystemIsDark) await changePromise;
expect(await getPrefersColorSchemeIsDark(w)).to.equal(true);
changePromise = emittedOnce(ipcMain, 'theme-change');
changePromise = once(ipcMain, 'theme-change');
nativeTheme.themeSource = 'light';
await changePromise;
expect(await getPrefersColorSchemeIsDark(w)).to.equal(false);
changePromise = emittedOnce(ipcMain, 'theme-change');
changePromise = once(ipcMain, 'theme-change');
nativeTheme.themeSource = 'system';
if (originalSystemIsDark) await changePromise;
expect(await getPrefersColorSchemeIsDark(w)).to.equal(originalSystemIsDark);