5a1e892036fe2c2559e3f6cdc3e713f68e877ba5d037a5c57b96ed67ada48ebf6a551bdbeb8745b59ea0ead57ff7df885c9e30e4cee85bcc16006028a5fdc3 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {RecordTranslator, registerIdentity, getTranslator} from 'handsontable/utils/recordTranslator';
  2. import Handsontable from 'handsontable';
  3. describe('RecordTranslator', () => {
  4. it('should translate to visual row using hook system', () => {
  5. var hotMock = {
  6. runHooks: jasmine.createSpy().and.returnValue(54),
  7. };
  8. var t = new RecordTranslator(hotMock);
  9. expect(t.toVisualRow(12)).toBe(54);
  10. expect(hotMock.runHooks).toHaveBeenCalledWith('unmodifyRow', 12);
  11. });
  12. it('should translate to visual column using hook system', () => {
  13. var hotMock = {
  14. runHooks: jasmine.createSpy().and.returnValue(54),
  15. };
  16. var t = new RecordTranslator(hotMock);
  17. expect(t.toVisualColumn(12)).toBe(54);
  18. expect(hotMock.runHooks).toHaveBeenCalledWith('unmodifyCol', 12);
  19. });
  20. it('should translate to visual coordinates (as an object)', () => {
  21. var t = new RecordTranslator();
  22. spyOn(t, 'toVisualRow').and.returnValue(6);
  23. spyOn(t, 'toVisualColumn').and.returnValue(12);
  24. expect(t.toVisual({row: 3, column: 4})).toEqual({row: 6, column: 12});
  25. });
  26. it('should translate to visual coordinates (as an array)', () => {
  27. var t = new RecordTranslator();
  28. spyOn(t, 'toVisualRow').and.returnValue(6);
  29. spyOn(t, 'toVisualColumn').and.returnValue(12);
  30. expect(t.toVisual(3, 4)).toEqual([6, 12]);
  31. });
  32. it('should translate to physical row using hook system', () => {
  33. var hotMock = {
  34. runHooks: jasmine.createSpy().and.returnValue(54),
  35. };
  36. var t = new RecordTranslator(hotMock);
  37. expect(t.toPhysicalRow(12)).toBe(54);
  38. expect(hotMock.runHooks).toHaveBeenCalledWith('modifyRow', 12);
  39. });
  40. it('should translate to physical column using hook system', () => {
  41. var hotMock = {
  42. runHooks: jasmine.createSpy().and.returnValue(54),
  43. };
  44. var t = new RecordTranslator(hotMock);
  45. expect(t.toPhysicalColumn(12)).toBe(54);
  46. expect(hotMock.runHooks).toHaveBeenCalledWith('modifyCol', 12);
  47. });
  48. it('should translate to physical coordinates (as an object)', () => {
  49. var t = new RecordTranslator();
  50. spyOn(t, 'toPhysicalRow').and.returnValue(6);
  51. spyOn(t, 'toPhysicalColumn').and.returnValue(12);
  52. expect(t.toPhysical({row: 3, column: 4})).toEqual({row: 6, column: 12});
  53. });
  54. it('should translate to physical coordinates (as an array)', () => {
  55. var t = new RecordTranslator();
  56. spyOn(t, 'toPhysicalRow').and.returnValue(6);
  57. spyOn(t, 'toPhysicalColumn').and.returnValue(12);
  58. expect(t.toPhysical(3, 4)).toEqual([6, 12]);
  59. });
  60. it('should always return the same instance of RecordTranslator for Handsontable instance', () => {
  61. var hot = new Handsontable(document.createElement('div'));
  62. var translator = getTranslator(hot);
  63. expect(translator === getTranslator(hot)).toBe(true);
  64. });
  65. it('should throw error when identifier was not registered while retrieving translator', () => {
  66. expect(() => {
  67. getTranslator({});
  68. }).toThrow();
  69. });
  70. it('should always return the same instance of RecordTranslator for custom identifier', () => {
  71. var hot = new Handsontable(document.createElement('div'));
  72. var customIdentifier = {};
  73. registerIdentity(customIdentifier, hot);
  74. var translator = getTranslator(customIdentifier);
  75. expect(translator === getTranslator(customIdentifier)).toBe(true);
  76. });
  77. });