diff --git a/src/Dashboard/Widgets/SharpGraphWidgetDataSet.php b/src/Dashboard/Widgets/SharpGraphWidgetDataSet.php index 154e7f2d8..b73366393 100644 --- a/src/Dashboard/Widgets/SharpGraphWidgetDataSet.php +++ b/src/Dashboard/Widgets/SharpGraphWidgetDataSet.php @@ -56,9 +56,10 @@ protected function formatValues(array $values): array protected function formatLabels(array $values): array { if ($this->hasDateLabels) { - return array_map(function ($value) { - return (new Carbon($value))->setTimezone(config('app.timezone'))->toAtomString(); - }, array_keys($values)); + return array_map( + fn ($value) => (new Carbon($value))->shiftTimezone('UTC')->toAtomString(), + array_keys($values) + ); } return array_keys($values); diff --git a/tests/Unit/Dashboard/SharpDashboardTest.php b/tests/Unit/Dashboard/SharpDashboardTest.php index d506ced64..a7c85d6d5 100644 --- a/tests/Unit/Dashboard/SharpDashboardTest.php +++ b/tests/Unit/Dashboard/SharpDashboardTest.php @@ -193,6 +193,7 @@ protected function buildWidgetsData(): void '2026-04-03' => 10, '2026-04-04 12:30:04' => 20, '2026-04-05' => 30, + '2026-04-06 12:30:04+02:00' => 40, ])->setLabel('test')->setColor('blue')); } }; @@ -203,7 +204,7 @@ protected function buildWidgetsData(): void 'key' => 'widget', 'datasets' => [ [ - 'data' => [10, 20, 30], + 'data' => [10, 20, 30, 40], 'label' => 'test', 'color' => 'blue', ], @@ -212,6 +213,7 @@ protected function buildWidgetsData(): void '2026-04-03T00:00:00+00:00', '2026-04-04T12:30:04+00:00', '2026-04-05T00:00:00+00:00', + '2026-04-06T12:30:04+00:00', ], ], ]); diff --git a/tests/Unit/Dashboard/Widgets/SharpGraphWidgetDataSetTest.php b/tests/Unit/Dashboard/Widgets/SharpGraphWidgetDataSetTest.php new file mode 100644 index 000000000..fcc813373 --- /dev/null +++ b/tests/Unit/Dashboard/Widgets/SharpGraphWidgetDataSetTest.php @@ -0,0 +1,75 @@ + 1, 'b' => 2, 'c' => 3]); + + expect($dataSet->toArray()['data'])->toEqual([1.0, 2.0, 3.0]); +}); + +it('formats string numeric values as floats', function () { + $dataSet = SharpGraphWidgetDataSet::make(['a' => '1.5', 'b' => '2.7']); + + expect($dataSet->toArray()['data'])->toEqual([1.5, 2.7]); +}); + +it('returns array keys as labels by default', function () { + $dataSet = SharpGraphWidgetDataSet::make(['foo' => 10, 'bar' => 20]); + + expect($dataSet->toArray()['labels'])->toEqual(['foo', 'bar']); +}); + +it('accepts a collection', function () { + $dataSet = SharpGraphWidgetDataSet::make(collect(['a' => 1, 'b' => 2])); + + expect($dataSet->toArray()['data'])->toEqual([1.0, 2.0]) + ->and($dataSet->toArray()['labels'])->toEqual(['a', 'b']); +}); + +it('sets a label', function () { + $dataSet = SharpGraphWidgetDataSet::make(['a' => 1]) + ->setLabel('My Dataset'); + + expect($dataSet->toArray()['label'])->toEqual('My Dataset'); +}); + +it('does not include label key when not set', function () { + $dataSet = SharpGraphWidgetDataSet::make(['a' => 1]); + + expect($dataSet->toArray())->not->toHaveKey('label'); +}); + +it('sets a color', function () { + $dataSet = SharpGraphWidgetDataSet::make(['a' => 1]) + ->setColor('#ff0000'); + + expect($dataSet->toArray()['color'])->toEqual('#ff0000'); +}); + +it('does not include color key when not set', function () { + $dataSet = SharpGraphWidgetDataSet::make(['a' => 1]); + + expect($dataSet->toArray())->not->toHaveKey('color'); +}); + +it('formats date labels as UTC atom strings regardless of app timezone', function (string $timezone) { + config(['app.timezone' => $timezone]); + + $dataSet = SharpGraphWidgetDataSet::make(['2024-01-15' => 10])->withDateLabels(); + + expect($dataSet->toArray()['labels'][0])->toEqual('2024-01-15T00:00:00+00:00'); +})->with([ + 'UTC', + 'America/New_York', // UTC-5 + 'Asia/Tokyo', // UTC+9 +]); + +it('returns plain keys when withDateLabels is not called', function () { + $dataSet = SharpGraphWidgetDataSet::make([ + '2024-01-15' => 10, + '2024-06-01' => 20, + ]); + + expect($dataSet->toArray()['labels'])->toEqual(['2024-01-15', '2024-06-01']); +});