Skip to content

Commit

Permalink
fix(widgets): Prevent case change in table column names (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Oct 8, 2024
1 parent df88c5f commit 6f2f524
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
15 changes: 13 additions & 2 deletions examples/components/widgets/table-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class TableWidget extends BaseWidget {
return widgetSource.getTable({
columns,
...(sortBy && {sortBy, sortDirection}),
rowsPerPage: limit,
limit,
spatialFilter: this.getSpatialFilterOrViewState(),
});
},
Expand Down Expand Up @@ -132,6 +132,17 @@ function renderTableBody(response: TableResponse) {

function renderTableRow(row: unknown[]) {
return html`<tr>
${map(row, (value) => html`<td>${value}</td>`)}
${map(row, renderTableCell)}
</tr>`;
}

const _numberFormatter = new Intl.NumberFormat();
function renderTableCell(value: unknown) {
let formattedValue: string;
if (typeof value === 'number') {
return _numberFormatter.format(value);
} else {
formattedValue = String(value);
}
return html`<td>${formattedValue}</td>`;
}
5 changes: 3 additions & 2 deletions src/sources/widget-base-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,9 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
},
opts: {abortController},
}).then((res: TableModelResponse) => ({
rows: normalizeObjectKeys(res.rows),
totalCount: res.metadata.total,
// Avoid `normalizeObjectKeys()`, which changes column names.
rows: res.rows ?? (res as any).ROWS,
totalCount: res.metadata?.total ?? (res as any).METADATA?.TOTAL,
}));
}

Expand Down
47 changes: 47 additions & 0 deletions test/sources/widget-base-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,53 @@ test('getTable', async () => {
});
});

test('getTable - snowflake', async () => {
const widgetSource = new WidgetTestSource({
accessToken: '<token>',
connectionName: 'carto_dw',
});

const expectedRows = [
{name: 'Veggie Mart', rEVenUe: 1200},
{name: 'EZ Drive Thru', rEVenUe: 400},
{name: "Buddy's Convenience", rEVenUe: 800},
];

// NOTICE: Snowflake returns uppercase keys.
const mockFetch = vi
.fn()
.mockResolvedValueOnce(
createMockResponse({ROWS: expectedRows, METADATA: {TOTAL: 3}})
);
vi.stubGlobal('fetch', mockFetch);

const actualTable = await widgetSource.getTable({
columns: ['name', 'revenue'],
limit: 20,
offset: 10,
});

expect(mockFetch).toHaveBeenCalledOnce();
expect(actualTable).toEqual({
rows: expectedRows,
totalCount: 3,
});

const params = new URL(mockFetch.mock.lastCall[0]).searchParams.entries();
expect(Object.fromEntries(params)).toMatchObject({
type: 'test',
source: 'test-data',
params: JSON.stringify({
column: ['name', 'revenue'],
limit: 20,
offset: 10,
}),
queryParameters: '',
filters: JSON.stringify({}),
filtersLogicalOperator: 'and',
});
});

/******************************************************************************
* getScatter
*/
Expand Down

0 comments on commit 6f2f524

Please sign in to comment.