position.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Position from '/ckeditor5/core/treeview/position.js';
  8. describe( 'Position', () => {
  9. const parentMock = {};
  10. describe( 'constructor', () => {
  11. it( 'should create element without attributes', () => {
  12. const elem = new Position( parentMock, 5 );
  13. expect( elem ).to.have.property( 'parent' ).that.equals( parentMock );
  14. expect( elem ).to.have.property( 'offset' ).that.equals( 5 );
  15. } );
  16. } );
  17. describe( 'getShiftedBy', () => {
  18. it( 'returns new instance with shifted offset', () => {
  19. const position = new Position( parentMock, 10 );
  20. const shifted = position.getShiftedBy( 12 );
  21. expect( shifted.offset ).to.equal( 22 );
  22. } );
  23. it( 'accepts negative values', () => {
  24. const position = new Position( parentMock, 10 );
  25. const shifted = position.getShiftedBy( -5 );
  26. expect( shifted.offset ).to.equal( 5 );
  27. } );
  28. it( 'prevents offset to be a negative value', () => {
  29. const position = new Position( parentMock, 10 );
  30. const shifted = position.getShiftedBy( -20 );
  31. expect( shifted.offset ).to.equal( 0 );
  32. } );
  33. } );
  34. describe( 'createFromPosition', () => {
  35. it( 'creates new Position with same parent and offset', () => {
  36. const offset = 50;
  37. const position = new Position( parentMock, offset );
  38. const newPosition = Position.createFromPosition( position );
  39. expect( position ).to.not.equal( newPosition );
  40. expect( position.offset ).to.equal( offset );
  41. expect( position.parent ).to.equal( parentMock );
  42. } );
  43. } );
  44. describe( 'isEqual', () => {
  45. it( 'should return true for same object', () => {
  46. const position = new Position( {}, 12 );
  47. expect( position.isEqual( position ) ).to.be.true;
  48. } );
  49. it( 'should return true for positions with same parent and offset', () => {
  50. const parentMock = {};
  51. const position1 = new Position( parentMock, 12 );
  52. const position2 = new Position( parentMock, 12 );
  53. expect( position1.isEqual( position2 ) ).to.be.true;
  54. } );
  55. it( 'should return false for positions with different parents', () => {
  56. const position1 = new Position( {}, 12 );
  57. const position2 = new Position( {}, 12 );
  58. expect( position1.isEqual( position2 ) ).to.be.false;
  59. } );
  60. it( 'should return false for positions with different positions', () => {
  61. const parentMock = {};
  62. const position1 = new Position( parentMock, 12 );
  63. const position2 = new Position( parentMock, 2 );
  64. expect( position1.isEqual( position2 ) ).to.be.false;
  65. } );
  66. } );
  67. } );