-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- move canvas tests out of text_truncate - update existing method of mocking canvas width, since the new `testenv` file now allows bypassing setting `HTMLCanvasElement`
- Loading branch information
Showing
2 changed files
with
64 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { CanvasTextUtils } from './canvas_text_utils'; | ||
|
||
let mockCanvasWidth = 0; | ||
Object.defineProperty(HTMLCanvasElement.prototype, 'getContext', { | ||
value: () => ({ measureText: () => ({ width: mockCanvasWidth }), font: '' }), | ||
}); | ||
|
||
describe('CanvasTextUtils', () => { | ||
describe('font calculations', () => { | ||
it('computes the set font if passed a container element', () => { | ||
const container = document.createElement('div'); | ||
container.style.font = '14px Inter'; | ||
|
||
const utils = new CanvasTextUtils({ container }); | ||
expect(utils.context.font).toEqual('14px Inter'); | ||
}); | ||
|
||
it('accepts a static font string', () => { | ||
const utils = new CanvasTextUtils({ font: '14px Inter' }); | ||
expect(utils.context.font).toEqual('14px Inter'); | ||
}); | ||
}); | ||
|
||
describe('text width utils', () => { | ||
const utils = new CanvasTextUtils({ font: '' }); | ||
|
||
describe('textWidth', () => { | ||
it('returns the measured text width from the canvas', () => { | ||
mockCanvasWidth = 200; | ||
expect(utils.textWidth).toEqual(200); | ||
}); | ||
}); | ||
|
||
describe('setTextToCheck', () => { | ||
it('sets the internal currentText variable', () => { | ||
utils.setTextToCheck('hello world'); | ||
expect(utils.currentText).toEqual('hello world'); | ||
}); | ||
}); | ||
}); | ||
}); |