api-auto-updater-spec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. const assert = require('assert')
  2. const {autoUpdater} = require('electron').remote
  3. const {ipcRenderer} = require('electron')
  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', function (event, message) {
  21. assert.equal(message, 'Update URL is not set')
  22. done()
  23. })
  24. autoUpdater.setFeedURL('')
  25. autoUpdater.checkForUpdates()
  26. })
  27. })
  28. describe('getFeedURL', function () {
  29. it('returns a falsey value by default', function () {
  30. assert.ok(!autoUpdater.getFeedURL())
  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. assert.equal(autoUpdater.getFeedURL(), 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. noThrow(() => autoUpdater.setFeedURL('http://electronjs.org', { header: 'val' }))
  56. assert.equal(autoUpdater.getFeedURL(), 'http://electronjs.org')
  57. })
  58. it('throws if no url is provided when using the old style', () => {
  59. assert.throws(
  60. () => autoUpdater.setFeedURL(),
  61. err => err.message.includes('Expected an options object with a \'url\' property to be provided') // eslint-disable-line
  62. )
  63. })
  64. it('sets url successfully using new ({ url }) syntax', () => {
  65. noThrow(() => autoUpdater.setFeedURL({ url: 'http://mymagicurl.local' }))
  66. assert.equal(autoUpdater.getFeedURL(), 'http://mymagicurl.local')
  67. })
  68. it('throws if no url is provided when using the new style', () => {
  69. assert.throws(
  70. () => 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. )
  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', function (done) {
  83. ipcRenderer.once('auto-updater-error', function (event, message) {
  84. assert.equal(message, '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. assert.doesNotThrow(
  91. () => autoUpdater.setFeedURL({ url: '', serverType: 'default' }),
  92. isServerTypeError
  93. )
  94. })
  95. it('does not throw if json is the serverType', () => {
  96. assert.doesNotThrow(
  97. () => autoUpdater.setFeedURL({ url: '', serverType: 'default' }),
  98. isServerTypeError
  99. )
  100. })
  101. it('does throw if an unknown string is the serverType', () => {
  102. assert.throws(
  103. () => autoUpdater.setFeedURL({ url: '', serverType: 'weow' }),
  104. isServerTypeError
  105. )
  106. })
  107. })
  108. })
  109. describe('quitAndInstall', function () {
  110. it('emits an error on Windows when no update is available', function (done) {
  111. if (process.platform !== 'win32') {
  112. // FIXME(alexeykuzmin): Skip the test.
  113. // this.skip()
  114. return done()
  115. }
  116. ipcRenderer.once('auto-updater-error', function (event, message) {
  117. assert.equal(message, 'No update available, can\'t quit and install')
  118. done()
  119. })
  120. autoUpdater.quitAndInstall()
  121. })
  122. })
  123. describe('error event', function () {
  124. it('serializes correctly over the remote module', function (done) {
  125. if (process.platform === 'linux') {
  126. // FIXME(alexeykuzmin): Skip the test.
  127. // this.skip()
  128. return done()
  129. }
  130. autoUpdater.once('error', function (error) {
  131. assert.equal(error instanceof Error, true)
  132. assert.deepEqual(Object.getOwnPropertyNames(error), ['stack', 'message', 'name'])
  133. done()
  134. })
  135. autoUpdater.setFeedURL('')
  136. if (process.platform === 'win32') {
  137. autoUpdater.checkForUpdates()
  138. }
  139. })
  140. })
  141. })