window-helpers.ts 916 B

1234567891011121314151617181920212223242526272829303132
  1. import { expect } from 'chai'
  2. import { BrowserWindow, WebContents } from 'electron'
  3. import { emittedOnce } from './events-helpers';
  4. export const closeWindow = async (
  5. window: BrowserWindow | null = null,
  6. { assertNotWindows } = { assertNotWindows: true }
  7. ) => {
  8. if (window && !window.isDestroyed()) {
  9. const isClosed = emittedOnce(window, 'closed')
  10. window.setClosable(true)
  11. window.close()
  12. await isClosed
  13. }
  14. if (assertNotWindows) {
  15. const windows = BrowserWindow.getAllWindows()
  16. for (const win of windows) {
  17. const closePromise = emittedOnce(win, 'closed')
  18. win.close()
  19. await closePromise
  20. }
  21. expect(windows).to.have.lengthOf(0)
  22. }
  23. }
  24. exports.waitForWebContentsToLoad = async (webContents: WebContents) => {
  25. const didFinishLoadPromise = emittedOnce(webContents, 'did-finish-load')
  26. if (webContents.isLoadingMainFrame()) {
  27. await didFinishLoadPromise
  28. }
  29. }