mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 02:02:33 +00:00
Chart builder: filter/calculate transforms, data preview, and inline expression validation
This commit is contained in:
@@ -140,6 +140,95 @@ describe('applyWarningFix', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('filters (1C)', () => {
|
||||
test('addFilter seeds a predicate on the first column with its derived type', () => {
|
||||
const id = seedDataset('S', [{ region: 'N', revenue: 5 }]);
|
||||
cb().init(id);
|
||||
cb().addFilter();
|
||||
const f = cb().config.filters?.[0];
|
||||
expect(f).toMatchObject({
|
||||
mode: 'predicate',
|
||||
field: 'region',
|
||||
fieldType: 'nominal',
|
||||
op: 'equal',
|
||||
});
|
||||
});
|
||||
|
||||
test('setFilterField re-derives the field type and clamps an out-of-range operator', () => {
|
||||
const id = seedDataset('S', [{ region: 'N', revenue: 5 }]);
|
||||
cb().init(id);
|
||||
cb().addFilter();
|
||||
const fid = cb().config.filters![0].id;
|
||||
// Point at the measure, choose an ordering op, then point back at the category:
|
||||
cb().setFilterField(fid, 'revenue');
|
||||
cb().updateFilter(fid, { op: 'gt', value: '3' });
|
||||
expect(cb().config.filters![0]).toMatchObject({ fieldType: 'quantitative', op: 'gt' });
|
||||
cb().setFilterField(fid, 'region'); // gt is invalid on a category → resets to equal
|
||||
expect(cb().config.filters![0]).toMatchObject({
|
||||
field: 'region',
|
||||
fieldType: 'nominal',
|
||||
op: 'equal',
|
||||
});
|
||||
});
|
||||
|
||||
test('removeFilter drops the row', () => {
|
||||
const id = seedDataset('S', [{ region: 'N' }]);
|
||||
cb().init(id);
|
||||
cb().addFilter();
|
||||
cb().addFilter();
|
||||
const first = cb().config.filters![0].id;
|
||||
cb().removeFilter(first);
|
||||
expect(cb().config.filters).toHaveLength(1);
|
||||
expect(cb().config.filters![0].id).not.toBe(first);
|
||||
});
|
||||
|
||||
test('setFilterMode toggles to expression and back, seeding a field on return', () => {
|
||||
const id = seedDataset('S', [{ region: 'N', revenue: 5 }]);
|
||||
cb().init(id);
|
||||
cb().addFilter();
|
||||
const fid = cb().config.filters![0].id;
|
||||
cb().setFilterMode(fid, 'expression');
|
||||
expect(cb().config.filters![0].mode).toBe('expression');
|
||||
cb().setFilterMode(fid, 'predicate');
|
||||
expect(cb().config.filters![0]).toMatchObject({ mode: 'predicate', field: 'region' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculated fields (1C)', () => {
|
||||
test('a calculated field becomes selectable on a channel, defaulting to quantitative', () => {
|
||||
const id = seedDataset('S', [{ a: 1, b: 2 }]);
|
||||
cb().init(id);
|
||||
cb().addCalculate();
|
||||
const cid = cb().config.calculates![0].id;
|
||||
cb().updateCalculate(cid, { as: 'total', expr: 'datum.a + datum.b' });
|
||||
cb().setChannelColumn('y', 'total');
|
||||
expect(cb().config.encodings.y).toEqual({ field: 'total', type: 'quantitative' });
|
||||
});
|
||||
|
||||
test('removing a calculated field clears any channel that referenced it', () => {
|
||||
const id = seedDataset('S', [{ a: 1, b: 2 }]);
|
||||
cb().init(id);
|
||||
cb().addCalculate();
|
||||
const cid = cb().config.calculates![0].id;
|
||||
cb().updateCalculate(cid, { as: 'total', expr: 'datum.a + datum.b' });
|
||||
cb().setChannelColumn('y', 'total');
|
||||
cb().removeCalculate(cid);
|
||||
expect(cb().config.calculates).toHaveLength(0);
|
||||
expect(cb().config.encodings.y).toBeNull(); // pruned — its field no longer exists
|
||||
});
|
||||
|
||||
test('renaming a referenced calculated field prunes the now-dangling channel', () => {
|
||||
const id = seedDataset('S', [{ a: 1 }]);
|
||||
cb().init(id);
|
||||
cb().addCalculate();
|
||||
const cid = cb().config.calculates![0].id;
|
||||
cb().updateCalculate(cid, { as: 'doubled', expr: 'datum.a * 2' });
|
||||
cb().setChannelColumn('x', 'doubled');
|
||||
cb().updateCalculate(cid, { as: 'tripled' }); // the channel still points at "doubled"
|
||||
expect(cb().config.encodings.x).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('createSnippet', () => {
|
||||
test('builds a linked snippet, activates it, and resets the builder', () => {
|
||||
const id = seedDataset('Sales', [
|
||||
|
||||
Reference in New Issue
Block a user