| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
import { expect } from 'chai'; |
| 4 |
|
| 5 |
import { CellRenderer, GraphicsContext, TextRenderer } from '@lumino/datagrid'; |
| 6 |
|
| 7 |
class LoggingGraphicsContext extends GraphicsContext { |
| 8 |
fillText(text: string, x: number, y: number, maxWidth?: number): void { |
| 9 |
super.fillText(text, x, y, maxWidth); |
| 10 |
this.texts.push(text); |
| 11 |
} |
| 12 |
|
| 13 |
texts: string[] = []; |
| 14 |
} |
| 15 |
|
| 16 |
describe('@lumino/datagrid', () => { |
| 17 |
let gc: LoggingGraphicsContext; |
| 18 |
const defaultCellConfig: CellRenderer.CellConfig = { |
| 19 |
x: 0, |
| 20 |
y: 0, |
| 21 |
width: 100, |
| 22 |
height: 20, |
| 23 |
row: 0, |
| 24 |
column: 0, |
| 25 |
region: 'body', |
| 26 |
value: '', |
| 27 |
metadata: {} |
| 28 |
}; |
| 29 |
beforeEach(() => { |
| 30 |
const canvas = document.createElement('canvas'); |
| 31 |
const context = canvas.getContext('2d')!; |
| 32 |
gc = new LoggingGraphicsContext(context); |
| 33 |
}); |
| 34 |
describe('TextRenderer', () => { |
| 35 |
describe('drawText()', () => { |
| 36 |
it('should right-elide long strings', () => { |
| 37 |
const renderer = new TextRenderer({ |
| 38 |
elideDirection: 'right', |
| 39 |
font: '12px Arial' |
| 40 |
}); |
| 41 |
renderer.drawText(gc, { |
| 42 |
...defaultCellConfig, |
| 43 |
width: 100, |
| 44 |
value: 'this text exceeds 100px' |
| 45 |
}); |
| 46 |
expect(gc.texts.pop()).to.eq('this text exce…'); |
| 47 |
}); |
| 48 |
|
| 49 |
it('should left-elide long strings', () => { |
| 50 |
const renderer = new TextRenderer({ |
| 51 |
elideDirection: 'left', |
| 52 |
font: '12px Arial' |
| 53 |
}); |
| 54 |
renderer.drawText(gc, { |
| 55 |
...defaultCellConfig, |
| 56 |
width: 100, |
| 57 |
value: 'this text exceeds 100px' |
| 58 |
}); |
| 59 |
// Pixel calculations differ between Firefox and Chrome, either is good. |
| 60 |
expect(gc.texts.pop()).to.be.oneOf(['…xceeds 100px', '…ceeds 100px']); |
| 61 |
}); |
| 62 |
|
| 63 |
it('should not elide if eliding is disabled', () => { |
| 64 |
const renderer = new TextRenderer({ |
| 65 |
elideDirection: 'none', |
| 66 |
font: '12px Arial' |
| 67 |
}); |
| 68 |
renderer.drawText(gc, { |
| 69 |
...defaultCellConfig, |
| 70 |
width: 100, |
| 71 |
value: 'this text exceeds 100px' |
| 72 |
}); |
| 73 |
expect(gc.texts.pop()).to.eq('this text exceeds 100px'); |
| 74 |
}); |
| 75 |
|
| 76 |
it('should wrap text', () => { |
| 77 |
const renderer = new TextRenderer({ |
| 78 |
elideDirection: 'none', |
| 79 |
wrapText: true, |
| 80 |
font: '12px Arial' |
| 81 |
}); |
| 82 |
renderer.drawText(gc, { |
| 83 |
...defaultCellConfig, |
| 84 |
width: 100, |
| 85 |
value: 'this text exceeds 100px' |
| 86 |
}); |
| 87 |
expect(gc.texts.pop()).to.eq('exceeds 100px'); |
| 88 |
expect(gc.texts.pop()).to.eq('this text'); |
| 89 |
}); |
| 90 |
|
| 91 |
it('should not break up Unicode surrogate characters (left)', () => { |
| 92 |
const renderer = new TextRenderer({ |
| 93 |
elideDirection: 'left', |
| 94 |
font: '12px Arial' |
| 95 |
}); |
| 96 |
renderer.drawText(gc, { |
| 97 |
...defaultCellConfig, |
| 98 |
width: 45, |
| 99 |
value: '📦📦📦' |
| 100 |
}); |
| 101 |
expect(gc.texts.pop()).to.eq('…📦'); |
| 102 |
}); |
| 103 |
it('should not break up Unicode surrogate characters (right)', () => { |
| 104 |
const renderer = new TextRenderer({ |
| 105 |
elideDirection: 'right', |
| 106 |
font: '12px Arial' |
| 107 |
}); |
| 108 |
renderer.drawText(gc, { |
| 109 |
...defaultCellConfig, |
| 110 |
width: 45, |
| 111 |
value: '📦📦📦' |
| 112 |
}); |
| 113 |
expect(gc.texts.pop()).to.eq('📦…'); |
| 114 |
}); |
| 115 |
}); |
| 116 |
}); |
| 117 |
}); |