mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 02:02:33 +00:00
Disable dataset Save until the form is valid (§05)
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import { beforeEach, describe, expect, test } from 'vitest';
|
||||
import { createDataset } from '@core/dataset';
|
||||
import { createSnippet } from '@core/snippet';
|
||||
import { selectSelectedDataset, useDatasetStore, type DatasetForm } from './DatasetStore';
|
||||
import {
|
||||
selectCanSave,
|
||||
selectSelectedDataset,
|
||||
useDatasetStore,
|
||||
type DatasetForm,
|
||||
} from './DatasetStore';
|
||||
import { useSnippetStore } from './SnippetStore';
|
||||
|
||||
const store = () => useDatasetStore.getState();
|
||||
@@ -82,6 +87,45 @@ describe('save — validation', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('selectCanSave — drives the Save button disabled state (§05)', () => {
|
||||
const canSaveWith = (patch: Partial<DatasetForm>) => {
|
||||
store().startCreate();
|
||||
store().updateForm(patch);
|
||||
return selectCanSave(useDatasetStore.getState());
|
||||
};
|
||||
|
||||
test('false until a name and valid data are present', () => {
|
||||
store().startCreate();
|
||||
expect(selectCanSave(useDatasetStore.getState())).toBe(false); // empty form
|
||||
expect(canSaveWith({ name: 'Sales' })).toBe(false); // no data
|
||||
expect(canSaveWith({ name: ' ', input: '[{"a":1}]' })).toBe(false); // blank name
|
||||
expect(canSaveWith({ name: 'Sales', input: 'not data' })).toBe(false); // unrecognized
|
||||
});
|
||||
|
||||
test('true for valid inline data and valid URLs', () => {
|
||||
expect(canSaveWith({ name: 'Sales', input: '[{"a":1,"b":2}]' })).toBe(true);
|
||||
expect(canSaveWith({ name: 'Regions', input: 'city,pop\nA,10' })).toBe(true);
|
||||
expect(canSaveWith({ name: 'Remote', source: 'url', input: 'https://x/y.csv' })).toBe(true);
|
||||
});
|
||||
|
||||
test('false for a non-http URL', () => {
|
||||
expect(canSaveWith({ name: 'Bad', source: 'url', input: 'ftp://x/y.csv' })).toBe(false);
|
||||
});
|
||||
|
||||
test('false on a duplicate name, true once the editing dataset keeps its own name', () => {
|
||||
store().add(
|
||||
createDataset({ name: 'Sales', data: [{ a: 1 }], format: 'json', source: 'inline', now: T }),
|
||||
);
|
||||
const id = store().datasets[0].id;
|
||||
// Creating another "Sales" is blocked.
|
||||
expect(canSaveWith({ name: 'Sales', input: '[{"a":1}]' })).toBe(false);
|
||||
// Editing the existing one may keep its own name (excludeId).
|
||||
store().select(id);
|
||||
store().startEdit();
|
||||
expect(selectCanSave(useDatasetStore.getState())).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('save — edit', () => {
|
||||
test('updating inline data re-profiles and advances modified', () => {
|
||||
store().add(
|
||||
|
||||
Reference in New Issue
Block a user