use MockedD3, spies in util insertTitle spec (remove MockD3)

This commit is contained in:
Ashley Engelund (weedySeaDragon @ github) 2022-11-27 10:14:11 -08:00
parent 7508cd796d
commit f1bc2deafd

View File

@ -4,7 +4,8 @@ import assignWithDepth from './assignWithDepth';
import { detectType } from './diagram-api/detectType';
import { addDiagrams } from './diagram-api/diagram-orchestration';
import memoize from 'lodash-es/memoize';
import { MockD3 } from 'd3';
import { MockedD3 } from './tests/MockedD3';
addDiagrams();
describe('when assignWithDepth: should merge objects within objects', function () {
@ -352,21 +353,52 @@ describe('when initializing the id generator', function () {
});
describe('when inserting titles', function () {
it('should do nothing when title is empty', function () {
const svg = MockD3('svg');
utils.insertTitle(svg, 'testClass', 0, '');
expect(svg.__children.length).toBe(0);
const svg = new MockedD3('svg');
const mockedElement = {
getBBox: vi.fn().mockReturnValue({ x: 10, y: 11, width: 100, height: 200 }),
};
const fauxTitle = new MockedD3('title');
beforeEach(() => {
svg.node = vi.fn().mockReturnValue(mockedElement);
});
it('should insert title centered', function () {
const svg = MockD3('svg');
it('does nothing if the title is empty', function () {
const svg_append_spy = vi.spyOn(svg, 'append');
utils.insertTitle(svg, 'testClass', 0, '');
expect(svg_append_spy).not.toHaveBeenCalled();
});
it('appends the title as a text item with the given title text', function () {
const svg_append_spy = vi.spyOn(svg, 'append').mockReturnValue(fauxTitle);
const title_text_spy = vi.spyOn(fauxTitle, 'text');
utils.insertTitle(svg, 'testClass', 5, 'test title');
expect(svg.__children.length).toBe(1);
const text = svg.__children[0];
expect(text.__name).toBe('text');
expect(text.text).toHaveBeenCalledWith('test title');
expect(text.attr).toHaveBeenCalledWith('x', 15);
expect(text.attr).toHaveBeenCalledWith('y', -5);
expect(text.attr).toHaveBeenCalledWith('class', 'testClass');
expect(svg_append_spy).toHaveBeenCalled();
expect(title_text_spy).toHaveBeenCalledWith('test title');
});
it('x value is the bounds x position + half of the bounds width', () => {
vi.spyOn(svg, 'append').mockReturnValue(fauxTitle);
const title_attr_spy = vi.spyOn(fauxTitle, 'attr');
utils.insertTitle(svg, 'testClass', 5, 'test title');
expect(title_attr_spy).toHaveBeenCalledWith('x', 10 + 100 / 2);
});
it('y value is - given title top margin', () => {
vi.spyOn(svg, 'append').mockReturnValue(fauxTitle);
const title_attr_spy = vi.spyOn(fauxTitle, 'attr');
utils.insertTitle(svg, 'testClass', 5, 'test title');
expect(title_attr_spy).toHaveBeenCalledWith('y', -5);
});
it('class is the given css class', () => {
vi.spyOn(svg, 'append').mockReturnValue(fauxTitle);
const title_attr_spy = vi.spyOn(fauxTitle, 'attr');
utils.insertTitle(svg, 'testClass', 5, 'test title');
expect(title_attr_spy).toHaveBeenCalledWith('class', 'testClass');
});
});