summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/cli-table2/test/layout-manager-test.js
blob: 362f348e0edd7cd4809240df06e3d0ed11cd0bfa (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
describe('layout-manager',function(){
  var chai = require('chai');
  var expect = chai.expect;

  var layoutManager = require('../src/layout-manager');
  var layoutTable = layoutManager.layoutTable;
  var addRowSpanCells = layoutManager.addRowSpanCells;
  var maxWidth = layoutManager.maxWidth;
  var Cell = require('../src/cell');
  var RowSpanCell = Cell.RowSpanCell;

  describe('layoutTable',function(){
    it('sets x and y',function(){
      var table = [
        [{},{}],
        [{},{}]
      ];

      layoutTable(table);

      expect(table).to.eql([
        [{x:0,y:0},{x:1,y:0}],
        [{x:0,y:1},{x:1,y:1}]
      ]);

      var w = maxWidth(table);
      expect(w).to.equal(2);
    });

    it('colSpan will push x values to the right',function(){
      var table = [
        [{colSpan:2},{}],
        [{},{colSpan:2}]
      ];

      layoutTable(table);

      expect(table).to.eql([
        [{x:0,y:0,colSpan:2},{x:2,y:0}],
        [{x:0,y:1},{x:1,y:1,colSpan:2}]
      ]);

      expect(maxWidth(table)).to.equal(3);
    });

    it('rowSpan will push x values on cells below',function(){
      var table = [
        [{rowSpan:2},{}],
        [{}]
      ];

      layoutTable(table);

      expect(table).to.eql([
        [{x:0,y:0,rowSpan:2},{x:1,y:0}],
        [{x:1,y:1}]
      ]);

      expect(maxWidth(table)).to.equal(2);
    });

    it('colSpan and rowSpan together',function(){
      var table = [
        [{rowSpan:2,colSpan:2},{}],
        [{}]
      ];

      layoutTable(table);

      expect(table).to.eql([
        [{x:0,y:0,rowSpan:2,colSpan:2},{x:2,y:0}],
        [{x:2,y:1}]
      ]);

      expect(maxWidth(table)).to.equal(3);
    });

    it('complex layout',function(){

      var table = [
        [{c:'a'},{c:'b'},             {c:'c',rowSpan:3,colSpan:2}, {c:'d'}],
        [{c:'e',rowSpan:2,colSpan:2},                              {c:'f'}],
        [                                                          {c:'g'}]
      ];

      layoutTable(table);

      expect(table).to.eql([
        [{c:'a',y:0,x:0},    {c:'b',y:0,x:1}, {c:'c',y:0,x:2,rowSpan:3,colSpan:2}, {c:'d',y:0,x:4}],
        [{c:'e',rowSpan:2,colSpan:2,y:1,x:0},                                       {c:'f',y:1,x:4}],
        [{c:'g',y:2,x:4}]
      ]);

    });

    it('maxWidth of single element',function(){
      var table = [[{}]];
      layoutTable(table)
      expect(maxWidth(table)).to.equal(1);
    });
  });

  describe('addRowSpanCells',function(){
    it('will insert a rowSpan cell - beginning of line',function(){
      var table = [
        [{x:0,y:0,rowSpan:2},{x:1,y:0}],
        [{x:1,y:1}]
      ];

      addRowSpanCells(table);

      expect(table[0]).to.eql([{x:0,y:0,rowSpan:2},{x:1,y:0}]);
      expect(table[1].length).to.equal(2);
      expect(table[1][0]).to.be.instanceOf(RowSpanCell);
      expect(table[1][1]).to.eql({x:1,y:1});
    });

    it('will insert a rowSpan cell - end of line',function(){
      var table = [
        [{x:0,y:0},{x:1,y:0,rowSpan:2}],
        [{x:0,y:1}]
      ];

      addRowSpanCells(table);

      expect(table[0]).to.eql([{x:0,y:0},{rowSpan:2,x:1,y:0}]);
      expect(table[1].length).to.equal(2);
      expect(table[1][0]).to.eql({x:0,y:1});
      expect(table[1][1]).to.be.instanceOf(RowSpanCell);
    });

    it('will insert a rowSpan cell - middle of line',function(){
      var table = [
        [{x:0,y:0},{x:1,y:0,rowSpan:2},{x:2,y:0}],
        [{x:0,y:1},{x:2,y:1}]
      ];

      addRowSpanCells(table);

      expect(table[0]).to.eql([{x:0,y:0},{rowSpan:2,x:1,y:0},{x:2,y:0}]);
      expect(table[1].length).to.equal(3);
      expect(table[1][0]).to.eql({x:0,y:1});
      expect(table[1][1]).to.be.instanceOf(RowSpanCell);
      expect(table[1][2]).to.eql({x:2,y:1});
    });

    it('will insert a rowSpan cell - multiple on the same line',function(){
      var table = [
        [{x:0,y:0},{x:1,y:0,rowSpan:2},{x:2,y:0,rowSpan:2},{x:3,y:0}],
        [{x:0,y:1},{x:3,y:1}]
      ];

      addRowSpanCells(table);

      expect(table[0]).to.eql([{x:0,y:0},{rowSpan:2,x:1,y:0},{rowSpan:2,x:2,y:0},{x:3,y:0}]);
      expect(table[1].length).to.equal(4);
      expect(table[1][0]).to.eql({x:0,y:1});
      expect(table[1][1]).to.be.instanceOf(RowSpanCell);
      expect(table[1][2]).to.be.instanceOf(RowSpanCell);
      expect(table[1][3]).to.eql({x:3,y:1});
    });
  });
});