|
| 1 | +import {afterEach, describe, expect, it} from 'vitest'; |
| 2 | +import {JSDOM} from 'jsdom'; |
| 3 | +import Parser from '../../src/Parser.js'; |
| 4 | +import SvgDrawer from '../../src/SvgDrawer.js'; |
| 5 | +import SvgWrapper from '../../src/SvgWrapper.js'; |
| 6 | + |
| 7 | +let restoreGetContext = null; |
| 8 | + |
| 9 | +function setupDom() { |
| 10 | + const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>'); |
| 11 | + global.document = dom.window.document; |
| 12 | + global.window = dom.window; |
| 13 | + return dom; |
| 14 | +} |
| 15 | + |
| 16 | +function disableCanvasContext(dom) { |
| 17 | + const proto = dom.window.HTMLCanvasElement.prototype; |
| 18 | + const original = proto.getContext; |
| 19 | + proto.getContext = () => null; |
| 20 | + restoreGetContext = () => { |
| 21 | + proto.getContext = original; |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +afterEach(() => { |
| 26 | + if (restoreGetContext) { |
| 27 | + restoreGetContext(); |
| 28 | + restoreGetContext = null; |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +describe('SvgWrapper', () => { |
| 33 | + it('falls back to estimated text size when canvas is unavailable', () => { |
| 34 | + const dom = setupDom(); |
| 35 | + disableCanvasContext(dom); |
| 36 | + |
| 37 | + const dims = SvgWrapper.measureText('CH3', 11, 'Helvetica'); |
| 38 | + |
| 39 | + expect(dims.width).toBeGreaterThan(0); |
| 40 | + expect(dims.height).toBeGreaterThan(0); |
| 41 | + }); |
| 42 | + |
| 43 | + it('renders SVG without canvas text metrics support', () => { |
| 44 | + const dom = setupDom(); |
| 45 | + disableCanvasContext(dom); |
| 46 | + |
| 47 | + const svg = dom.window.document.createElementNS('http://www.w3.org/2000/svg', 'svg'); |
| 48 | + svg.setAttributeNS(null, 'id', 'test-svg'); |
| 49 | + dom.window.document.body.appendChild(svg); |
| 50 | + |
| 51 | + const tree = Parser.parse('N[C@@H](C)C(=O)O'); |
| 52 | + const drawer = new SvgDrawer({isomeric: true}); |
| 53 | + |
| 54 | + expect(() => drawer.draw(tree, svg, 'light', false)).not.toThrow(); |
| 55 | + expect(drawer.preprocessor.graph.vertices.length).toBeGreaterThan(0); |
| 56 | + }); |
| 57 | +}); |
0 commit comments