Repository: electron/electron
File: spec/api-menu-spec.ts (TypeScript)

Code before:
        await setTimeout(500);
        menu.once('menu-will-close', failOnEvent);
        menu.popup({ window: w, x: 50, y: 50 });
        await setTimeout(1500);
        menu.off('menu-will-close', failOnEvent);
        menu.once('menu-will-close', () => done());
        menu.closePopup();
      });
      w.show();
    });

    it('does not crash when rendering menu item with single accelerator combinations', async () => {
      const chunkSize = 10;
      for (let i = 0; i < singleModifierCombinations.length; i += chunkSize) {
        const chunk = singleModifierCombinations.slice(i, i + chunkSize);
        const menu = Menu.buildFromTemplate([
          ...chunk.map(combination => ({
            label: `Test ${combination}`,
            accelerator: combination
          }))
        ]);
        menu.popup({ window: w });
        menu.closePopup();
        await setTimeout(100);
      }
    });
  });

  describe('Menu.setApplicationMenu', () => {
    it('sets a menu', () => {
      const menu = Menu.buildFromTemplate([
        { label: '1' },
        { label: '2' }
      ]);

      Menu.setApplicationMenu(menu);
      expect(Menu.getApplicationMenu()).to.not.be.null('application menu');
    });

    // DISABLED-FIXME(nornagon): this causes the focus handling tests to fail
    it('unsets a menu with null', () => {
      Menu.setApplicationMenu(null);
      expect(Menu.getApplicationMenu()).to.be.null('application menu');
    });

    ifit(process.platform !== 'darwin')('does not override menu visibility on startup', async () => {
      const appPath = path.join(fixturesPath, 'api', 'test-menu-visibility');
      const appProcess = cp.spawn(process.execPath, [appPath]);

      let output = '';
      await new Promise<void>((resolve) => {

Review comment (bug):
I took a look at this to see if there is a better way to handle this instead of using a setTimeout.  If we separate each chunk out into its own test, each test gets its own window (which gets cleaned up at the end of the test) and therefore removes the need to wait for the popup to close - particularly because these tests are just looking for crashes, not testing to see if the popup closes.
```suggestion
    const chunkSize = 10;
    let chunkCount = 0;
    const totalChunks = Math.ceil(singleModifierCombinations.length / chunkSize);
    for (let i = 0; i < singleModifierCombinations.length; i += chunkSize) {
      const chunk = singleModifierCombinations.slice(i, i + chunkSize);
      it(`does not crash when rendering menu item with single accelerator combinations ${++chunkCount}/${totalChunks}`, async () => {
        const menu = Menu.buildFromTemplate([
          ...chunk.map(combination => ({
            label: `Test ${combination}`,
            accelerator: combination
          }))
        ]);
        menu.popup({ window: w });
        menu.closePopup();
      });
    }
```

Code after:
        await setTimeout(500);
        menu.once('menu-will-close', failOnEvent);
        menu.popup({ window: w, x: 50, y: 50 });
        await setTimeout(1500);
        menu.off('menu-will-close', failOnEvent);
        menu.once('menu-will-close', () => done());
        menu.closePopup();
      });
      w.show();
    });

    const chunkSize = 10;
    let chunkCount = 0;
    const totalChunks = Math.ceil(singleModifierCombinations.length / chunkSize);
    for (let i = 0; i < singleModifierCombinations.length; i += chunkSize) {
      const chunk = singleModifierCombinations.slice(i, i + chunkSize);
      it(`does not crash when rendering menu item with single accelerator combinations ${++chunkCount}/${totalChunks}`, async () => {
        const menu = Menu.buildFromTemplate([
          ...chunk.map(combination => ({
            label: `Test ${combination}`,
            accelerator: combination
          }))
        ]);
        menu.popup({ window: w });
        menu.closePopup();
      });
    }
  });

  describe('Menu.setApplicationMenu', () => {
    it('sets a menu', () => {
      const menu = Menu.buildFromTemplate([
        { label: '1' },
        { label: '2' }
      ]);

      Menu.setApplicationMenu(menu);
      expect(Menu.getApplicationMenu()).to.not.be.null('application menu');
    });

    // DISABLED-FIXME(nornagon): this causes the focus handling tests to fail
    it('unsets a menu with null', () => {
      Menu.setApplicationMenu(null);
      expect(Menu.getApplicationMenu()).to.be.null('application menu');
    });

    ifit(process.platform !== 'darwin')('does not override menu visibility on startup', async () => {
      const appPath = path.join(fixturesPath, 'api', 'test-menu-visibility');
      const appProcess = cp.spawn(process.execPath, [appPath]);

      let output = '';