asar-spec.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { expect } from 'chai';
  2. import * as path from 'path';
  3. import { BrowserWindow, ipcMain } from 'electron/main';
  4. import { closeAllWindows } from './window-helpers';
  5. import { emittedOnce } from './events-helpers';
  6. describe('asar package', () => {
  7. const fixtures = path.join(__dirname, '..', 'spec', 'fixtures');
  8. const asarDir = path.join(fixtures, 'test.asar');
  9. afterEach(closeAllWindows);
  10. describe('asar protocol', () => {
  11. it('sets __dirname correctly', async function () {
  12. after(function () {
  13. ipcMain.removeAllListeners('dirname');
  14. });
  15. const w = new BrowserWindow({
  16. show: false,
  17. width: 400,
  18. height: 400,
  19. webPreferences: {
  20. nodeIntegration: true
  21. }
  22. });
  23. const p = path.resolve(asarDir, 'web.asar', 'index.html');
  24. const dirnameEvent = emittedOnce(ipcMain, 'dirname');
  25. w.loadFile(p);
  26. const [, dirname] = await dirnameEvent;
  27. expect(dirname).to.equal(path.dirname(p));
  28. });
  29. it('loads script tag in html', async function () {
  30. after(function () {
  31. ipcMain.removeAllListeners('ping');
  32. });
  33. const w = new BrowserWindow({
  34. show: false,
  35. width: 400,
  36. height: 400,
  37. webPreferences: {
  38. nodeIntegration: true
  39. }
  40. });
  41. const p = path.resolve(asarDir, 'script.asar', 'index.html');
  42. const ping = emittedOnce(ipcMain, 'ping');
  43. w.loadFile(p);
  44. const [, message] = await ping;
  45. expect(message).to.equal('pong');
  46. });
  47. it('loads video tag in html', async function () {
  48. this.timeout(60000);
  49. after(function () {
  50. ipcMain.removeAllListeners('asar-video');
  51. });
  52. const w = new BrowserWindow({
  53. show: false,
  54. width: 400,
  55. height: 400,
  56. webPreferences: {
  57. nodeIntegration: true
  58. }
  59. });
  60. const p = path.resolve(asarDir, 'video.asar', 'index.html');
  61. w.loadFile(p);
  62. const [, message, error] = await emittedOnce(ipcMain, 'asar-video');
  63. if (message === 'ended') {
  64. expect(error).to.be.null();
  65. } else if (message === 'error') {
  66. throw new Error(error);
  67. }
  68. });
  69. });
  70. });