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