api-auto-updater-spec.ts 3.7 KB

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