Skip to content

Commit

Permalink
implement unit testing and refactor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Contreras LEMONCODE committed Aug 22, 2024
1 parent b40559a commit bf6f112
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/core/local-disk/local-disk.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ShapeModel } from '../model';

interface Page {
export interface Page {
id: string;
name: string;
shapes: ShapeModel[];
Expand Down
22 changes: 13 additions & 9 deletions src/core/local-disk/shapes-to-document.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { ShapeModel } from '../model';
import { QuickMockFileContract } from './local-disk.model';
import { QuickMockFileContract, Page } from './local-disk.model';

export const mapFromShapesArrayToQuickMockDocument = (
export const mapFromShapesArrayToQuickMockFileDocument = (
shapes: ShapeModel[]
): QuickMockFileContract => {
const pages: Page[] = shapes.reduce((acc, shape) => {
const newPage: Page = {
id: '1',
name: 'default',
shapes: [{ ...shape }],
};

return [...acc, newPage];
}, [] as Page[]);

return {
version: '0.1',
pages: [
{
id: '1',
name: 'default',
shapes,
},
],
pages,
};
};
131 changes: 131 additions & 0 deletions src/core/local-disk/shapes-to.document.mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { mapFromShapesArrayToQuickMockFileDocument } from './shapes-to-document.mapper';
import { ShapeModel } from '../model';
import { QuickMockFileContract } from './local-disk.model';

describe('shapes to document mapper', () => {
describe('mapFromShapesArrayToQuickMockFileDocument', () => {
it('Should return a ShapeModel with empty pages', () => {
// Arrange
const shapes: ShapeModel[] = [];
const expectedResult: QuickMockFileContract = {
version: '0.1',
pages: [],
};
// Act
const result = mapFromShapesArrayToQuickMockFileDocument(shapes);

// Assert
expect(result).toEqual(expectedResult);
});

it('Should return a ShapeModel with one pages and shapes', () => {
// Arrange
const shapes: ShapeModel[] = [
{
id: '1',
x: 0,
y: 0,
width: 100,
height: 100,
type: 'rectangle',
allowsInlineEdition: false,
typeOfTransformer: ['rotate'],
},
];
const expectedResult: QuickMockFileContract = {
version: '0.1',
pages: [
{
id: '1',
name: 'default',
shapes: [
{
id: '1',
x: 0,
y: 0,
width: 100,
height: 100,
type: 'rectangle',
allowsInlineEdition: false,
typeOfTransformer: ['rotate'],
},
],
},
],
};
// Act
const result = mapFromShapesArrayToQuickMockFileDocument(shapes);

// Assert
expect(result).toEqual(expectedResult);
});

it('Should return a ShapeModel with two pages and shapes', () => {
// Arrange
const shapes: ShapeModel[] = [
{
id: '1',
x: 0,
y: 0,
width: 100,
height: 100,
type: 'rectangle',
allowsInlineEdition: false,
typeOfTransformer: ['rotate'],
},
{
id: '2',
x: 0,
y: 0,
width: 100,
height: 100,
type: 'circle',
allowsInlineEdition: true,
typeOfTransformer: ['rotate'],
},
];
const expectedResult: QuickMockFileContract = {
version: '0.1',
pages: [
{
id: '1',
name: 'default',
shapes: [
{
id: '1',
x: 0,
y: 0,
width: 100,
height: 100,
type: 'rectangle',
allowsInlineEdition: false,
typeOfTransformer: ['rotate'],
},
],
},
{
id: '1',
name: 'default',
shapes: [
{
id: '2',
x: 0,
y: 0,
width: 100,
height: 100,
type: 'circle',
allowsInlineEdition: true,
typeOfTransformer: ['rotate'],
},
],
},
],
};
// Act
const result = mapFromShapesArrayToQuickMockFileDocument(shapes);

// Assert
expect(result).toEqual(expectedResult);
});
});
});
4 changes: 2 additions & 2 deletions src/core/local-disk/use-local-disk.hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { saveFileModern } from '@/common/export';
import { useCanvasContext } from '../providers';
import { mapFromShapesArrayToQuickMockDocument } from './shapes-to-document.mapper';
import { mapFromShapesArrayToQuickMockFileDocument } from './shapes-to-document.mapper';

const DEFAULT_FILE_NAME = 'mymockui';
const DEFAULT_FILE_EXTENSION = 'qm';
Expand All @@ -10,7 +10,7 @@ export const useLocalDisk = () => {
const { shapes } = useCanvasContext();

const serializeShapes = (): string => {
const quickMockDocument = mapFromShapesArrayToQuickMockDocument(shapes);
const quickMockDocument = mapFromShapesArrayToQuickMockFileDocument(shapes);
return JSON.stringify(quickMockDocument);
};

Expand Down
2 changes: 0 additions & 2 deletions src/core/providers/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export interface SelectionInfo {
export interface CanvasContextModel {
shapes: ShapeModel[];
scale: number;
fileName: string;
setFileName: (filename: string) => void;
clearCanvas: () => void;
setScale: React.Dispatch<React.SetStateAction<number>>;
pasteShape: (shape: ShapeModel) => void;
Expand Down
3 changes: 0 additions & 3 deletions src/core/providers/canvas/canvas.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const CanvasProvider: React.FC<Props> = props => {
const { children } = props;

const [scale, setScale] = React.useState(1);
const [fileName, setFileName] = React.useState("");
const stageRef = React.useRef<Konva.Stage>(null);

const {
Expand Down Expand Up @@ -127,8 +126,6 @@ export const CanvasProvider: React.FC<Props> = props => {
value={{
shapes: document.shapes,
scale,
fileName,
setFileName,
setScale,
clearCanvas,
selectionInfo,
Expand Down

0 comments on commit bf6f112

Please sign in to comment.