Skip to content

Commit

Permalink
Convert 'expectedFont' in jest extension 'withFont'
Browse files Browse the repository at this point in the history
  • Loading branch information
csouchet committed Oct 7, 2020
1 parent 190c064 commit 1587a56
Showing 1 changed file with 65 additions and 31 deletions.
96 changes: 65 additions & 31 deletions test/e2e/ExpectModelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,43 +133,77 @@ function withGeometry(this: MatcherContext, received: mxCell, expected: mxGeomet
};
}

expect.extend({
toBeCell,
withGeometry,
});
function withFont(this: MatcherContext, received: mxCell, expected: ExpectedFont): CustomMatcherResult {
if (expected) {
const style = bpmnVisualization.graph.getView().getState(received).style;

export function expectGeometry(cell: mxCell, geometry: mxGeometry): void {
const cellGeometry = cell.getGeometry();
expect(cellGeometry.x).toEqual(geometry.x);
expect(cellGeometry.y).toEqual(geometry.y);
expect(cellGeometry.width).toEqual(geometry.width);
expect(cellGeometry.height).toEqual(geometry.height);
expect(cellGeometry.points).toEqual(geometry.points);
}
const receivedFont = { fontStyle: style[mxConstants.STYLE_FONTSTYLE], fontFamily: style[mxConstants.STYLE_FONTFAMILY], fontSize: style[mxConstants.STYLE_FONTSIZE] };
const expectedFont = { fontStyle: getFontStyleValue(expected), fontFamily: expected.name, fontSize: expected.size };

export function expectFont(state: mxCellState, expectedFont: ExpectedFont): void {
if (expectedFont) {
if (expectedFont.isBold) {
expect(state.style[mxConstants.STYLE_FONTSTYLE]).toEqual(mxConstants.FONT_BOLD);
}
const pass = receivedFont === expectedFont;

if (expectedFont.isItalic) {
expect(state.style[mxConstants.STYLE_FONTSTYLE]).toEqual(mxConstants.FONT_ITALIC);
}
return {
message: pass
? () =>
this.utils.matcherHint('.not.withFont') +
'\n\n' +
`Expected font of the cell with id '${received.cell.id}' not to be equals to:\n` +
` ${this.utils.printExpected(expectedFont)}\n` +
`Received:\n` +
` ${this.utils.printReceived(receivedFont)}`
: () => {
const diffString = this.utils.diff(expectedFont, receivedFont, {
expand: this.expand,
});
return (
this.utils.matcherHint('.withFont') +
'\n\n' +
`Expected font of the cell with id '${received.cell.id}' to be equals to:\n` +
` ${this.utils.printExpected(expectedFont)}\n` +
`Received:\n` +
` ${this.utils.printReceived(receivedFont)}` +
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
);
},
pass,
};
}
}

if (expectedFont.isUnderline) {
expect(state.style[mxConstants.STYLE_FONTSTYLE]).toEqual(mxConstants.FONT_UNDERLINE);
}
function getFontStyleValue(expectedFont: ExpectedFont): number {
let value = 0;
if (expectedFont.isBold) {
value += mxConstants.FONT_BOLD;
}
if (expectedFont.isItalic) {
value += mxConstants.FONT_ITALIC;
}
if (expectedFont.isStrikeThrough) {
value += mxConstants.FONT_STRIKETHROUGH;
}
if (expectedFont.isUnderline) {
value += mxConstants.FONT_UNDERLINE;
}
return value;
}

if (expectedFont.isStrikeThrough) {
expect(state.style[mxConstants.STYLE_FONTSTYLE]).toEqual(mxConstants.FONT_STRIKETHROUGH);
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Matchers<R> {
toBeCell(): R;
withGeometry(geometry: mxGeometry): R;
withFont(font: ExpectedFont): R;
}

expect(state.style[mxConstants.STYLE_FONTFAMILY]).toEqual(expectedFont.name);
expect(state.style[mxConstants.STYLE_FONTSIZE]).toEqual(expectedFont.size);
}
}

expect.extend({
toBeCell,
withGeometry,
withFont,
});

export function expectModelNotContainCell(cellId: string): void {
expect(cellId).not.toBeCell();
}
Expand All @@ -180,7 +214,7 @@ export function expectModelContainsCell(cellId: string): mxCell {
}

export function expectModelContainsShape(cellId: string, modelElement: ExpectedShapeModelElement): mxCell {
const cell = expectModelContainsCell(cellId);
const cell: mxCell = expectModelContainsCell(cellId);
const parentId = modelElement.parentId;
if (parentId) {
expect(cell.parent.id).toEqual(parentId);
Expand All @@ -199,7 +233,7 @@ export function expectModelContainsShape(cellId: string, modelElement: ExpectedS
const styleShape = !modelElement.styleShape ? modelElement.kind : modelElement.styleShape;
expect(state.style[mxConstants.STYLE_SHAPE]).toEqual(styleShape);
expect(cell.value).toEqual(modelElement.label);
expectFont(state, modelElement.font);
expect(cell).withFont(modelElement.font);
return cell;
}

Expand All @@ -219,7 +253,7 @@ export function expectModelContainsEdge(cellId: string, modelElement: ExpectedEd
if (modelElement.startArrow || modelElement.font) {
const state = bpmnVisualization.graph.getView().getState(cell);
expect(state.style[mxConstants.STYLE_STARTARROW]).toEqual(modelElement.startArrow);
expectFont(state, modelElement.font);
expect(cell).withFont(modelElement.font);
}

expect(cell.value).toEqual(modelElement.label);
Expand Down

0 comments on commit 1587a56

Please sign in to comment.