From 52a7ba0dbbf8c7841ea174e7deaa26b09492808a Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Tue, 12 Nov 2024 17:51:08 -0500 Subject: [PATCH] chore(picking): Fix response formatting, add tests --- src/widget-sources/widget-base-source.ts | 4 +- .../widget-sources/widget-base-source.test.ts | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/widget-sources/widget-base-source.ts b/src/widget-sources/widget-base-source.ts index dc3aa03..4a88520 100644 --- a/src/widget-sources/widget-base-source.ts +++ b/src/widget-sources/widget-base-source.ts @@ -141,7 +141,9 @@ export abstract class WidgetBaseSource { tileResolution: tileResolution || DEFAULT_TILE_RESOLUTION, }, opts: {abortController}, - }).then((res: FeaturesModelResponse) => normalizeObjectKeys(res)); + }).then((res: FeaturesModelResponse) => ({ + rows: normalizeObjectKeys(res.rows), + })); } /**************************************************************************** diff --git a/test/widget-sources/widget-base-source.test.ts b/test/widget-sources/widget-base-source.test.ts index 92ddfe8..0f3350b 100644 --- a/test/widget-sources/widget-base-source.test.ts +++ b/test/widget-sources/widget-base-source.test.ts @@ -224,6 +224,55 @@ test('filters - owner', async () => { } }); +/****************************************************************************** + * getFeatures + */ + +test('getFeatures', async () => { + const widgetSource = new WidgetTestSource({ + accessToken: '', + connectionName: 'carto_dw', + }); + + const expectedRows = [ + {_carto_feature_id: 'a', name: 'Veggie Mart', revenue: 1200}, + {_carto_feature_id: 'b', name: 'EZ Drive Thru', revenue: 400}, + {_carto_feature_id: 'c', name: "Buddy's Convenience", revenue: 800}, + ]; + + const mockFetch = vi + .fn() + .mockResolvedValueOnce( + createMockResponse({rows: expectedRows, meta: {foo: 'bar'}}) + ); + vi.stubGlobal('fetch', mockFetch); + + const actualFeatures = await widgetSource.getFeatures({ + columns: ['_carto_feature_id', 'name', 'revenue'], + featureIds: ['a', 'b', 'c'], + dataType: 'points', + }); + + expect(mockFetch).toHaveBeenCalledOnce(); + expect(actualFeatures).toEqual({rows: expectedRows}); + + const params = new URL(mockFetch.mock.lastCall[0]).searchParams.entries(); + expect(Object.fromEntries(params)).toMatchObject({ + type: 'test', + source: 'test-data', + params: JSON.stringify({ + columns: ['_carto_feature_id', 'name', 'revenue'], + dataType: 'points', + featureIds: ['a', 'b', 'c'], + limit: 1000, + tileResolution: 0.5, + }), + queryParameters: '', + filters: JSON.stringify({}), + filtersLogicalOperator: 'and', + }); +}); + /****************************************************************************** * getFormula */