Skip to content

Commit 975bb26

Browse files
committed
fixup: better naming, add test for invalid state
1 parent b63e03c commit 975bb26

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed

packages/compass-data-modeling/src/components/drawer/collection-drawer-content.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ const CollectionDrawerContent: React.FunctionComponent<
116116
() => toNS(namespace).collection
117117
);
118118

119-
const isCollectionNameValid = useMemo(
119+
const {
120+
isValid: isCollectionNameValid,
121+
errorMessage: collectionNameEditErrorMessage,
122+
} = useMemo(
120123
() => getIsCollectionNameValid(collectionName, namespaces, namespace),
121124
[collectionName, namespaces, namespace]
122125
);
@@ -131,20 +134,15 @@ const CollectionDrawerContent: React.FunctionComponent<
131134
return;
132135
}
133136

134-
if (!isCollectionNameValid.isValid) {
137+
if (!isCollectionNameValid) {
135138
// Reset to previous valid name.
136139
// TODO: Maybe we don't reset.
137140
setCollectionName(toNS(namespace).collection);
138141
return;
139142
}
140143

141144
onRenameCollection(namespace, `${toNS(namespace).database}.${trimmedName}`);
142-
}, [
143-
collectionName,
144-
namespace,
145-
onRenameCollection,
146-
isCollectionNameValid.isValid,
147-
]);
145+
}, [collectionName, namespace, onRenameCollection, isCollectionNameValid]);
148146

149147
return (
150148
<>
@@ -154,8 +152,8 @@ const CollectionDrawerContent: React.FunctionComponent<
154152
label="Name"
155153
sizeVariant="small"
156154
value={collectionName}
157-
state={isCollectionNameValid.isValid ? undefined : 'error'}
158-
errorMessage={isCollectionNameValid.errorMessage}
155+
state={isCollectionNameValid ? undefined : 'error'}
156+
errorMessage={collectionNameEditErrorMessage}
159157
onChange={(e) => {
160158
setCollectionName(e.target.value);
161159
}}

packages/compass-data-modeling/src/components/drawer/diagram-editor-side-panel.spec.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,71 @@ describe('DiagramEditorSidePanel', function () {
261261

262262
expect(modifiedCollection).to.exist;
263263
});
264+
265+
it('should prevent editing to an empty collection name', async function () {
266+
const result = renderDrawer();
267+
result.plugin.store.dispatch(selectCollection('flights.countries'));
268+
269+
await waitFor(() => {
270+
expect(screen.getByLabelText('Name')).to.have.value('countries');
271+
expect(screen.getByLabelText('Name')).to.have.attribute(
272+
'aria-invalid',
273+
'false'
274+
);
275+
});
276+
277+
userEvent.clear(screen.getByLabelText('Name'));
278+
279+
await waitFor(() => {
280+
expect(screen.getByLabelText('Name')).to.have.attribute(
281+
'aria-invalid',
282+
'true'
283+
);
284+
});
285+
286+
// Blur/unfocus the input.
287+
userEvent.click(document.body);
288+
289+
const notModifiedCollection = selectCurrentModel(
290+
getCurrentDiagramFromState(result.plugin.store.getState()).edits
291+
).collections.find((c: DataModelCollection) => {
292+
return c.ns === 'flights.countries';
293+
});
294+
295+
expect(notModifiedCollection).to.exist;
296+
});
297+
298+
it('should prevent editing to a duplicate collection name', async function () {
299+
const result = renderDrawer();
300+
result.plugin.store.dispatch(selectCollection('flights.countries'));
301+
302+
await waitFor(() => {
303+
expect(screen.getByLabelText('Name')).to.have.value('countries');
304+
expect(screen.getByLabelText('Name')).to.have.attribute(
305+
'aria-invalid',
306+
'false'
307+
);
308+
});
309+
310+
userEvent.clear(screen.getByLabelText('Name'));
311+
userEvent.type(screen.getByLabelText('Name'), 'airlines');
312+
313+
await waitFor(() => {
314+
expect(screen.getByLabelText('Name')).to.have.attribute(
315+
'aria-invalid',
316+
'true'
317+
);
318+
});
319+
320+
// Blur/unfocus the input.
321+
userEvent.click(document.body);
322+
323+
const notModifiedCollection = selectCurrentModel(
324+
getCurrentDiagramFromState(result.plugin.store.getState()).edits
325+
).collections.find((c: DataModelCollection) => {
326+
return c.ns === 'flights.countries';
327+
});
328+
329+
expect(notModifiedCollection).to.exist;
330+
});
264331
});

0 commit comments

Comments
 (0)