notification.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Copyright (c) 2016, CKSource - Frederico Knabben. All rights reserved.
  3. */
  4. /* globals window */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import Notification from '../../src/notification/notification';
  9. describe( 'Notification', () => {
  10. let editor, notification;
  11. testUtils.createSinonSandbox();
  12. beforeEach( () => {
  13. return VirtualTestEditor
  14. .create( {
  15. plugins: [ Notification ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. notification = editor.plugins.get( Notification );
  20. } );
  21. } );
  22. describe( 'init()', () => {
  23. it( 'should create notification plugin', () => {
  24. expect( notification ).to.instanceof( Notification );
  25. expect( notification ).to.instanceof( Plugin );
  26. } );
  27. } );
  28. describe( 'showSuccess()', () => {
  29. it( 'should fire `show:success` event with given data', () => {
  30. const spy = testUtils.sinon.spy();
  31. notification.on( 'show:success', spy );
  32. notification.showSuccess( 'foo bar' );
  33. sinon.assert.calledOnce( spy );
  34. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  35. message: 'foo bar',
  36. type: 'success',
  37. title: ''
  38. } );
  39. } );
  40. it( 'should fire `show:success` event with additional namespace', () => {
  41. const spy = testUtils.sinon.spy();
  42. notification.on( 'show:success:something:else', spy );
  43. notification.showSuccess( 'foo bar', {
  44. namespace: 'something:else'
  45. } );
  46. sinon.assert.calledOnce( spy );
  47. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  48. message: 'foo bar',
  49. type: 'success',
  50. title: ''
  51. } );
  52. } );
  53. it( 'should fire `show:success` event with title', () => {
  54. const spy = testUtils.sinon.spy();
  55. notification.on( 'show:success', spy );
  56. notification.showSuccess( 'foo bar', {
  57. title: 'foo bar baz'
  58. } );
  59. sinon.assert.calledOnce( spy );
  60. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  61. message: 'foo bar',
  62. type: 'success',
  63. title: 'foo bar baz'
  64. } );
  65. } );
  66. } );
  67. describe( 'showInfo()', () => {
  68. it( 'should fire `show:info` event with given data', () => {
  69. const spy = testUtils.sinon.spy();
  70. notification.on( 'show:info', spy );
  71. notification.showInfo( 'foo bar' );
  72. sinon.assert.calledOnce( spy );
  73. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  74. message: 'foo bar',
  75. type: 'info',
  76. title: ''
  77. } );
  78. } );
  79. it( 'should fire `show:info` event with additional namespace', () => {
  80. const spy = testUtils.sinon.spy();
  81. notification.on( 'show:info:something:else', spy );
  82. notification.showInfo( 'foo bar', {
  83. namespace: 'something:else'
  84. } );
  85. sinon.assert.calledOnce( spy );
  86. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  87. message: 'foo bar',
  88. type: 'info',
  89. title: ''
  90. } );
  91. } );
  92. it( 'should fire `show:info` event with title', () => {
  93. const spy = testUtils.sinon.spy();
  94. notification.on( 'show:info', spy );
  95. notification.showInfo( 'foo bar', {
  96. title: 'foo bar baz'
  97. } );
  98. sinon.assert.calledOnce( spy );
  99. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  100. message: 'foo bar',
  101. type: 'info',
  102. title: 'foo bar baz'
  103. } );
  104. } );
  105. } );
  106. describe( 'showWarning()', () => {
  107. let alertStub;
  108. beforeEach( () => {
  109. alertStub = testUtils.sinon.stub( window, 'alert' );
  110. } );
  111. it( 'should fire `show:warning` event with given data', () => {
  112. const spy = testUtils.sinon.spy();
  113. notification.on( 'show:warning', spy );
  114. notification.showWarning( 'foo bar' );
  115. sinon.assert.calledOnce( spy );
  116. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  117. message: 'foo bar',
  118. type: 'warning',
  119. title: ''
  120. } );
  121. } );
  122. it( 'should fire `show:warning` event with additional namespace', () => {
  123. const spy = testUtils.sinon.spy();
  124. notification.on( 'show:warning:something:else', spy );
  125. notification.showWarning( 'foo bar', {
  126. namespace: 'something:else'
  127. } );
  128. sinon.assert.calledOnce( spy );
  129. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  130. message: 'foo bar',
  131. type: 'warning',
  132. title: ''
  133. } );
  134. } );
  135. it( 'should fire `show:warning` event with title', () => {
  136. const spy = testUtils.sinon.spy();
  137. notification.on( 'show:warning', spy );
  138. notification.showWarning( 'foo bar', {
  139. title: 'foo bar baz'
  140. } );
  141. sinon.assert.calledOnce( spy );
  142. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  143. message: 'foo bar',
  144. type: 'warning',
  145. title: 'foo bar baz'
  146. } );
  147. } );
  148. it( 'should display `warning` message as system alert if is not cached and stopped by other plugins', () => {
  149. notification.showWarning( 'foo bar' );
  150. sinon.assert.calledOnce( alertStub );
  151. expect( alertStub.firstCall.args[ 0 ] ).to.equal( 'foo bar' );
  152. } );
  153. it( 'should not display alert when `warning` message is cached and stopped by other plugins', () => {
  154. notification.on( 'show:warning', evt => {
  155. evt.stop();
  156. } );
  157. notification.showWarning( 'foo bar' );
  158. sinon.assert.notCalled( alertStub );
  159. } );
  160. } );
  161. } );