api-dialog-spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { expect } from 'chai';
  2. import { dialog, BrowserWindow } from 'electron/main';
  3. import { closeAllWindows } from './window-helpers';
  4. import { ifit } from './spec-helpers';
  5. describe('dialog module', () => {
  6. describe('showOpenDialog', () => {
  7. afterEach(closeAllWindows);
  8. ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
  9. expect(() => {
  10. dialog.showOpenDialog({ title: 'i am title' });
  11. }).to.not.throw();
  12. expect(() => {
  13. const w = new BrowserWindow();
  14. dialog.showOpenDialog(w, { title: 'i am title' });
  15. }).to.not.throw();
  16. });
  17. it('throws errors when the options are invalid', () => {
  18. expect(() => {
  19. dialog.showOpenDialog({ properties: false as any });
  20. }).to.throw(/Properties must be an array/);
  21. expect(() => {
  22. dialog.showOpenDialog({ title: 300 as any });
  23. }).to.throw(/Title must be a string/);
  24. expect(() => {
  25. dialog.showOpenDialog({ buttonLabel: [] as any });
  26. }).to.throw(/Button label must be a string/);
  27. expect(() => {
  28. dialog.showOpenDialog({ defaultPath: {} as any });
  29. }).to.throw(/Default path must be a string/);
  30. expect(() => {
  31. dialog.showOpenDialog({ message: {} as any });
  32. }).to.throw(/Message must be a string/);
  33. });
  34. });
  35. describe('showSaveDialog', () => {
  36. afterEach(closeAllWindows);
  37. ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
  38. expect(() => {
  39. dialog.showSaveDialog({ title: 'i am title' });
  40. }).to.not.throw();
  41. expect(() => {
  42. const w = new BrowserWindow();
  43. dialog.showSaveDialog(w, { title: 'i am title' });
  44. }).to.not.throw();
  45. });
  46. it('throws errors when the options are invalid', () => {
  47. expect(() => {
  48. dialog.showSaveDialog({ title: 300 as any });
  49. }).to.throw(/Title must be a string/);
  50. expect(() => {
  51. dialog.showSaveDialog({ buttonLabel: [] as any });
  52. }).to.throw(/Button label must be a string/);
  53. expect(() => {
  54. dialog.showSaveDialog({ defaultPath: {} as any });
  55. }).to.throw(/Default path must be a string/);
  56. expect(() => {
  57. dialog.showSaveDialog({ message: {} as any });
  58. }).to.throw(/Message must be a string/);
  59. expect(() => {
  60. dialog.showSaveDialog({ nameFieldLabel: {} as any });
  61. }).to.throw(/Name field label must be a string/);
  62. });
  63. });
  64. describe('showMessageBox', () => {
  65. afterEach(closeAllWindows);
  66. // parentless message boxes are synchronous on macOS
  67. // dangling message boxes on windows cause a DCHECK: https://cs.chromium.org/chromium/src/base/win/message_window.cc?l=68&rcl=7faa4bf236a866d007dc5672c9ce42660e67a6a6
  68. ifit(process.platform !== 'darwin' && process.platform !== 'win32')('should not throw for a parentless message box', () => {
  69. expect(() => {
  70. dialog.showMessageBox({ message: 'i am message' });
  71. }).to.not.throw();
  72. });
  73. ifit(process.platform !== 'win32')('should not throw for valid cases', () => {
  74. expect(() => {
  75. const w = new BrowserWindow();
  76. dialog.showMessageBox(w, { message: 'i am message' });
  77. }).to.not.throw();
  78. });
  79. it('throws errors when the options are invalid', () => {
  80. expect(() => {
  81. dialog.showMessageBox(undefined as any, { type: 'not-a-valid-type', message: '' });
  82. }).to.throw(/Invalid message box type/);
  83. expect(() => {
  84. dialog.showMessageBox(null as any, { buttons: false as any, message: '' });
  85. }).to.throw(/Buttons must be an array/);
  86. expect(() => {
  87. dialog.showMessageBox({ title: 300 as any, message: '' });
  88. }).to.throw(/Title must be a string/);
  89. expect(() => {
  90. dialog.showMessageBox({ message: [] as any });
  91. }).to.throw(/Message must be a string/);
  92. expect(() => {
  93. dialog.showMessageBox({ detail: 3.14 as any, message: '' });
  94. }).to.throw(/Detail must be a string/);
  95. expect(() => {
  96. dialog.showMessageBox({ checkboxLabel: false as any, message: '' });
  97. }).to.throw(/checkboxLabel must be a string/);
  98. });
  99. });
  100. describe('showErrorBox', () => {
  101. it('throws errors when the options are invalid', () => {
  102. expect(() => {
  103. (dialog.showErrorBox as any)();
  104. }).to.throw(/Insufficient number of arguments/);
  105. expect(() => {
  106. dialog.showErrorBox(3 as any, 'four');
  107. }).to.throw(/Error processing argument at index 0/);
  108. expect(() => {
  109. dialog.showErrorBox('three', 4 as any);
  110. }).to.throw(/Error processing argument at index 1/);
  111. });
  112. });
  113. describe('showCertificateTrustDialog', () => {
  114. it('throws errors when the options are invalid', () => {
  115. expect(() => {
  116. (dialog.showCertificateTrustDialog as any)();
  117. }).to.throw(/options must be an object/);
  118. expect(() => {
  119. dialog.showCertificateTrustDialog({} as any);
  120. }).to.throw(/certificate must be an object/);
  121. expect(() => {
  122. dialog.showCertificateTrustDialog({ certificate: {} as any, message: false as any });
  123. }).to.throw(/message must be a string/);
  124. });
  125. });
  126. });