8
0

utils.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 document */
  6. import { getCsrfToken, getCookie, setCookie } from '../src/utils';
  7. describe( 'utils', () => {
  8. beforeEach( () => {
  9. clearCookie( 'ckCsrfToken' );
  10. } );
  11. describe( 'getCsrfToken', () => {
  12. let token;
  13. beforeEach( () => {
  14. token = getCsrfToken();
  15. } );
  16. it( 'should be saved in cookie', () => {
  17. expect( document.cookie.indexOf( `ckCsrfToken=${ token }` ) > -1 ).to.be.true;
  18. } );
  19. it( 'should have proper length', () => {
  20. expect( token.length ).to.equal( 40 );
  21. } );
  22. it( 'should produce same token for all cals', () => {
  23. expect( token ).to.equal( getCsrfToken() );
  24. } );
  25. } );
  26. describe( 'get/set cookie', () => {
  27. let cookieName, cookieValue;
  28. beforeEach( () => {
  29. cookieName = 'test-cookie-name';
  30. cookieValue = 'test-value' + Math.random();
  31. clearCookie( cookieName );
  32. } );
  33. describe( 'setCookie', () => {
  34. it( 'should set cookie', () => {
  35. setCookie( cookieName, cookieValue );
  36. expect( document.cookie.indexOf( `${ cookieName }=${ cookieValue }` ) > -1 ).to.be.true;
  37. } );
  38. } );
  39. describe( 'getCookie', () => {
  40. it( 'should get cookie', () => {
  41. document.cookie = encodeURIComponent( cookieName ) + '=' + encodeURIComponent( cookieValue ) + ';path=/';
  42. expect( getCookie( cookieName ) ).to.equal( cookieValue );
  43. } );
  44. it( 'should return null if cookie is not present', () => {
  45. expect( getCookie( cookieName ) ).to.be.null;
  46. } );
  47. it( 'should return empty cookie', () => {
  48. document.cookie = encodeURIComponent( cookieName ) + '=;path=/';
  49. expect( getCookie( cookieName ) ).to.equal( '' );
  50. } );
  51. } );
  52. } );
  53. } );
  54. function clearCookie( name ) {
  55. document.cookie = `${ name }=;expires=Thu, 01 Jan 1970 00:00:00 GMT`;
  56. }