api-auto-updater-spec.ts 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { autoUpdater } from 'electron/main';
  2. import { expect } from 'chai';
  3. import { ifit, ifdescribe } from './lib/spec-helpers';
  4. import { once } from 'node:events';
  5. ifdescribe(!process.mas)('autoUpdater module', function () {
  6. describe('checkForUpdates', function () {
  7. ifit(process.platform === 'win32')('emits an error on Windows if the feed URL is not set', async function () {
  8. const errorEvent = once(autoUpdater, 'error') as Promise<[Error]>;
  9. autoUpdater.setFeedURL({ url: '' });
  10. autoUpdater.checkForUpdates();
  11. const [error] = await errorEvent;
  12. expect(error.message).to.equal('Update URL is not set');
  13. });
  14. });
  15. describe('getFeedURL', () => {
  16. it('returns an empty string by default', () => {
  17. expect(autoUpdater.getFeedURL()).to.equal('');
  18. });
  19. ifit(process.platform === 'win32')('correctly fetches the previously set FeedURL', function () {
  20. const updateURL = 'https://fake-update.electron.io';
  21. autoUpdater.setFeedURL({ url: updateURL });
  22. expect(autoUpdater.getFeedURL()).to.equal(updateURL);
  23. });
  24. });
  25. describe('setFeedURL', function () {
  26. ifdescribe(process.platform === 'win32' || process.platform === 'darwin')('on Mac or Windows', () => {
  27. it('sets url successfully using old (url, headers) syntax', () => {
  28. const url = 'http://electronjs.org';
  29. try {
  30. (autoUpdater.setFeedURL as any)(url, { header: 'val' });
  31. } catch { /* ignore */ }
  32. expect(autoUpdater.getFeedURL()).to.equal(url);
  33. });
  34. it('throws if no url is provided when using the old style', () => {
  35. expect(() => (autoUpdater.setFeedURL as any)()).to.throw('Expected an options object with a \'url\' property to be provided');
  36. });
  37. it('sets url successfully using new ({ url }) syntax', () => {
  38. const url = 'http://mymagicurl.local';
  39. try {
  40. autoUpdater.setFeedURL({ url });
  41. } catch { /* ignore */ }
  42. expect(autoUpdater.getFeedURL()).to.equal(url);
  43. });
  44. it('throws if no url is provided when using the new style', () => {
  45. expect(() => autoUpdater.setFeedURL({ noUrl: 'lol' } as any)
  46. ).to.throw('Expected options object to contain a \'url\' string property in setFeedUrl call');
  47. });
  48. });
  49. ifdescribe(process.platform === 'darwin' && process.arch !== 'arm64')('on Mac', function () {
  50. it('emits an error when the application is unsigned', async () => {
  51. const errorEvent = once(autoUpdater, 'error') as Promise<[Error]>;
  52. autoUpdater.setFeedURL({ url: '' });
  53. const [error] = await errorEvent;
  54. expect(error.message).equal('Could not get code signature for running application');
  55. });
  56. it('does not throw if default is the serverType', () => {
  57. // "Could not get code signature..." means the function got far enough to validate that serverType was OK.
  58. expect(() => autoUpdater.setFeedURL({ url: '', serverType: 'default' })).to.throw('Could not get code signature for running application');
  59. });
  60. it('does not throw if json is the serverType', () => {
  61. // "Could not get code signature..." means the function got far enough to validate that serverType was OK.
  62. expect(() => autoUpdater.setFeedURL({ url: '', serverType: 'json' })).to.throw('Could not get code signature for running application');
  63. });
  64. it('does throw if an unknown string is the serverType', () => {
  65. expect(() => autoUpdater.setFeedURL({ url: '', serverType: 'weow' as any })).to.throw('Expected serverType to be \'default\' or \'json\'');
  66. });
  67. });
  68. });
  69. describe('quitAndInstall', () => {
  70. ifit(process.platform === 'win32')('emits an error on Windows when no update is available', async function () {
  71. const errorEvent = once(autoUpdater, 'error') as Promise<[Error]>;
  72. autoUpdater.quitAndInstall();
  73. const [error] = await errorEvent;
  74. expect(error.message).to.equal('No update available, can\'t quit and install');
  75. });
  76. });
  77. });