notification.js 5.1 KB

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