move mocks specific to only seq spec files out of global d3 mock

This commit is contained in:
Ashley Engelund (weedySeaDragon @ github)
2022-11-17 12:24:16 -08:00
parent 8a3c4f64b2
commit 1fc02940ae
2 changed files with 81 additions and 1 deletions

View File

@@ -1,8 +1,62 @@
import { vi } from 'vitest';
import * as configApi from '../../config';
import mermaidAPI from '../../mermaidAPI';
import Diagram from '../../Diagram';
import { addDiagrams } from '../../diagram-api/diagram-orchestration';
/**
* Sequence diagrams require their own very special version of a mocked d3 module
* diagrams/sequence/svgDraw uses statements like this with d3 nodes: (note the [0][0])
*
* // in drawText(...)
* textHeight += (textElem._groups || textElem)[0][0].getBBox().height;
*/
vi.mock('d3', () => {
const NewD3 = function () {
function returnThis() {
return this;
}
return {
append: function () {
return NewD3();
},
lower: returnThis,
attr: returnThis,
style: returnThis,
text: returnThis,
// [0][0] (below) is required by drawText() in packages/mermaid/src/diagrams/sequence/svgDraw.js
0: {
0: {
getBBox: function () {
return {
height: 10,
width: 20,
};
},
},
},
};
};
return {
select: function () {
return new NewD3();
},
selectAll: function () {
return new NewD3();
},
curveBasis: 'basis',
curveLinear: 'linear',
curveCardinal: 'cardinal',
};
});
// -------------------------------
addDiagrams();
/**
* @param conf
* @param key

View File

@@ -1,5 +1,31 @@
import { vi } from 'vitest';
import svgDraw from './svgDraw';
import { MockD3 } from 'd3';
// This is the only place that uses this mock
export const MockD3 = (name, parent) => {
const children = [];
const elem = {
get __children() {
return children;
},
get __name() {
return name;
},
get __parent() {
return parent;
},
};
elem.append = (name) => {
const mockElem = MockD3(name, elem);
children.push(mockElem);
return mockElem;
};
elem.lower = vi.fn(() => elem);
elem.attr = vi.fn(() => elem);
elem.text = vi.fn(() => elem);
elem.style = vi.fn(() => elem);
return elem;
};
describe('svgDraw', function () {
describe('drawRect', function () {