notification.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. } );
  37. } );
  38. it( 'should fire `show:success` event with additional namespace', () => {
  39. const spy = testUtils.sinon.spy();
  40. notification.on( 'show:success:something:else', spy );
  41. notification.showSuccess( 'foo bar', {
  42. namespace: 'something:else'
  43. } );
  44. sinon.assert.calledOnce( spy );
  45. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  46. message: 'foo bar',
  47. type: 'success'
  48. } );
  49. } );
  50. } );
  51. describe( 'showInfo()', () => {
  52. it( 'should fire `show:info` event with given data', () => {
  53. const spy = testUtils.sinon.spy();
  54. notification.on( 'show:info', spy );
  55. notification.showInfo( 'foo bar' );
  56. sinon.assert.calledOnce( spy );
  57. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  58. message: 'foo bar',
  59. type: 'info'
  60. } );
  61. } );
  62. it( 'should fire `show:info` event with additional namespace', () => {
  63. const spy = testUtils.sinon.spy();
  64. notification.on( 'show:info:something:else', spy );
  65. notification.showInfo( 'foo bar', {
  66. namespace: 'something:else'
  67. } );
  68. sinon.assert.calledOnce( spy );
  69. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  70. message: 'foo bar',
  71. type: 'info'
  72. } );
  73. } );
  74. } );
  75. describe( 'showWarning()', () => {
  76. let alertStub;
  77. beforeEach( () => {
  78. alertStub = testUtils.sinon.stub( window, 'alert' );
  79. } );
  80. it( 'should fire `show:warning` event with given data', () => {
  81. const spy = testUtils.sinon.spy();
  82. notification.on( 'show:warning', spy );
  83. notification.showWarning( 'foo bar' );
  84. sinon.assert.calledOnce( spy );
  85. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  86. message: 'foo bar',
  87. type: 'warning'
  88. } );
  89. } );
  90. it( 'should fire `show:warning` event with additional namespace', () => {
  91. const spy = testUtils.sinon.spy();
  92. notification.on( 'show:warning:something:else', spy );
  93. notification.showWarning( 'foo bar', {
  94. namespace: 'something:else'
  95. } );
  96. sinon.assert.calledOnce( spy );
  97. expect( spy.firstCall.args[ 1 ] ).to.deep.equal( {
  98. message: 'foo bar',
  99. type: 'warning'
  100. } );
  101. } );
  102. it( 'should display `warning` message as system alert if is not cached and stopped by other plugins', () => {
  103. notification.showWarning( 'foo bar' );
  104. sinon.assert.calledOnce( alertStub );
  105. expect( alertStub.firstCall.args[ 0 ] ).to.equal( 'foo bar' );
  106. } );
  107. it( 'should not display alert when `warning` message is cached and stopped by other plugins', () => {
  108. notification.on( 'show:warning', evt => {
  109. evt.stop();
  110. } );
  111. notification.showWarning( 'foo bar' );
  112. sinon.assert.notCalled( alertStub );
  113. } );
  114. } );
  115. } );