@@ -22,6 +22,10 @@ let mockGetSettingWithDefault: <T>(key: string, defaultValue: T) => T
2222
2323const { mock } = await import ( 'bun:test' )
2424
25+ // Import the real implementation to delegate to it in the mock
26+ const { getMeasurementUnitByCountry : realGetMeasurementUnitByCountry } =
27+ await import ( '@screenly/edge-apps' )
28+
2529mock . module ( '@screenly/edge-apps' , ( ) => ( {
2630 fetchCurrentWeatherData : (
2731 lat : number ,
@@ -31,8 +35,44 @@ mock.module('@screenly/edge-apps', () => ({
3135 ) => mockFetchCurrentWeatherData ( lat , lng , tz , unit ) ,
3236 getSettingWithDefault : < T > ( key : string , defaultValue : T ) =>
3337 mockGetSettingWithDefault ( key , defaultValue ) ,
38+ getMeasurementUnitByCountry : ( countryCode : string ) =>
39+ realGetMeasurementUnitByCountry ( countryCode ) ,
3440} ) )
3541
42+ // Mock weather data objects
43+ const metricWeatherData = {
44+ temperature : 19 ,
45+ tempHigh : 22 ,
46+ tempLow : 15 ,
47+ weatherId : 800 ,
48+ description : 'clear sky' ,
49+ iconSrc : '/static/images/icons/clear.svg' ,
50+ iconAlt : 'clear sky' ,
51+ unit : 'metric' as const ,
52+ }
53+
54+ const imperialWeatherData = {
55+ temperature : 66 ,
56+ tempHigh : 70 ,
57+ tempLow : 60 ,
58+ weatherId : 800 ,
59+ description : 'clear sky' ,
60+ iconSrc : '/static/images/icons/clear.svg' ,
61+ iconAlt : 'clear sky' ,
62+ unit : 'imperial' as const ,
63+ }
64+
65+ const zeroTempWeatherData = {
66+ temperature : 0 ,
67+ tempHigh : 3 ,
68+ tempLow : - 2 ,
69+ weatherId : 800 ,
70+ description : 'clear sky' ,
71+ iconSrc : '/static/images/icons/clear.svg' ,
72+ iconAlt : 'clear sky' ,
73+ unit : 'metric' as const ,
74+ }
75+
3676describe ( 'getWeatherData' , ( ) => {
3777 test ( 'should return null when fetchCurrentWeatherData returns null' , async ( ) => {
3878 mockGetSettingWithDefault = ( key , defaultValue ) => defaultValue
@@ -42,28 +82,21 @@ describe('getWeatherData', () => {
4282 37.3861 ,
4383 - 122.0839 ,
4484 'America/Los_Angeles' ,
85+ 'GB' ,
4586 )
4687
4788 expect ( result ) . toBeNull ( )
4889 } )
4990
5091 test ( 'should return weather data with metric units' , async ( ) => {
5192 mockGetSettingWithDefault = ( key , defaultValue ) => defaultValue
52- mockFetchCurrentWeatherData = async ( ) => ( {
53- temperature : 19 ,
54- tempHigh : 22 ,
55- tempLow : 15 ,
56- weatherId : 800 ,
57- description : 'clear sky' ,
58- iconSrc : '/static/images/icons/clear.svg' ,
59- iconAlt : 'clear sky' ,
60- unit : 'metric' ,
61- } )
93+ mockFetchCurrentWeatherData = async ( ) => metricWeatherData
6294
6395 const result = await getWeatherData (
6496 37.3861 ,
6597 - 122.0839 ,
6698 'America/Los_Angeles' ,
99+ 'GB' ,
67100 )
68101
69102 expect ( result ?. temperature ) . toBe ( 19 )
@@ -73,21 +106,13 @@ describe('getWeatherData', () => {
73106 test ( 'should return weather data with imperial units' , async ( ) => {
74107 mockGetSettingWithDefault = < T > ( _key : string , _defaultValue : T ) : T =>
75108 'imperial' as T
76- mockFetchCurrentWeatherData = async ( ) => ( {
77- temperature : 66 ,
78- tempHigh : 70 ,
79- tempLow : 60 ,
80- weatherId : 800 ,
81- description : 'clear sky' ,
82- iconSrc : '/static/images/icons/clear.svg' ,
83- iconAlt : 'clear sky' ,
84- unit : 'imperial' ,
85- } )
109+ mockFetchCurrentWeatherData = async ( ) => imperialWeatherData
86110
87111 const result = await getWeatherData (
88112 37.3861 ,
89113 - 122.0839 ,
90114 'America/Los_Angeles' ,
115+ 'US' ,
91116 )
92117
93118 expect ( result ?. temperature ) . toBe ( 66 )
@@ -96,18 +121,14 @@ describe('getWeatherData', () => {
96121
97122 test ( 'should handle temperature of 0 correctly' , async ( ) => {
98123 mockGetSettingWithDefault = ( key , defaultValue ) => defaultValue
99- mockFetchCurrentWeatherData = async ( ) => ( {
100- temperature : 0 ,
101- tempHigh : 3 ,
102- tempLow : - 2 ,
103- weatherId : 800 ,
104- description : 'clear sky' ,
105- iconSrc : '/static/images/icons/clear.svg' ,
106- iconAlt : 'clear sky' ,
107- unit : 'metric' ,
108- } )
109-
110- const result = await getWeatherData ( 59.3293 , 18.0686 , 'Europe/Stockholm' )
124+ mockFetchCurrentWeatherData = async ( ) => zeroTempWeatherData
125+
126+ const result = await getWeatherData (
127+ 59.3293 ,
128+ 18.0686 ,
129+ 'Europe/Stockholm' ,
130+ 'SE' ,
131+ )
111132
112133 expect ( result ) . not . toBeNull ( )
113134 expect ( result ?. temperature ) . toBe ( 0 )
0 commit comments