1- import { describe , it , expect } from "vitest"
1+ import { describe , it , expect , vi } from "vitest"
22import {
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
148262describe ( "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