@@ -3,14 +3,29 @@ import { contextRender, createRootStoreState } from "../../testResources";
33import { vi } from "vitest" ;
44import { act , screen } from "@testing-library/react" ;
55import { ensureWidgetsRegistered } from "../widgets" ;
6- import { useClassFile } from "./useClassFile" ;
6+ import { extractThemeProps , useClassFile } from "./useClassFile" ;
77import { CsWebLibConfig } from "../../redux" ;
88import { phoebusTheme } from "../../phoebusTheme" ;
99import { createTheme } from "@mui/material" ;
1010import { CsState } from "../../redux/csState" ;
11+ import { WidgetDescription } from "../widgets/createComponent" ;
12+ import { newAbsolutePosition } from "../../types/position" ;
13+ import { newFont } from "../../types/font" ;
14+ import { newColor } from "../../types/color" ;
1115
1216ensureWidgetsRegistered ( ) ;
1317
18+ const CLASS_WIDGET : WidgetDescription = {
19+ type : "label" ,
20+ id : "123" ,
21+ fileId : "AShapeFilePath" ,
22+ name : "MY_CLASS" ,
23+ font : newFont ( 20 ) ,
24+ position : newAbsolutePosition ( "0" , "0" , "0" , "0" ) ,
25+ backgroundColor : newColor ( "rgba(56,206,56,1)" ) ,
26+ foregroundColor : newColor ( "rgba(29,41,69,1)" )
27+ } ;
28+
1429function getFileState ( ) : CsState {
1530 return {
1631 valueCache : { } ,
@@ -84,6 +99,15 @@ describe("useClassFile", (): void => {
8499 </color>
85100 </background_color>
86101 <tooltip>$(actions)</tooltip>
102+ <font use_class="true">
103+ <font family="Montserrat" style="REGULAR" size="8.0">
104+ </font>
105+ </font>
106+ <border_width use_class="true">3</border_width>
107+ <border_color use_class="true">
108+ <color name="INVALID" red="255" green="0" blue="255">
109+ </color>
110+ </border_color>
87111 </widget>
88112 </display>` ;
89113 const mockJsonPromise = Promise . resolve ( mockSuccessResponse ) ;
@@ -124,3 +148,60 @@ describe("useClassFile", (): void => {
124148 ) . toBeInTheDocument ( ) ;
125149 } ) ;
126150} ) ;
151+
152+ describe ( "extractThemeProps" , ( ) : void => {
153+ it ( "returns nothing if no props in list match" , ( ) : void => {
154+ const matches = extractThemeProps (
155+ CLASS_WIDGET ,
156+ new Set ( [ "offColor" , "onColor" ] ) ,
157+ value => value . colorString
158+ ) ;
159+ expect ( matches ) . toEqual ( { } ) ;
160+ } ) ;
161+ it ( "returns nothing if the map function is wrong" , ( ) : void => {
162+ const matches = extractThemeProps (
163+ CLASS_WIDGET ,
164+ new Set ( [ "font" ] ) ,
165+ value => value . colorString
166+ ) ;
167+ expect ( matches ) . toEqual ( { } ) ;
168+ } ) ;
169+ it ( "filters out undefined values" , ( ) : void => {
170+ const newClassWidget = {
171+ ...CLASS_WIDGET ,
172+ backgroundColor : { colorString : undefined }
173+ } ;
174+ const matches = extractThemeProps (
175+ newClassWidget ,
176+ new Set ( [ "backgroundColor" , "foregroundColor" ] ) ,
177+ value => value . colorString
178+ ) ;
179+ expect ( matches ) . toEqual ( { contrastText : "rgba(29,41,69,1)" } ) ;
180+ } ) ;
181+ it ( "returns a list of matches that are correctly mapped" , ( ) : void => {
182+ const matches = extractThemeProps (
183+ CLASS_WIDGET ,
184+ new Set ( [ "backgroundColor" , "foregroundColor" ] ) ,
185+ value => value . colorString
186+ ) ;
187+ expect ( matches ) . toEqual ( {
188+ contrastText : "rgba(29,41,69,1)" ,
189+ main : "rgba(56,206,56,1)"
190+ } ) ;
191+ } ) ;
192+ it ( "returns a list of matches that don't need mapping" , ( ) : void => {
193+ const matches = extractThemeProps (
194+ CLASS_WIDGET ,
195+ new Set ( [ "font" ] ) ,
196+ value => value
197+ ) ;
198+ expect ( matches ) . toEqual ( {
199+ font : {
200+ name : undefined ,
201+ size : 20 ,
202+ style : "Regular" ,
203+ typeface : "Liberation sans"
204+ }
205+ } ) ;
206+ } ) ;
207+ } ) ;
0 commit comments