api-notification-spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { expect } from 'chai';
  2. import { Notification } from 'electron/main';
  3. import { once } from 'events';
  4. import { ifit } from './lib/spec-helpers';
  5. describe('Notification module', () => {
  6. it('sets the correct class name on the prototype', () => {
  7. expect(Notification.prototype.constructor.name).to.equal('Notification');
  8. });
  9. it('is supported', () => {
  10. expect(Notification.isSupported()).to.be.a('boolean');
  11. });
  12. it('inits, gets and sets basic string properties correctly', () => {
  13. const n = new Notification({
  14. title: 'title',
  15. subtitle: 'subtitle',
  16. body: 'body',
  17. replyPlaceholder: 'replyPlaceholder',
  18. sound: 'sound',
  19. closeButtonText: 'closeButtonText'
  20. });
  21. expect(n.title).to.equal('title');
  22. n.title = 'title1';
  23. expect(n.title).to.equal('title1');
  24. expect(n.subtitle).equal('subtitle');
  25. n.subtitle = 'subtitle1';
  26. expect(n.subtitle).equal('subtitle1');
  27. expect(n.body).to.equal('body');
  28. n.body = 'body1';
  29. expect(n.body).to.equal('body1');
  30. expect(n.replyPlaceholder).to.equal('replyPlaceholder');
  31. n.replyPlaceholder = 'replyPlaceholder1';
  32. expect(n.replyPlaceholder).to.equal('replyPlaceholder1');
  33. expect(n.sound).to.equal('sound');
  34. n.sound = 'sound1';
  35. expect(n.sound).to.equal('sound1');
  36. expect(n.closeButtonText).to.equal('closeButtonText');
  37. n.closeButtonText = 'closeButtonText1';
  38. expect(n.closeButtonText).to.equal('closeButtonText1');
  39. });
  40. it('inits, gets and sets basic boolean properties correctly', () => {
  41. const n = new Notification({
  42. title: 'title',
  43. body: 'body',
  44. silent: true,
  45. hasReply: true
  46. });
  47. expect(n.silent).to.be.true('silent');
  48. n.silent = false;
  49. expect(n.silent).to.be.false('silent');
  50. expect(n.hasReply).to.be.true('has reply');
  51. n.hasReply = false;
  52. expect(n.hasReply).to.be.false('has reply');
  53. });
  54. it('inits, gets and sets actions correctly', () => {
  55. const n = new Notification({
  56. title: 'title',
  57. body: 'body',
  58. actions: [
  59. {
  60. type: 'button',
  61. text: '1'
  62. }, {
  63. type: 'button',
  64. text: '2'
  65. }
  66. ]
  67. });
  68. expect(n.actions.length).to.equal(2);
  69. expect(n.actions[0].type).to.equal('button');
  70. expect(n.actions[0].text).to.equal('1');
  71. expect(n.actions[1].type).to.equal('button');
  72. expect(n.actions[1].text).to.equal('2');
  73. n.actions = [
  74. {
  75. type: 'button',
  76. text: '3'
  77. }, {
  78. type: 'button',
  79. text: '4'
  80. }
  81. ];
  82. expect(n.actions.length).to.equal(2);
  83. expect(n.actions[0].type).to.equal('button');
  84. expect(n.actions[0].text).to.equal('3');
  85. expect(n.actions[1].type).to.equal('button');
  86. expect(n.actions[1].text).to.equal('4');
  87. });
  88. it('can be shown and closed', () => {
  89. const n = new Notification({
  90. title: 'test notification',
  91. body: 'test body',
  92. silent: true
  93. });
  94. n.show();
  95. n.close();
  96. });
  97. ifit(process.platform === 'win32')('inits, gets and sets custom xml', () => {
  98. const n = new Notification({
  99. toastXml: '<xml/>'
  100. });
  101. expect(n.toastXml).to.equal('<xml/>');
  102. });
  103. ifit(process.platform === 'darwin')('emits show and close events', async () => {
  104. const n = new Notification({
  105. title: 'test notification',
  106. body: 'test body',
  107. silent: true
  108. });
  109. {
  110. const e = once(n, 'show');
  111. n.show();
  112. await e;
  113. }
  114. {
  115. const e = once(n, 'close');
  116. n.close();
  117. await e;
  118. }
  119. });
  120. ifit(process.platform === 'win32')('emits failed event', async () => {
  121. const n = new Notification({
  122. toastXml: 'not xml'
  123. });
  124. {
  125. const e = once(n, 'failed');
  126. n.show();
  127. await e;
  128. }
  129. });
  130. // TODO(sethlu): Find way to test init with notification icon?
  131. });