api-dialog-spec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const { expect } = require('chai')
  2. const { closeWindow } = require('./window-helpers')
  3. const { remote } = require('electron')
  4. const { BrowserWindow, dialog } = remote
  5. const isCI = remote.getGlobal('isCi')
  6. describe('dialog module', () => {
  7. describe('showOpenDialog', () => {
  8. it('should not throw for valid cases', () => {
  9. // Blocks the main process and can't be run in CI
  10. if (isCI) return
  11. let w
  12. expect(() => {
  13. dialog.showOpenDialog({ title: 'i am title' })
  14. }).to.not.throw()
  15. expect(() => {
  16. w = new BrowserWindow()
  17. dialog.showOpenDialog(w, { title: 'i am title' })
  18. }).to.not.throw()
  19. closeWindow(w).then(() => { w = null })
  20. })
  21. it('throws errors when the options are invalid', () => {
  22. expect(() => {
  23. dialog.showOpenDialog({ properties: false })
  24. }).to.throw(/Properties must be an array/)
  25. expect(() => {
  26. dialog.showOpenDialog({ title: 300 })
  27. }).to.throw(/Title must be a string/)
  28. expect(() => {
  29. dialog.showOpenDialog({ buttonLabel: [] })
  30. }).to.throw(/Button label must be a string/)
  31. expect(() => {
  32. dialog.showOpenDialog({ defaultPath: {} })
  33. }).to.throw(/Default path must be a string/)
  34. expect(() => {
  35. dialog.showOpenDialog({ message: {} })
  36. }).to.throw(/Message must be a string/)
  37. })
  38. })
  39. describe('showSaveDialog', () => {
  40. it('should not throw for valid cases', () => {
  41. // Blocks the main process and can't be run in CI
  42. if (isCI) return
  43. let w
  44. expect(() => {
  45. dialog.showSaveDialog({ title: 'i am title' })
  46. }).to.not.throw()
  47. expect(() => {
  48. w = new BrowserWindow()
  49. dialog.showSaveDialog(w, { title: 'i am title' })
  50. }).to.not.throw()
  51. closeWindow(w).then(() => { w = null })
  52. })
  53. it('throws errors when the options are invalid', () => {
  54. expect(() => {
  55. dialog.showSaveDialog({ title: 300 })
  56. }).to.throw(/Title must be a string/)
  57. expect(() => {
  58. dialog.showSaveDialog({ buttonLabel: [] })
  59. }).to.throw(/Button label must be a string/)
  60. expect(() => {
  61. dialog.showSaveDialog({ defaultPath: {} })
  62. }).to.throw(/Default path must be a string/)
  63. expect(() => {
  64. dialog.showSaveDialog({ message: {} })
  65. }).to.throw(/Message must be a string/)
  66. expect(() => {
  67. dialog.showSaveDialog({ nameFieldLabel: {} })
  68. }).to.throw(/Name field label must be a string/)
  69. })
  70. })
  71. describe('showMessageBox', () => {
  72. it('should not throw for valid cases', () => {
  73. // Blocks the main process and can't be run in CI
  74. if (isCI) return
  75. let w
  76. expect(() => {
  77. dialog.showMessageBox({ title: 'i am title' })
  78. }).to.not.throw()
  79. expect(() => {
  80. w = new BrowserWindow()
  81. dialog.showMessageBox(w, { title: 'i am title' })
  82. }).to.not.throw()
  83. closeWindow(w).then(() => { w = null })
  84. })
  85. it('throws errors when the options are invalid', () => {
  86. expect(() => {
  87. dialog.showMessageBox(undefined, { type: 'not-a-valid-type' })
  88. }).to.throw(/Invalid message box type/)
  89. expect(() => {
  90. dialog.showMessageBox(null, { buttons: false })
  91. }).to.throw(/Buttons must be an array/)
  92. expect(() => {
  93. dialog.showMessageBox({ title: 300 })
  94. }).to.throw(/Title must be a string/)
  95. expect(() => {
  96. dialog.showMessageBox({ message: [] })
  97. }).to.throw(/Message must be a string/)
  98. expect(() => {
  99. dialog.showMessageBox({ detail: 3.14 })
  100. }).to.throw(/Detail must be a string/)
  101. expect(() => {
  102. dialog.showMessageBox({ checkboxLabel: false })
  103. }).to.throw(/checkboxLabel must be a string/)
  104. })
  105. })
  106. describe('showErrorBox', () => {
  107. it('throws errors when the options are invalid', () => {
  108. expect(() => {
  109. dialog.showErrorBox()
  110. }).to.throw(/Insufficient number of arguments/)
  111. expect(() => {
  112. dialog.showErrorBox(3, 'four')
  113. }).to.throw(/Error processing argument at index 0/)
  114. expect(() => {
  115. dialog.showErrorBox('three', 4)
  116. }).to.throw(/Error processing argument at index 1/)
  117. })
  118. })
  119. describe('showCertificateTrustDialog', () => {
  120. it('throws errors when the options are invalid', () => {
  121. expect(() => {
  122. dialog.showCertificateTrustDialog()
  123. }).to.throw(/options must be an object/)
  124. expect(() => {
  125. dialog.showCertificateTrustDialog({})
  126. }).to.throw(/certificate must be an object/)
  127. expect(() => {
  128. dialog.showCertificateTrustDialog({ certificate: {}, message: false })
  129. }).to.throw(/message must be a string/)
  130. })
  131. })
  132. })