api-auto-updater-spec.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. const { autoUpdater } = require('electron').remote
  2. const { ipcRenderer } = require('electron')
  3. const { expect } = require('chai')
  4. describe('autoUpdater module', function () {
  5. // XXX(alexeykuzmin): Calling `.skip()` in a 'before' hook
  6. // doesn't affect nested 'describe's
  7. beforeEach(function () {
  8. // Skip autoUpdater tests in MAS build.
  9. if (process.mas) {
  10. this.skip()
  11. }
  12. })
  13. describe('checkForUpdates', function () {
  14. it('emits an error on Windows when called the feed URL is not set', function (done) {
  15. if (process.platform !== 'win32') {
  16. // FIXME(alexeykuzmin): Skip the test.
  17. // this.skip()
  18. return done()
  19. }
  20. ipcRenderer.once('auto-updater-error', (event, message) => {
  21. expect(message).to.equal('Update URL is not set')
  22. done()
  23. })
  24. autoUpdater.setFeedURL('')
  25. autoUpdater.checkForUpdates()
  26. })
  27. })
  28. describe('getFeedURL', () => {
  29. it('returns a falsey value by default', () => {
  30. expect(autoUpdater.getFeedURL()).to.equal('')
  31. })
  32. it('correctly fetches the previously set FeedURL', function (done) {
  33. if (process.platform !== 'win32') {
  34. // FIXME(alexeykuzmin): Skip the test.
  35. // this.skip()
  36. return done()
  37. }
  38. const updateURL = 'https://fake-update.electron.io'
  39. autoUpdater.setFeedURL(updateURL)
  40. expect(autoUpdater.getFeedURL()).to.equal(updateURL)
  41. done()
  42. })
  43. })
  44. describe('setFeedURL', function () {
  45. describe('on Mac or Windows', () => {
  46. const noThrow = (fn) => {
  47. try { fn() } catch (err) {}
  48. }
  49. before(function () {
  50. if (process.platform !== 'win32' && process.platform !== 'darwin') {
  51. this.skip()
  52. }
  53. })
  54. it('sets url successfully using old (url, headers) syntax', () => {
  55. const url = 'http://electronjs.org'
  56. noThrow(() => autoUpdater.setFeedURL(url, { header: 'val' }))
  57. expect(autoUpdater.getFeedURL()).to.equal(url)
  58. })
  59. it('throws if no url is provided when using the old style', () => {
  60. expect(() => autoUpdater.setFeedURL(),
  61. err => err.message.includes('Expected an options object with a \'url\' property to be provided') // eslint-disable-line
  62. ).to.throw()
  63. })
  64. it('sets url successfully using new ({ url }) syntax', () => {
  65. const url = 'http://mymagicurl.local'
  66. noThrow(() => autoUpdater.setFeedURL({ url }))
  67. expect(autoUpdater.getFeedURL()).to.equal(url)
  68. })
  69. it('throws if no url is provided when using the new style', () => {
  70. expect(() => autoUpdater.setFeedURL({ noUrl: 'lol' }),
  71. err => err.message.includes('Expected options object to contain a \'url\' string property in setFeedUrl call') // eslint-disable-line
  72. ).to.throw()
  73. })
  74. })
  75. describe('on Mac', function () {
  76. const isServerTypeError = (err) => err.message.includes('Expected serverType to be \'default\' or \'json\'')
  77. before(function () {
  78. if (process.platform !== 'darwin') {
  79. this.skip()
  80. }
  81. })
  82. it('emits an error when the application is unsigned', done => {
  83. ipcRenderer.once('auto-updater-error', (event, message) => {
  84. expect(message).equal('Could not get code signature for running application')
  85. done()
  86. })
  87. autoUpdater.setFeedURL('')
  88. })
  89. it('does not throw if default is the serverType', () => {
  90. expect(() => autoUpdater.setFeedURL({ url: '', serverType: 'default' }),
  91. isServerTypeError
  92. ).to.not.throw()
  93. })
  94. it('does not throw if json is the serverType', () => {
  95. expect(() => autoUpdater.setFeedURL({ url: '', serverType: 'default' }),
  96. isServerTypeError
  97. ).to.not.throw()
  98. })
  99. it('does throw if an unknown string is the serverType', () => {
  100. expect(() => autoUpdater.setFeedURL({ url: '', serverType: 'weow' }),
  101. isServerTypeError
  102. ).to.throw()
  103. })
  104. })
  105. })
  106. describe('quitAndInstall', () => {
  107. it('emits an error on Windows when no update is available', function (done) {
  108. if (process.platform !== 'win32') {
  109. // FIXME(alexeykuzmin): Skip the test.
  110. // this.skip()
  111. return done()
  112. }
  113. ipcRenderer.once('auto-updater-error', (event, message) => {
  114. expect(message).to.equal('No update available, can\'t quit and install')
  115. done()
  116. })
  117. autoUpdater.quitAndInstall()
  118. })
  119. })
  120. describe('error event', () => {
  121. it('serializes correctly over the remote module', function (done) {
  122. if (process.platform === 'linux') {
  123. // FIXME(alexeykuzmin): Skip the test.
  124. // this.skip()
  125. return done()
  126. }
  127. autoUpdater.once('error', error => {
  128. expect(error).to.be.an.instanceof(Error)
  129. expect(Object.getOwnPropertyNames(error)).to.deep.equal(['stack', 'message', 'name'])
  130. done()
  131. })
  132. autoUpdater.setFeedURL('')
  133. if (process.platform === 'win32') {
  134. autoUpdater.checkForUpdates()
  135. }
  136. })
  137. })
  138. })