Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Dashboard/Widgets/SharpGraphWidgetDataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Dashboard/SharpDashboardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
};
Expand All @@ -203,7 +204,7 @@ protected function buildWidgetsData(): void
'key' => 'widget',
'datasets' => [
[
'data' => [10, 20, 30],
'data' => [10, 20, 30, 40],
'label' => 'test',
'color' => 'blue',
],
Expand All @@ -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',
],
],
]);
Expand Down
75 changes: 75 additions & 0 deletions tests/Unit/Dashboard/Widgets/SharpGraphWidgetDataSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

use Code16\Sharp\Dashboard\Widgets\SharpGraphWidgetDataSet;

it('formats numeric values as floats', function () {
$dataSet = SharpGraphWidgetDataSet::make(['a' => 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']);
});
Loading