attributes.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var op = require('../lib/op');
  2. describe('attributes', function () {
  3. describe('compose()', function () {
  4. var attributes = { bold: true, color: 'red' };
  5. it('left is undefined', function () {
  6. expect(op.attributes.compose(undefined, attributes)).toEqual(attributes);
  7. });
  8. it('right is undefined', function () {
  9. expect(op.attributes.compose(attributes, undefined)).toEqual(attributes);
  10. });
  11. it('both are undefined', function () {
  12. expect(op.attributes.compose(undefined, undefined)).toBe(undefined);
  13. });
  14. it('missing', function () {
  15. expect(op.attributes.compose(attributes, { italic: true })).toEqual({
  16. bold: true,
  17. italic: true,
  18. color: 'red'
  19. });
  20. });
  21. it('overwrite', function () {
  22. expect(op.attributes.compose(attributes, { bold: false, color: 'blue' })).toEqual({
  23. bold: false,
  24. color: 'blue'
  25. });
  26. });
  27. it('remove', function () {
  28. expect(op.attributes.compose(attributes, { bold: null })).toEqual({
  29. color: 'red'
  30. });
  31. });
  32. it('remove to none', function () {
  33. expect(op.attributes.compose(attributes, { bold: null, color: null })).toEqual(undefined);
  34. });
  35. it('remove missing', function () {
  36. expect(op.attributes.compose(attributes, { italic: null })).toEqual(attributes);
  37. });
  38. });
  39. describe('diff()', function () {
  40. var format = { bold: true, color: 'red' };
  41. it('left is undefined', function () {
  42. expect(op.attributes.diff(undefined, format)).toEqual(format);
  43. });
  44. it('right is undefined', function () {
  45. var expected = { bold: null, color: null };
  46. expect(op.attributes.diff(format, undefined)).toEqual(expected);
  47. });
  48. it('same format', function () {
  49. expect(op.attributes.diff(format, format)).toEqual(undefined);
  50. });
  51. it('add format', function () {
  52. var added = { bold: true, italic: true, color: 'red' };
  53. var expected = { italic: true };
  54. expect(op.attributes.diff(format, added)).toEqual(expected);
  55. });
  56. it('remove format', function () {
  57. var removed = { bold: true };
  58. var expected = { color: null };
  59. expect(op.attributes.diff(format, removed)).toEqual(expected);
  60. });
  61. it('overwrite format', function () {
  62. var overwritten = { bold: true, color: 'blue' };
  63. var expected = { color: 'blue' };
  64. expect(op.attributes.diff(format, overwritten)).toEqual(expected);
  65. });
  66. });
  67. describe('transform()', function () {
  68. var left = { bold: true, color: 'red', font: null };
  69. var right = { color: 'blue', font: 'serif', italic: true };
  70. it('left is undefined', function () {
  71. expect(op.attributes.transform(undefined, left, false)).toEqual(left);
  72. });
  73. it('right is undefined', function () {
  74. expect(op.attributes.transform(left, undefined, false)).toEqual(undefined);
  75. });
  76. it('both are undefined', function () {
  77. expect(op.attributes.transform(undefined, undefined, false)).toEqual(undefined);
  78. });
  79. it('with priority', function () {
  80. expect(op.attributes.transform(left, right, true)).toEqual({
  81. italic: true
  82. });
  83. });
  84. it('without priority', function () {
  85. expect(op.attributes.transform(left, right, false)).toEqual(right);
  86. });
  87. });
  88. });