Skip to content

Commit

Permalink
chore(picking): Fix response formatting, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Nov 12, 2024
1 parent c3cb8fe commit 52a7ba0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/widget-sources/widget-base-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
tileResolution: tileResolution || DEFAULT_TILE_RESOLUTION,
},
opts: {abortController},
}).then((res: FeaturesModelResponse) => normalizeObjectKeys(res));
}).then((res: FeaturesModelResponse) => ({
rows: normalizeObjectKeys(res.rows),
}));
}

/****************************************************************************
Expand Down
49 changes: 49 additions & 0 deletions test/widget-sources/widget-base-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,55 @@ test('filters - owner', async () => {
}
});

/******************************************************************************
* getFeatures
*/

test('getFeatures', async () => {
const widgetSource = new WidgetTestSource({
accessToken: '<token>',
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
*/
Expand Down

0 comments on commit 52a7ba0

Please sign in to comment.