From 52dfc853d200f0361f4ddd356393414271e2a74f Mon Sep 17 00:00:00 2001 From: Evan Rusackas Date: Tue, 12 May 2026 17:01:25 -0700 Subject: [PATCH] test(country-map): transformProps + buildQuery tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit transformProps tests cover the URL-derivation logic — the core piece that maps form_data into the right build-pipeline output: - Admin 0 + worldview → world choropleth URL - Admin 1 + country → per-country file - Region set + country → regional aggregation - Composite overrides admin level + country - Worldview defaults to 'ukr' when not specified - Different worldviews reflected in URL - Admin 1 without country → null URL (chart UI prompts) - Pass-through of metricName/numberFormat/linearColorScheme - Pass-through of query data + width/height buildQuery tests cover that we're producing a valid chart/data QueryContext with the expected shape (one query, form_data preserved, orderby normalized as array). These are the units most likely to break silently if someone refactors the form_data → URL mapping or the query layer. Co-Authored-By: Claude Opus 4.7 --- .../test/plugin/buildQuery.test.ts | 50 +++++++ .../test/plugin/transformProps.test.ts | 132 ++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 superset-frontend/plugins/plugin-chart-country-map/test/plugin/buildQuery.test.ts create mode 100644 superset-frontend/plugins/plugin-chart-country-map/test/plugin/transformProps.test.ts diff --git a/superset-frontend/plugins/plugin-chart-country-map/test/plugin/buildQuery.test.ts b/superset-frontend/plugins/plugin-chart-country-map/test/plugin/buildQuery.test.ts new file mode 100644 index 00000000000..3ffddfb6172 --- /dev/null +++ b/superset-frontend/plugins/plugin-chart-country-map/test/plugin/buildQuery.test.ts @@ -0,0 +1,50 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import buildQuery from '../../src/plugin/buildQuery'; +import { CountryMapFormData } from '../../src/types'; + +const baseFormData: CountryMapFormData = { + datasource: '3__table', + viz_type: 'country_map', + metric: 'sum__num', + entity: 'iso_code', + adhoc_filters: [], +}; + +test('buildQuery returns a chart/data context with one query', () => { + const ctx = buildQuery(baseFormData); + expect(ctx).toBeDefined(); + expect(Array.isArray(ctx.queries)).toBe(true); + expect(ctx.queries.length).toBe(1); +}); + +test('buildQuery preserves form data on the context', () => { + const ctx = buildQuery(baseFormData); + expect(ctx.form_data).toMatchObject({ + viz_type: 'country_map', + metric: 'sum__num', + entity: 'iso_code', + }); +}); + +test('buildQuery normalizes orderby on the query object', () => { + const ctx = buildQuery({ ...baseFormData, order_desc: true }); + // orderby should be present as an array (possibly empty if no metric ordering) + expect(Array.isArray(ctx.queries[0].orderby)).toBe(true); +}); diff --git a/superset-frontend/plugins/plugin-chart-country-map/test/plugin/transformProps.test.ts b/superset-frontend/plugins/plugin-chart-country-map/test/plugin/transformProps.test.ts new file mode 100644 index 00000000000..421e8bc5a2c --- /dev/null +++ b/superset-frontend/plugins/plugin-chart-country-map/test/plugin/transformProps.test.ts @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { ChartProps } from '@superset-ui/core'; +import transformProps from '../../src/plugin/transformProps'; +import { + CountryMapChartProps, + CountryMapFormData, +} from '../../src/types'; + +const baseFormData: CountryMapFormData = { + datasource: '3__table', + viz_type: 'country_map', +}; + +const buildChartProps = ( + formData: Partial, + data: Record[] = [], +): CountryMapChartProps => + new ChartProps({ + formData: { ...baseFormData, ...formData }, + width: 800, + height: 600, + queriesData: [{ data }], + } as any) as CountryMapChartProps; + +test('Admin 0 (no country) → world choropleth URL', () => { + const out = transformProps( + buildChartProps({ admin_level: 0, worldview: 'ukr' }), + ); + expect(out.geoJsonUrl).toBe('/static/assets/country-maps/ukr_admin0.geo.json'); +}); + +test('Admin 1 + country → per-country file URL', () => { + const out = transformProps( + buildChartProps({ admin_level: 1, country: 'FRA', worldview: 'ukr' }), + ); + expect(out.geoJsonUrl).toBe( + '/static/assets/country-maps/ukr_admin1_FRA.geo.json', + ); +}); + +test('Region set + country → regional aggregation URL', () => { + const out = transformProps( + buildChartProps({ + admin_level: 1, + country: 'TUR', + region_set: 'nuts_1', + worldview: 'ukr', + }), + ); + expect(out.geoJsonUrl).toBe( + '/static/assets/country-maps/regional_TUR_nuts_1_ukr.geo.json', + ); +}); + +test('Composite overrides admin_level + country', () => { + const out = transformProps( + buildChartProps({ + admin_level: 1, + country: 'FRA', + composite: 'france_overseas', + worldview: 'ukr', + }), + ); + expect(out.geoJsonUrl).toBe( + '/static/assets/country-maps/composite_france_overseas_ukr.geo.json', + ); +}); + +test('Worldview defaults to ukr when not specified', () => { + const out = transformProps(buildChartProps({ admin_level: 0 })); + expect(out.geoJsonUrl).toBe('/static/assets/country-maps/ukr_admin0.geo.json'); +}); + +test('Different worldview reflected in URL', () => { + const out = transformProps( + buildChartProps({ admin_level: 0, worldview: 'default' }), + ); + expect(out.geoJsonUrl).toBe( + '/static/assets/country-maps/default_admin0.geo.json', + ); +}); + +test('Admin 1 without country → no URL (chart UI should prompt)', () => { + const out = transformProps(buildChartProps({ admin_level: 1 })); + expect(out.geoJsonUrl).toBeNull(); +}); + +test('Passes through metricName, numberFormat, linearColorScheme', () => { + const out = transformProps( + buildChartProps({ + admin_level: 0, + metric: 'sum__num', + number_format: 'SMART_NUMBER', + linear_color_scheme: 'schemeBlues', + }), + ); + expect(out.metricName).toBe('sum__num'); + expect(out.numberFormat).toBe('SMART_NUMBER'); + expect(out.linearColorScheme).toBe('schemeBlues'); +}); + +test('Passes through query data rows', () => { + const data = [ + { iso: 'FR-75C', sum__num: 100 }, + { iso: 'FR-971', sum__num: 50 }, + ]; + const out = transformProps(buildChartProps({ admin_level: 0 }, data)); + expect(out.data).toEqual(data); +}); + +test('Passes through width/height', () => { + const out = transformProps(buildChartProps({ admin_level: 0 })); + expect(out.width).toBe(800); + expect(out.height).toBe(600); +});