package com.example.e2e; import com.microsoft.playwright.Locator; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; /** * End-to-end coverage for the incremental update path of {@code apex-chart.ts}: * a data update must patch the running ApexCharts instance instead of tearing * the chart down and building it again. */ class DashboardChartPlaywrightTest extends PlaywrightTestBase { private static final String PATCH = """ {"series":[{"name":"Revenue","data":[1,2,3,4,5,6]}], "categories":["Jan","Feb","Mar","Apr","May","Jun"]}"""; @BeforeEach void openDashboard() { navigate(""); } @Test void dataUpdate_keepsTheRenderedSvgInPlace() { Locator chart = axisChart(); assertThat(chart.locator("svg").first()).isVisible(); // Tag the live SVG node: a full rebuild would replace it and drop the tag. chart.evaluate("el => el.querySelector('svg').dataset.marker = 'first-render'"); chart.evaluate("(el, patch) => el.updateData(patch)", PATCH); assertEquals(1, chart.locator("svg[data-marker='first-render']").count(), "the chart must be patched in place, not recreated"); assertEquals("1,2,3,4,5,6", chart.evaluate("el => JSON.parse(el.lastOptionsJson).series[0].data.join(',')"), "the patched data must have reached the chart"); } @Test void dataUpdate_keepsTheThemeOverlay() { Locator chart = axisChart(); assertThat(chart.locator("svg").first()).isVisible(); chart.evaluate("(el, patch) => el.updateData(patch)", PATCH); // grid.borderColor is only ever set by applyThemeOverlay(), from the // resolved --dialect-border token — so a resolved rgb() value here means // the overlay survived the incremental update. Object borderColor = chart.evaluate("el => JSON.parse(el.lastOptionsJson).grid.borderColor"); assertTrue(String.valueOf(borderColor).startsWith("rgb"), "expected a resolved theme color, got: " + borderColor); } /** The first chart in the DOM is a KPI sparkline, which has no * {@code xaxis.categories} — patching one with categories is a structural * change and legitimately redraws. The patch above is an axis-chart patch, * so it has to be applied to an axis chart. */ private Locator axisChart() { return page.locator("apex-chart:not(.dialect-sparkline)").first(); } }