-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
266 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { ReactFlowProvider, useReactFlow } from 'reactflow'; | ||
import { renderHook, act } from '@/test/utils'; | ||
import { useCreateColumn } from './useCreateColumn'; | ||
import { EditorStateProvider } from '@/contexts/EditorStateContext'; | ||
import { generateMock } from '@anatine/zod-mock'; | ||
import crypto from 'crypto'; | ||
import { expect, it } from 'vitest'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { TableSchema, TableWithoutIdType } from '@/schemas/table'; | ||
import { CreateVarcharColumnSchema } from '@/schemas/varchar'; | ||
import { EdgeType } from '@/schemas/relation'; | ||
import { SchemaType, SchemaSchema } from '@/schemas/schema'; | ||
|
||
const tableId = crypto.randomUUID(); | ||
|
||
const schemaMock: SchemaType = { | ||
...generateMock( | ||
SchemaSchema.omit({ positions: true, relations: true, tables: true, groups: true }), | ||
), | ||
tables: [ | ||
{ | ||
...generateMock(TableSchema.omit({ id: true, columns: true, indexes: true })), | ||
id: tableId, | ||
columns: [], | ||
indexes: [], | ||
}, | ||
], | ||
groups: [], | ||
relations: [], | ||
positions: [ | ||
{ | ||
itemId: tableId, | ||
x: 0, | ||
y: 0, | ||
}, | ||
], | ||
}; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
queryClient.setQueryData([`schema-${schemaMock.id}`], schemaMock); | ||
|
||
it('should create a column', () => { | ||
const { result } = renderHook( | ||
() => { | ||
return { | ||
createColumn: useCreateColumn(), | ||
reactFlowInstance: useReactFlow<TableWithoutIdType, EdgeType>(), | ||
}; | ||
}, | ||
{ | ||
wrapper({ children }) { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<ReactFlowProvider> | ||
<EditorStateProvider schema={schemaMock}>{children}</EditorStateProvider> | ||
</ReactFlowProvider> | ||
</QueryClientProvider> | ||
); | ||
}, | ||
}, | ||
); | ||
|
||
const columnData = generateMock(CreateVarcharColumnSchema); | ||
|
||
act(() => { | ||
const { createColumn } = result.current; | ||
|
||
createColumn(columnData); | ||
}); | ||
|
||
expect( | ||
result.current.reactFlowInstance.getNodes().find((node) => node.id === tableId)?.data.columns ?? | ||
[], | ||
).toHaveLength(1); | ||
expect( | ||
result.current.reactFlowInstance.getNodes().find((node) => node.id === tableId)?.data.columns[0] | ||
.name, | ||
).toEqual(columnData.name); | ||
expect( | ||
result.current.reactFlowInstance.getNodes().find((node) => node.id === tableId)?.data.columns[0] | ||
.type, | ||
).toEqual(columnData.type); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { z } from 'zod'; | ||
|
||
export const GroupSchema = z.object({ | ||
id: z.string().uuid(), | ||
name: z.string(), | ||
width: z.number().int(), | ||
height: z.number().int(), | ||
color: z.object({ | ||
background: z.string(), | ||
border: z.string(), | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from 'zod'; | ||
|
||
export const PositionSchema = z.object({ | ||
itemId: z.string().uuid(), | ||
groupId: z.string().uuid().optional(), | ||
x: z.number(), | ||
y: z.number(), | ||
}); | ||
|
||
export type PositionType = z.infer<typeof PositionSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Edge } from 'reactflow'; | ||
import { z } from 'zod'; | ||
import { ColumnType } from './column'; | ||
|
||
export const RelationActionEnum = z.enum(['CASCADE', 'RESTRICT', 'SET_NULL', 'NO_ACTION']); | ||
|
||
export const RelationSchema = z.object({ | ||
id: z.string().uuid(), | ||
// catch for backward compat | ||
name: z.string().catch('fk'), | ||
source: z.object({ | ||
columnId: z.string().uuid(), | ||
tableId: z.string().uuid(), | ||
}), | ||
target: z.object({ | ||
columnId: z.string().uuid(), | ||
tableId: z.string().uuid(), | ||
}), | ||
// catch for backward compat | ||
actions: z | ||
.object({ | ||
onDelete: RelationActionEnum, | ||
onUpdate: RelationActionEnum, | ||
}) | ||
.catch({ | ||
onDelete: 'RESTRICT', | ||
onUpdate: 'RESTRICT', | ||
}), | ||
}); | ||
|
||
export type RelationType = z.infer<typeof RelationSchema>; | ||
|
||
export const CreateRelationSchema = RelationSchema.omit({ | ||
id: true, | ||
}); | ||
|
||
export type CreateRelationType = z.infer<typeof CreateRelationSchema>; | ||
|
||
export type EdgeType = { | ||
name: RelationType['name']; | ||
sourceColumnId: ColumnType['id']; | ||
targetColumnId: ColumnType['id']; | ||
actions: RelationType['actions']; | ||
}; | ||
|
||
export type RelationEdgeType = Edge<EdgeType>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { z } from 'zod'; | ||
import { GroupSchema } from './group'; | ||
import { PositionSchema } from './position'; | ||
import { RelationSchema } from './relation'; | ||
import { TableSchema } from './table'; | ||
|
||
export const SchemaSchema = z.object({ | ||
id: z.string().uuid(), | ||
name: z.string().trim().max(64), | ||
vendor: z.enum(['MYSQL:8.0']), | ||
tables: TableSchema.array(), | ||
groups: GroupSchema.array(), | ||
positions: PositionSchema.array(), | ||
relations: RelationSchema.array(), | ||
}); | ||
|
||
export type SchemaType = z.infer<typeof SchemaSchema>; |
Oops, something went wrong.