notification.js 5.0 KB

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