@@ -26,11 +26,7 @@ const mockDiagramming = {
26
26
27
27
import React from 'react' ;
28
28
import { expect } from 'chai' ;
29
- import {
30
- screen ,
31
- userEvent ,
32
- waitFor ,
33
- } from '@mongodb-js/testing-library-compass' ;
29
+ import { screen , waitFor } from '@mongodb-js/testing-library-compass' ;
34
30
import DiagramEditor from './diagram-editor' ;
35
31
import { renderWithOpenedDiagramStore } from '../../test/setup-store' ;
36
32
import type { DataModelingStore } from '../../test/setup-store' ;
@@ -43,10 +39,43 @@ import { DiagramProvider } from '@mongodb-js/diagramming';
43
39
44
40
const storageItems : MongoDBDataModelDescription [ ] = [
45
41
{
46
- id : '1 ' ,
42
+ id : 'existing-diagram-id ' ,
47
43
name : 'One' ,
48
44
createdAt : '2023-10-01T00:00:00.000Z' ,
49
45
updatedAt : '2023-10-03T00:00:00.000Z' ,
46
+ edits : [
47
+ {
48
+ id : 'edit-id-1' ,
49
+ timestamp : '2023-10-02T00:00:00.000Z' ,
50
+ type : 'SetModel' ,
51
+ model : {
52
+ collections : [
53
+ {
54
+ ns : 'db1.collection1' ,
55
+ indexes : [ ] ,
56
+ displayPosition : [ 50 , 50 ] ,
57
+ shardKey : { } ,
58
+ jsonSchema : { bsonType : 'object' } ,
59
+ } ,
60
+ {
61
+ ns : 'db1.collection2' ,
62
+ indexes : [ ] ,
63
+ displayPosition : [ 150 , 150 ] ,
64
+ shardKey : { } ,
65
+ jsonSchema : { bsonType : 'object' } ,
66
+ } ,
67
+ ] ,
68
+ relationships : [ ] ,
69
+ } ,
70
+ } ,
71
+ ] ,
72
+ connectionId : null ,
73
+ } ,
74
+ {
75
+ id : 'new-diagram-id' ,
76
+ name : 'Two' ,
77
+ createdAt : '2023-10-01T00:00:00.000Z' ,
78
+ updatedAt : '2023-10-03T00:00:00.000Z' ,
50
79
edits : [
51
80
{
52
81
id : 'edit-id-1' ,
@@ -79,8 +108,10 @@ const storageItems: MongoDBDataModelDescription[] = [
79
108
80
109
const renderDiagramEditor = async ( {
81
110
items = storageItems ,
111
+ renderedItem = items [ 0 ] ,
82
112
} : {
83
113
items ?: MongoDBDataModelDescription [ ] ;
114
+ renderedItem ?: MongoDBDataModelDescription ;
84
115
} = { } ) => {
85
116
const mockDataModelStorage = {
86
117
status : 'READY' ,
@@ -108,38 +139,73 @@ const renderDiagramEditor = async ({
108
139
dataModelStorage : mockDataModelStorage ,
109
140
} ,
110
141
} ,
111
- items [ 0 ]
142
+ renderedItem
112
143
) ;
113
144
return result ;
114
145
} ;
115
146
116
147
describe . only ( 'DiagramEditor' , function ( ) {
117
148
let store : DataModelingStore ;
118
149
119
- beforeEach ( async function ( ) {
120
- const result = await renderDiagramEditor ( ) ;
121
- store = result . store ;
150
+ context ( 'with initial diagram' , function ( ) {
151
+ beforeEach ( async function ( ) {
152
+ const result = await renderDiagramEditor ( {
153
+ renderedItem : storageItems [ 1 ] ,
154
+ } ) ;
155
+ store = result . store ;
156
+
157
+ // wait till the editor is loaded
158
+ await waitFor ( ( ) => {
159
+ expect ( screen . getByTestId ( 'model-preview' ) ) . to . be . visible ;
160
+ } ) ;
161
+ } ) ;
162
+
163
+ it ( 'applies the initial layout to unpositioned nodes' , async function ( ) {
164
+ const state = store . getState ( ) ;
122
165
123
- // wait till the editor is loaded
124
- await waitFor ( ( ) => {
125
- expect ( screen . getByTestId ( 'model-preview' ) ) . to . be . visible ;
166
+ expect ( state . diagram ?. edits . current ) . to . have . lengthOf ( 1 ) ;
167
+ expect ( state . diagram ?. edits . current [ 0 ] . type ) . to . equal ( 'SetModel' ) ;
168
+ const initialEdit = state . diagram ?. edits . current [ 0 ] as Extract <
169
+ Edit ,
170
+ { type : 'SetModel' }
171
+ > ;
172
+ expect ( initialEdit . model ?. collections [ 0 ] . displayPosition ) . to . deep . equal ( [
173
+ 100 , 100 ,
174
+ ] ) ;
175
+ expect ( initialEdit . model ?. collections [ 1 ] . displayPosition ) . to . deep . equal ( [
176
+ 200 , 200 ,
177
+ ] ) ;
126
178
} ) ;
127
179
} ) ;
128
180
129
- it ( 'applies the initial layout to unpositioned nodes' , async function ( ) {
130
- const state = store . getState ( ) ;
181
+ context ( 'with existing diagram' , function ( ) {
182
+ beforeEach ( async function ( ) {
183
+ const result = await renderDiagramEditor ( {
184
+ renderedItem : storageItems [ 0 ] ,
185
+ } ) ;
186
+ store = result . store ;
131
187
132
- expect ( state . diagram ?. edits . current ) . to . have . lengthOf ( 1 ) ;
133
- expect ( state . diagram ?. edits . current [ 0 ] . type ) . to . equal ( 'SetModel' ) ;
134
- const initialEdit = state . diagram ?. edits . current [ 0 ] as Extract <
135
- Edit ,
136
- { type : 'SetModel' }
137
- > ;
138
- expect ( initialEdit . model ?. collections [ 0 ] . displayPosition ) . to . deep . equal ( [
139
- 100 , 100 ,
140
- ] ) ;
141
- expect ( initialEdit . model ?. collections [ 1 ] . displayPosition ) . to . deep . equal ( [
142
- 200 , 200 ,
143
- ] ) ;
188
+ // wait till the editor is loaded
189
+ await waitFor ( ( ) => {
190
+ expect ( screen . getByTestId ( 'model-preview' ) ) . to . be . visible ;
191
+ } ) ;
192
+ } ) ;
193
+
194
+ it ( 'does not change the position of the nodes' , async function ( ) {
195
+ const state = store . getState ( ) ;
196
+
197
+ expect ( state . diagram ?. edits . current ) . to . have . lengthOf ( 1 ) ;
198
+ expect ( state . diagram ?. edits . current [ 0 ] . type ) . to . equal ( 'SetModel' ) ;
199
+ const initialEdit = state . diagram ?. edits . current [ 0 ] as Extract <
200
+ Edit ,
201
+ { type : 'SetModel' }
202
+ > ;
203
+ expect ( initialEdit . model ?. collections [ 0 ] . displayPosition ) . to . deep . equal (
204
+ storageItems [ 0 ] . edits [ 0 ] . model . collections [ 0 ] . displayPosition
205
+ ) ;
206
+ expect ( initialEdit . model ?. collections [ 1 ] . displayPosition ) . to . deep . equal (
207
+ storageItems [ 0 ] . edits [ 0 ] . model . collections [ 1 ] . displayPosition
208
+ ) ;
209
+ } ) ;
144
210
} ) ;
145
211
} ) ;
0 commit comments