Skip to content

Commit 8404dff

Browse files
Attoh, StefanAttoh, Stefan
authored andcommitted
Remaining Testing Changes
1 parent 4d3170a commit 8404dff

13 files changed

Lines changed: 316 additions & 61 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import "@testing-library/jest-dom"
2+
import { render } from "@testing-library/react"
3+
import { describe, it } from "vitest"
4+
import { assembler, integer } from "@hatchifyjs/core"
5+
import { HatchifyColumn } from "./HatchifyColumn.js"
6+
7+
describe("components/HatchifyColumn", () => {
8+
const partialSchemas = {
9+
Todo: {
10+
name: "Todo",
11+
attributes: {
12+
importance: integer(),
13+
},
14+
},
15+
}
16+
17+
const finalSchemas = assembler(partialSchemas)
18+
19+
it("HatchifyColumn renders", async () => {
20+
render(<HatchifyColumn allSchemas={finalSchemas} schemaName="Todo" />)
21+
})
22+
})

packages/react-ui/src/hatchifyReact/hatchifyReact.test.ts renamed to packages/react-ui/src/hatchifyReact/hatchifyReact.test.tsx

Lines changed: 132 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1+
import "@testing-library/jest-dom"
2+
import { render } from "@testing-library/react"
13
import { describe, it, expect } from "vitest"
24
import type { RestClient } from "@hatchifyjs/rest-client"
35
import { hatchifyReact } from "./hatchifyReact.js"
46
import { integer, string } from "@hatchifyjs/core"
57

68
describe("react-ui/hatchifyReact", () => {
79
it("should return objects for each schema", () => {
8-
const fakeDataSource: RestClient<any, any> = {
9-
completeSchemaMap: {
10-
Article: {
11-
name: "Article",
12-
type: "Article",
13-
attributes: {
14-
title: string(),
15-
body: string(),
16-
},
10+
const schemas = {
11+
Article: {
12+
name: "Article",
13+
type: "Article",
14+
attributes: {
15+
title: string(),
16+
body: string(),
1717
},
18-
Person: {
19-
name: "Person",
20-
type: "Person",
21-
attributes: {
22-
name: string(),
23-
age: integer(),
24-
},
18+
},
19+
Person: {
20+
name: "Person",
21+
type: "Person",
22+
attributes: {
23+
name: string(),
24+
age: integer(),
2525
},
2626
},
27+
}
28+
29+
const fakeDataSource: RestClient<typeof schemas, keyof typeof schemas> = {
30+
completeSchemaMap: schemas,
2731
version: 0,
2832
findAll: () => Promise.resolve([{ records: [], related: [] }, {}]),
2933
findOne: () => Promise.resolve({ record: {} as any, related: [] }),
@@ -130,4 +134,116 @@ describe("react-ui/hatchifyReact", () => {
130134
state: { Feature_Article: { useDataGridState: expect.any(Function) } },
131135
})
132136
})
137+
138+
it("should accept schemas with namespaces", () => {
139+
const schemas = {
140+
Feature_Article: {
141+
name: "Article",
142+
type: "Article",
143+
namespace: "Feature",
144+
attributes: {
145+
title: string(),
146+
body: string(),
147+
},
148+
},
149+
}
150+
151+
const fakeDataSource: RestClient<any, any> = {
152+
completeSchemaMap: schemas,
153+
version: 0,
154+
findAll: () => Promise.resolve([{ records: [], related: [] }, {}]),
155+
findOne: () => Promise.resolve({ record: {} as any, related: [] }),
156+
createOne: () => Promise.resolve({ record: {} as any, related: [] }),
157+
updateOne: () => Promise.resolve({ record: {} as any, related: [] }),
158+
deleteOne: () => Promise.resolve(),
159+
}
160+
161+
const api = hatchifyReact(fakeDataSource)
162+
163+
expect(api).toEqual({
164+
Everything: expect.any(Function),
165+
components: {
166+
Feature_Article: {
167+
DataGrid: expect.any(Function),
168+
Column: expect.any(Function),
169+
Empty: expect.any(Function),
170+
},
171+
},
172+
model: {
173+
Feature_Article: {
174+
createOne: expect.any(Function),
175+
deleteOne: expect.any(Function),
176+
findAll: expect.any(Function),
177+
findOne: expect.any(Function),
178+
updateOne: expect.any(Function),
179+
useCreateOne: expect.any(Function),
180+
useDeleteOne: expect.any(Function),
181+
useAll: expect.any(Function),
182+
useOne: expect.any(Function),
183+
useUpdateOne: expect.any(Function),
184+
},
185+
},
186+
state: { Feature_Article: { useDataGridState: expect.any(Function) } },
187+
})
188+
189+
it("should do something else", () => {
190+
const schemas = {
191+
Feature_Article: {
192+
name: "Article",
193+
type: "Article",
194+
namespace: "Feature",
195+
attributes: {
196+
title: string(),
197+
body: string(),
198+
},
199+
},
200+
}
201+
202+
const fakeDataSource: RestClient<any, any> = {
203+
completeSchemaMap: schemas,
204+
version: 0,
205+
findAll: () => Promise.resolve([{ records: [], related: [] }, {}]),
206+
findOne: () => Promise.resolve({ record: {} as any, related: [] }),
207+
createOne: () => Promise.resolve({ record: {} as any, related: [] }),
208+
updateOne: () => Promise.resolve({ record: {} as any, related: [] }),
209+
deleteOne: () => Promise.resolve(),
210+
}
211+
212+
const hatchedReact = hatchifyReact(fakeDataSource)
213+
214+
const { state } = hatchedReact
215+
216+
expect(state.Feature_Article.useDataGridState()).not.toThrowError()
217+
})
218+
219+
it("should render everything", () => {
220+
const schemas = {
221+
Feature_Article: {
222+
name: "Article",
223+
type: "Article",
224+
namespace: "Feature",
225+
attributes: {
226+
title: string(),
227+
body: string(),
228+
},
229+
},
230+
}
231+
232+
const fakeDataSource: RestClient<any, any> = {
233+
completeSchemaMap: schemas,
234+
version: 0,
235+
findAll: () => Promise.resolve([{ records: [], related: [] }, {}]),
236+
findOne: () => Promise.resolve({ record: {} as any, related: [] }),
237+
createOne: () => Promise.resolve({ record: {} as any, related: [] }),
238+
updateOne: () => Promise.resolve({ record: {} as any, related: [] }),
239+
deleteOne: () => Promise.resolve(),
240+
}
241+
242+
const hatchedReact = hatchifyReact(fakeDataSource)
243+
244+
const { Everything } = hatchedReact
245+
246+
render(<Everything />)
247+
})
248+
})
133249
})

packages/react-ui/src/hooks/useCompoundComponents/helpers/getColumn.test.tsx

Lines changed: 139 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect } from "vitest"
1+
import { describe, it, expect, vi } from "vitest"
22
import {
33
assembler,
44
belongsTo,
@@ -34,7 +34,7 @@ describe("hooks/useCompoundComponents/helpers/getColumn", () => {
3434
},
3535
})
3636

37-
it("works with attribute on base schema", () => {
37+
it("returns a column given an attribute on base schema", () => {
3838
const column = getColumn({
3939
finalSchemas: finalSchemas,
4040
schemaName: "Todo",
@@ -55,7 +55,7 @@ describe("hooks/useCompoundComponents/helpers/getColumn", () => {
5555
})
5656
})
5757

58-
it("works with compoundComponentProps", () => {
58+
it("return a column when given compoundComponentProps", () => {
5959
const column = getColumn({
6060
finalSchemas: finalSchemas,
6161
schemaName: "Todo",
@@ -78,30 +78,144 @@ describe("hooks/useCompoundComponents/helpers/getColumn", () => {
7878
})
7979
})
8080

81-
it("correctly sets headerOverride", () => {
82-
const column = getColumn({
83-
finalSchemas: finalSchemas,
84-
schemaName: "Todo",
85-
field: "title",
86-
key: "title",
87-
control: finalSchemas.Todo.attributes.created.control,
88-
compoundComponentProps: {
89-
renderHeaderValue: () => null,
90-
},
91-
defaultValueComponents: HatchifyPresentationDefaultValueComponents,
81+
describe("correctly sets renderDataValue", () => {
82+
const mockDataFunction = vi
83+
.fn()
84+
.mockImplementation(({ args, value }) => "Data")
85+
86+
const mockDataComponent = (header: string) => {
87+
return <h1> {header} </h1>
88+
}
89+
90+
it("for when 'renderData' is given", () => {
91+
const column = getColumn({
92+
finalSchemas: finalSchemas,
93+
schemaName: "Todo",
94+
field: "title",
95+
key: "title",
96+
control: finalSchemas.Todo.attributes.created.control,
97+
compoundComponentProps: {
98+
renderData: mockDataFunction,
99+
},
100+
defaultValueComponents: HatchifyPresentationDefaultValueComponents,
101+
})
102+
103+
expect(column.renderData("string" as any)).toBe("Data")
92104
})
93105

94-
expect(column).toEqual({
95-
headerOverride: true,
96-
sortable: true,
97-
key: "title",
98-
label: "Title",
99-
renderData: expect.any(Function),
100-
renderHeader: expect.any(Function),
106+
it("for when 'renderDataValue' is given", () => {
107+
const column = getColumn({
108+
finalSchemas: finalSchemas,
109+
schemaName: "Todo",
110+
field: "title",
111+
key: "title",
112+
control: finalSchemas.Todo.attributes.created.control,
113+
compoundComponentProps: {
114+
renderDataValue: mockDataFunction,
115+
},
116+
defaultValueComponents: HatchifyPresentationDefaultValueComponents,
117+
})
118+
119+
expect(
120+
column.renderData({ record: { title: "something" } as any } as any),
121+
).toBe("Data")
122+
})
123+
124+
it("for when 'DataValueComponent' is given, renders without error", () => {
125+
const column = getColumn({
126+
finalSchemas: finalSchemas,
127+
schemaName: "Todo",
128+
field: "title",
129+
key: "title",
130+
control: finalSchemas.Todo.attributes.created.control,
131+
compoundComponentProps: {
132+
DataValueComponent: mockDataComponent,
133+
},
134+
defaultValueComponents: HatchifyPresentationDefaultValueComponents,
135+
})
136+
137+
column.renderData({ record: { title: "something" } as any } as any)
138+
})
139+
})
140+
describe("correctly sets headerOverride", () => {
141+
const mockHeaderFunction = vi
142+
.fn()
143+
.mockImplementation((args: unknown) => "Overidden Header")
144+
145+
const mockComponent = (header: string) => {
146+
return <h1> {header} </h1>
147+
}
148+
149+
it("for when renderHeaderValue is null", () => {
150+
const column = getColumn({
151+
finalSchemas: finalSchemas,
152+
schemaName: "Todo",
153+
field: "title",
154+
key: "title",
155+
control: finalSchemas.Todo.attributes.created.control,
156+
compoundComponentProps: {
157+
renderHeaderValue: () => null,
158+
},
159+
defaultValueComponents: HatchifyPresentationDefaultValueComponents,
160+
})
161+
162+
expect(column).toEqual({
163+
headerOverride: true,
164+
sortable: true,
165+
key: "title",
166+
label: "Title",
167+
renderData: expect.any(Function),
168+
renderHeader: expect.any(Function),
169+
})
170+
})
171+
172+
it("for when renderHeaderValue is given render function", () => {
173+
const column = getColumn({
174+
finalSchemas: finalSchemas,
175+
schemaName: "Todo",
176+
field: "title",
177+
key: "title",
178+
control: finalSchemas.Todo.attributes.created.control,
179+
compoundComponentProps: {
180+
renderHeaderValue: mockHeaderFunction,
181+
},
182+
defaultValueComponents: HatchifyPresentationDefaultValueComponents,
183+
})
184+
185+
column.renderHeader("string" as any)
186+
187+
expect(mockHeaderFunction).toBeCalled()
188+
189+
// expect(column).toEqual({
190+
// headerOverride: true,
191+
// sortable: true,
192+
// key: "title",
193+
// label: "Title",
194+
// renderData: expect.any(Function),
195+
// renderHeader: () => expect.any(Function),
196+
// })
197+
})
198+
199+
it("for when HeaderValueComponent is given a component", () => {
200+
const column = getColumn({
201+
finalSchemas: finalSchemas,
202+
schemaName: "Todo",
203+
field: "title",
204+
key: "title",
205+
control: finalSchemas.Todo.attributes.created.control,
206+
compoundComponentProps: {
207+
HeaderValueComponent: mockComponent,
208+
},
209+
defaultValueComponents: HatchifyPresentationDefaultValueComponents,
210+
})
211+
212+
column.renderHeader("string" as any)
213+
214+
expect(mockHeaderFunction).toBeCalled()
101215
})
102216
})
103217

104-
it("works with additional column", () => {
218+
it("returns Column when given an additional column prop", () => {
105219
const column = getColumn({
106220
finalSchemas: finalSchemas,
107221
schemaName: "Todo",
@@ -122,7 +236,7 @@ describe("hooks/useCompoundComponents/helpers/getColumn", () => {
122236
})
123237
})
124238

125-
it("works on relationship", () => {
239+
it("returns a column when given a relationship", () => {
126240
const column = getColumn({
127241
finalSchemas: finalSchemas,
128242
schemaName: "Todo",
@@ -146,9 +260,8 @@ describe("hooks/useCompoundComponents/helpers/getColumn", () => {
146260
})
147261

148262
describe("hooks/useCompoundComponents/helpers/formatFieldAsLabel", () => {
149-
it("works", () => {
150-
// todo: should be `Camel Case`
151-
expect(formatFieldAsLabel("camelCase")).toBe("CamelCase")
263+
it("returns field names as Title Case for labeling", () => {
264+
expect(formatFieldAsLabel("camelCase")).toBe("Camel Case")
152265
expect(formatFieldAsLabel("Singluar")).toBe("Singluar")
153266
})
154267
})

0 commit comments

Comments
 (0)