Skip to content

Commit

Permalink
Fix the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
lordofthelake committed Jan 22, 2020
1 parent 435bce2 commit 76e4899
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 48 deletions.
4 changes: 2 additions & 2 deletions __tests__/jest/components/nodeImpl/Svg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jest.mock('../../../../src/jsonUtils/models', () => ({
}));

describe('node <Svg />', () => {
it('generates the json for an svg', () => {
it('generates the json for an svg', async () => {
class SVGElement extends React.Component {
render() {
return (
Expand All @@ -26,6 +26,6 @@ describe('node <Svg />', () => {
}
}

expect(ReactSketch.renderToJSON(<SVGElement />)).toMatchSnapshot();
expect(await ReactSketch.renderToJSON(<SVGElement />)).toMatchSnapshot();
});
});
6 changes: 1 addition & 5 deletions __tests__/jest/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/* eslint-disable global-require */
import ReactSketch from '../../src';
jest.mock('../../src/jsonUtils/sketchImpl/createStringMeasurer');
jest.mock('../../src/jsonUtils/sketchImpl/findFontName');
jest.mock('../../src/jsonUtils/sketchImpl/makeImageDataFromUrl');
jest.mock('../../src/jsonUtils/sketchImpl/makeSvgLayer');
import * as ReactSketch from '../../src';

describe('public API', () => {
it('exports render', () => {
Expand Down
3 changes: 2 additions & 1 deletion __tests__/jest/jsonUtils/computeYogaNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import yoga from 'yoga-layout-prebuilt';
import computeYogaNode from '../../../src/jsonUtils/computeYogaNode';
import Context from '../../../src/utils/Context';
import NodeMacOSBridge from '../../../src/platformBridges/NodeMacOSBridge';

const widthAndHeightStylesStub = {
width: 10,
Expand Down Expand Up @@ -32,7 +33,7 @@ const createYogaNodes = (
styles.forEach(style => {
const treeNode = createTreeNode(style);
const ctx = new Context();
const { node } = computeYogaNode(treeNode, ctx);
const { node } = computeYogaNode(treeNode, ctx, NodeMacOSBridge);
node.calculateLayout(
containerWidth || undefined,
containerHeight || undefined,
Expand Down
5 changes: 3 additions & 2 deletions __tests__/jest/jsonUtils/computeYogaTree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import yoga from 'yoga-layout-prebuilt';
import computeYogaTree from '../../../src/jsonUtils/computeYogaTree';
import Context from '../../../src/utils/Context';
import NodeMacOSBridge from '../../../src/platformBridges/NodeMacOSBridge';

const treeRootStub = {
type: 'artboard',
Expand Down Expand Up @@ -35,11 +36,11 @@ const treeRootStub = {
],
};

computeYogaTree(treeRootStub, new Context());
computeYogaTree(treeRootStub, new Context(), NodeMacOSBridge);

describe('Compute Yoga Tree', () => {
it('correctly create yoga nodes into layout tree', () => {
const yogaTree = computeYogaTree(treeRootStub, new Context());
const yogaTree = computeYogaTree(treeRootStub, new Context(), NodeMacOSBridge);
yogaTree.calculateLayout(undefined, undefined, yoga.DIRECTION_LTR);
expect(yogaTree.getComputedLayout()).toEqual({
bottom: 0,
Expand Down
3 changes: 2 additions & 1 deletion __tests__/jest/reactTreeToFlexTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import yoga from 'yoga-layout-prebuilt';
import computeYogaTree from '../../src/jsonUtils/computeYogaTree';
import Context from '../../src/utils/Context';
import { reactTreeToFlexTree } from '../../src/buildTree';
import NodeMacOSBridge from '../../src/platformBridges/NodeMacOSBridge';

const treeRootStub = {
type: 'artboard',
Expand Down Expand Up @@ -55,7 +56,7 @@ const treeRootStub = {

describe('Compute Flex Tree', () => {
it('correctly creates flex tree', () => {
const yogaNode = computeYogaTree(treeRootStub, new Context());
const yogaNode = computeYogaTree(treeRootStub, new Context(), NodeMacOSBridge);
yogaNode.calculateLayout(undefined, undefined, yoga.DIRECTION_LTR);
const tree = reactTreeToFlexTree(treeRootStub, yogaNode, new Context());

Expand Down
64 changes: 29 additions & 35 deletions __tests__/jest/sharedStyles/TextStyles.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import NodeMacOSBridge from '../../../src/platformBridges/NodeMacOSBridge';

/* eslint-disable global-require */
let TextStyles;
let doc;
Expand All @@ -7,7 +9,8 @@ beforeEach(() => {
jest.resetModules();

jest.mock('../../../src/utils/getSketchVersion', () => ({
getSketchVersion: jest.fn(() => 51),
__esModule: true,
default: jest.fn(() => '51'),
}));

TextStyles = require('../../../src/sharedStyles/TextStyles');
Expand All @@ -16,12 +19,12 @@ beforeEach(() => {

jest.mock('../../../src/utils/sharedTextStyles');

jest.mock('../../../src/jsonUtils/sketchImpl/createStringMeasurer');
jest.mock('../../../src/jsonUtils/sketchImpl/findFontName');
jest.mock('../../../src/jsonUtils/sketchImpl/makeImageDataFromUrl');
jest.mock('../../../src/jsonUtils/sketchImpl/makeSvgLayer');
// jest.mock('../../../src/jsonUtils/sketchImpl/createStringMeasurer');
// jest.mock('../../../src/jsonUtils/sketchImpl/findFontName');
// jest.mock('../../../src/jsonUtils/sketchImpl/makeImageDataFromUrl');
// jest.mock('../../../src/jsonUtils/sketchImpl/makeSvgLayer');

TextStyles = TextStyles.default;
TextStyles = TextStyles.default(() => NodeMacOSBridge);
sharedTextStyles = sharedTextStyles.default;

sharedTextStyles.setDocument = jest.fn(doc => {
Expand Down Expand Up @@ -115,41 +118,32 @@ describe('create', () => {
});

it('only stores text attributes', () => {
const whitelist = [
'color',
'fontFamily',
'fontSize',
'fontStyle',
'fontWeight',
'textShadowOffset',
'textShadowRadius',
'textShadowColor',
'textTransform',
'letterSpacing',
'lineHeight',
'textAlign',
'writingDirection',
];

const blacklist = ['foo', 'bar', 'baz'];

const input = [...whitelist, ...blacklist].reduce(
(acc, key) => ({
...acc,
[key]: true,
}),
{},
);
const whitelist = {
color: 'red',
fontFamily: 'Helvetica',
fontSize: 14,
fontStyle: 'italic',
fontWeight: 'bold',
textShadowOffset: 2,
textShadowRadius: 1,
textShadowColor: 'black',
textTransform: 'uppercase',
letterSpacing: 1,
lineHeight: 18,
textAlign: 'left',
writingDirection: 'ltr',
};

const res = TextStyles.create({ foo: input }, { document: doc });
const blacklist = { foo: 1, bar: 2, baz: 3 };

const res = TextStyles.create({ foo: { ...whitelist, ...blacklist } }, { document: doc });
const firstStoredStyle = res[Object.keys(res)[0]].cssStyle;

whitelist.forEach(key => {
expect(firstStoredStyle).toHaveProperty(key, true);
Object.keys(whitelist).forEach(key => {
expect(firstStoredStyle).toHaveProperty(key, whitelist[key]);
});

blacklist.forEach(key => {
Object.keys(blacklist).forEach(key => {
expect(firstStoredStyle).not.toHaveProperty(key);
});
});
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/jsonUtils/makeSvgLayer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import isRunningInSketch from '../../utils/isRunningInSketch';
import sketchMakeSvgLayer from './makeSvgLayer.sketch';
import pureJsSketchMakeSvgLayer from './makeSvgLayer';

export default isRunningInSketch() ? pureJsSketchMakeSvgLayer : sketchMakeSvgLayer;
export default isRunningInSketch() ? sketchMakeSvgLayer : pureJsSketchMakeSvgLayer;
2 changes: 1 addition & 1 deletion src/jsonUtils/makeSvgLayer/makeSvgLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default async function makeSvgLayer(
svg: string,
): Promise<FileFormat.Group> {
// Load the module only if it has been made available through another import.
const svgModel = await import(/* webpackMode: "weak" */ '@lona/svg-model');
const svgModel = (await import(/* webpackMode: "weak" */ '@lona/svg-model')).default;

const {
data: { params, children },
Expand Down

0 comments on commit 76e4899

Please sign in to comment.