Skip to content

Commit 9102096

Browse files
committed
fixing the types'
1 parent f116d6b commit 9102096

File tree

3 files changed

+97
-61
lines changed

3 files changed

+97
-61
lines changed

packages/compass-data-modeling/src/components/diagram-editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const DiagramEditor: React.FunctionComponent<{
110110
editErrors?: string[];
111111
onRetryClick: () => void;
112112
onCancelClick: () => void;
113-
onApplyClick: (edit: Edit) => void;
113+
onApplyClick: (edit: Omit<Edit, 'id' | 'timestamp'>) => void;
114114
}> = ({
115115
step,
116116
hasUndo,

packages/compass-data-modeling/src/store/diagram.spec.ts

Lines changed: 93 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,70 @@
11
import { expect } from 'chai';
2-
import { setupStore } from '../../test/setup-store';
2+
import { type DataModelingStore, setupStore } from '../../test/setup-store';
33
import {
44
applyEdit,
55
getCurrentDiagramFromState,
66
openDiagram,
77
redoEdit,
88
undoEdit,
99
} from './diagram';
10+
import type {
11+
Edit,
12+
MongoDBDataModelDescription,
13+
StaticModel,
14+
} from '../services/data-model-storage';
15+
16+
const model: StaticModel = {
17+
collections: [
18+
{
19+
ns: 'collection1',
20+
indexes: [],
21+
displayPosition: [0, 0],
22+
shardKey: {},
23+
jsonSchema: {},
24+
},
25+
{
26+
ns: 'collection2',
27+
indexes: [],
28+
displayPosition: [1, 1],
29+
shardKey: {},
30+
jsonSchema: {},
31+
},
32+
],
33+
relationships: [
34+
{
35+
id: 'relationship1',
36+
relationship: [
37+
{
38+
ns: 'db.sourceCollection',
39+
cardinality: 1,
40+
fields: ['field1'],
41+
},
42+
{
43+
ns: 'db.targetCollection',
44+
cardinality: 1,
45+
fields: ['field2'],
46+
},
47+
],
48+
isInferred: false,
49+
},
50+
],
51+
};
52+
53+
const loadedDiagram: MongoDBDataModelDescription = {
54+
id: 'diagram-id',
55+
name: 'diagram-name',
56+
connectionId: 'connection-id',
57+
edits: [{ type: 'SetModel', model } as Edit],
58+
};
1059

1160
describe('Data Modeling store', function () {
12-
let store;
61+
let store: DataModelingStore;
1362

1463
beforeEach(function () {
1564
store = setupStore();
1665
});
1766

1867
it('openDiagram', function () {
19-
const loadedDiagram = {
20-
id: 'diagram-id',
21-
name: 'diagram-name',
22-
connectionId: 'connection-id',
23-
edits: [{ type: 'SetModel', model: {} }],
24-
};
25-
2668
store.dispatch(openDiagram(loadedDiagram));
2769

2870
const diagram = getCurrentDiagramFromState(store.getState());
@@ -34,39 +76,37 @@ describe('Data Modeling store', function () {
3476

3577
describe('applyEdit', function () {
3678
it('should apply a valid SetModel edit', function () {
37-
const loadedDiagram = {
38-
id: 'diagram-id',
39-
name: 'diagram-name',
40-
connectionId: 'connection-id',
41-
edits: [{ type: 'SetModel', model: {} }],
42-
};
43-
4479
store.dispatch(openDiagram(loadedDiagram));
4580

46-
const newModel = { collections: [], relationships: [] };
47-
store.dispatch(applyEdit({ type: 'SetModel', model: newModel }));
81+
const edit = {
82+
type: 'SetModel' as const,
83+
model: {
84+
collections: [
85+
{
86+
ns: 'collection2',
87+
indexes: [],
88+
displayPosition: [0, 0],
89+
shardKey: {},
90+
jsonSchema: {},
91+
},
92+
] as StaticModel['collections'],
93+
relationships: [] as StaticModel['relationships'],
94+
},
95+
};
96+
store.dispatch(applyEdit(edit));
4897

49-
const diagram = getCurrentDiagramFromState(store.getState());
50-
expect(diagram.editErrors).to.be.undefined;
98+
const state = store.getState();
99+
const diagram = getCurrentDiagramFromState(state);
100+
expect(state.diagram?.editErrors).to.be.undefined;
51101
expect(diagram.edits).to.have.length(2);
52-
expect(diagram.edits[0]).to.deep.equal(diagram.edits[0]);
53-
expect(diagram.edits[1]).to.deep.include({
54-
type: 'SetModel',
55-
model: newModel,
56-
});
102+
expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]);
103+
expect(diagram.edits[1]).to.deep.include(edit);
57104
});
58105

59106
it('should apply a valid AddRelationship edit', function () {
60-
const loadedDiagram = {
61-
id: 'diagram-id',
62-
name: 'diagram-name',
63-
connectionId: 'connection-id',
64-
edits: [{ type: 'SetModel', model: {} }],
65-
};
66-
67107
store.dispatch(openDiagram(loadedDiagram));
68108

69-
const newRelationship = {
109+
const newRelationship: StaticModel['relationships'][number] = {
70110
id: 'relationship1',
71111
relationship: [
72112
{
@@ -83,27 +123,24 @@ describe('Data Modeling store', function () {
83123
isInferred: false,
84124
};
85125
store.dispatch(
86-
applyEdit({ type: 'AddRelationship', relationship: newRelationship })
126+
applyEdit({
127+
type: 'AddRelationship',
128+
relationship: newRelationship,
129+
} as Edit)
87130
);
88131

89-
const diagram = getCurrentDiagramFromState(store.getState());
90-
expect(diagram.editErrors).to.be.undefined;
132+
const state = store.getState();
133+
const diagram = getCurrentDiagramFromState(state);
134+
expect(state.diagram?.editErrors).to.be.undefined;
91135
expect(diagram.edits).to.have.length(2);
92-
expect(diagram.edits[0]).to.deep.equal(diagram.edits[0]);
136+
expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]);
93137
expect(diagram.edits[1]).to.deep.include({
94138
type: 'AddRelationship',
95139
relationship: newRelationship,
96140
});
97141
});
98142

99143
it('should not apply invalid AddRelationship edit', function () {
100-
const loadedDiagram = {
101-
id: 'diagram-id',
102-
name: 'diagram-name',
103-
connectionId: 'connection-id',
104-
edits: [{ type: 'SetModel', model: {} }],
105-
};
106-
107144
store.dispatch(openDiagram(loadedDiagram));
108145

109146
const edit = {
@@ -112,31 +149,29 @@ describe('Data Modeling store', function () {
112149
id: 'relationship1',
113150
isInferred: false,
114151
},
115-
};
152+
} as unknown as Edit;
116153
store.dispatch(applyEdit(edit));
117154

118-
const editErrors = store.getState().diagram.editErrors;
155+
const editErrors = store.getState().diagram?.editErrors;
119156
expect(editErrors).to.have.length(1);
120-
expect(editErrors[0]).to.equal("'relationship,relationship' is required");
157+
expect(editErrors && editErrors[0]).to.equal(
158+
"'relationship,relationship' is required"
159+
);
121160
const diagram = getCurrentDiagramFromState(store.getState());
122-
expect(diagram.edits).to.deep.equal(diagram.edits);
161+
expect(diagram.edits).to.deep.equal(loadedDiagram.edits);
123162
});
124163
});
125164

126-
it('undo/redo', function () {
127-
const loadedDiagram = {
128-
id: 'diagram-id',
129-
name: 'diagram-name',
130-
connectionId: 'connection-id',
131-
edits: [{ type: 'SetModel', model: {} }],
132-
};
133-
165+
it('undo & redo', function () {
134166
store.dispatch(openDiagram(loadedDiagram));
135167

136168
const edit = {
137169
type: 'SetModel',
138-
model: { collections: [], relationships: [] },
139-
};
170+
model: {
171+
...model,
172+
relationships: [] as StaticModel['relationships'],
173+
},
174+
} as Edit;
140175
store.dispatch(applyEdit(edit));
141176

142177
const diagramAfterEdit = getCurrentDiagramFromState(store.getState());

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,15 @@ export function redoEdit(): DataModelingThunkAction<void, RedoEditAction> {
199199
}
200200

201201
export function applyEdit(
202-
rawEdit: Edit
202+
rawEdit: Omit<Edit, 'id' | 'timestamp'>
203203
): DataModelingThunkAction<void, ApplyEditAction | ApplyEditFailedAction> {
204204
return (dispatch, getState, { dataModelStorage }) => {
205205
const edit = {
206206
...rawEdit,
207207
id: new UUID().toString(),
208208
timestamp: new Date().toISOString(),
209-
};
209+
// TS has a problem recognizing the discriminated union
210+
} as Edit;
210211
const { result: isValid, errors } = validateEdit(edit);
211212
if (!isValid) {
212213
dispatch({

0 commit comments

Comments
 (0)