Files
chart-app/src/test/java/com/example/widgets/WidgetRegistryTest.java
T
Pit Friedrich df5d1a9b9b
CI / build-and-test (pull_request) Successful in 2m46s
fix: add a global dashboard filter bar (#24)
The reporting period was baked into the data service, so nothing could
change what the dashboard shows without editing code.

- DashboardFilter (period + optional region) parameterises every
  ChartDataService query; the no-arg overloads are the default filter.
- DashboardContext is the bus between the new DashboardFilterBar and the
  widgets: charts subscribe in WidgetRegistry, KPI tiles in DashboardView.
- Updates go through updateData, so widgets patch in place instead of
  being rebuilt; SparklineChart gained the same path.
- KpiData carries the number, the bundle the unit and number pattern, so
  a KPI value can follow the filter and stay locale-formatted.
- Filter state is deliberately not persisted, unlike the grid layout.

Closes #24

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0124BiJikbhbiEsNfJxdWM69
2026-07-28 20:52:33 +02:00

49 lines
1.7 KiB
Java

package com.example.widgets;
import com.example.data.InMemoryChartDataService;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* The definitions themselves — the factories need a Vaadin UI to resolve
* translations, so they are exercised from {@code DashboardViewTest} instead.
*/
class WidgetRegistryTest {
private final WidgetRegistry registry = new WidgetRegistry(new InMemoryChartDataService());
@Test
void definitions_areListedInRegistrationOrder() {
List<String> types = registry.definitions().stream()
.map(WidgetDefinition::type).toList();
assertEquals(List.of(WidgetRegistry.REVENUE_TREND, WidgetRegistry.REVENUE_MONTH,
WidgetRegistry.REVENUE_REGION), types);
assertTrue(registry.definitions().stream()
.allMatch(d -> d.width() > 0 && d.height() > 0),
"every definition carries a usable default size");
}
@Test
void require_rejectsAnUnknownType() {
assertThrows(IllegalArgumentException.class, () -> registry.require("nope"));
}
@Test
void register_replacesTheDefinitionOfTheSameType() {
int before = registry.definitions().size();
WidgetDefinition replacement = new WidgetDefinition(WidgetRegistry.REVENUE_TREND,
"card.revenueTrend", 2, 2, context -> null);
registry.register(replacement);
assertEquals(before, registry.definitions().size());
assertEquals(replacement, registry.require(WidgetRegistry.REVENUE_TREND));
}
}