|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { setupStore } from '../../test/setup-store'; |
| 3 | +import { applyEdit, openDiagram } from './diagram'; |
| 4 | + |
| 5 | +describe('Data Modeling store', function () { |
| 6 | + let store; |
| 7 | + |
| 8 | + beforeEach(function () { |
| 9 | + store = setupStore(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('openDiagram', function () { |
| 13 | + const diagram = { |
| 14 | + id: 'diagram-id', |
| 15 | + name: 'diagram-name', |
| 16 | + connectionId: 'connection-id', |
| 17 | + edits: [{ type: 'SetModel', model: {} }], |
| 18 | + }; |
| 19 | + |
| 20 | + store.dispatch(openDiagram(diagram)); |
| 21 | + |
| 22 | + expect(store.getState().diagram.id).to.equal(diagram.id); |
| 23 | + expect(store.getState().diagram.name).to.equal(diagram.name); |
| 24 | + expect(store.getState().diagram.connectionId).to.equal( |
| 25 | + diagram.connectionId |
| 26 | + ); |
| 27 | + expect(store.getState().diagram.edits.current).to.deep.equal(diagram.edits); |
| 28 | + }); |
| 29 | + |
| 30 | + it('openDiagram', function () { |
| 31 | + const diagram = { |
| 32 | + id: 'diagram-id', |
| 33 | + name: 'diagram-name', |
| 34 | + connectionId: 'connection-id', |
| 35 | + edits: [{ type: 'SetModel', model: {} }], |
| 36 | + }; |
| 37 | + |
| 38 | + store.dispatch(openDiagram(diagram)); |
| 39 | + |
| 40 | + expect(store.getState().diagram.id).to.equal(diagram.id); |
| 41 | + expect(store.getState().diagram.name).to.equal(diagram.name); |
| 42 | + expect(store.getState().diagram.connectionId).to.equal( |
| 43 | + diagram.connectionId |
| 44 | + ); |
| 45 | + expect(store.getState().diagram.edits.current).to.deep.equal(diagram.edits); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('applyEdit', function () { |
| 49 | + it('should apply a valid SetModel edit', function () { |
| 50 | + const diagram = { |
| 51 | + id: 'diagram-id', |
| 52 | + name: 'diagram-name', |
| 53 | + connectionId: 'connection-id', |
| 54 | + edits: [{ type: 'SetModel', model: {} }], |
| 55 | + }; |
| 56 | + |
| 57 | + store.dispatch(openDiagram(diagram)); |
| 58 | + |
| 59 | + const newModel = { collections: [], relationships: [] }; |
| 60 | + store.dispatch(applyEdit({ type: 'SetModel', model: newModel })); |
| 61 | + |
| 62 | + const state = store.getState(); |
| 63 | + expect(state.diagram.editErrors).to.be.undefined; |
| 64 | + expect(state.diagram.edits.current[0]).to.deep.equal(diagram.edits[0]); |
| 65 | + expect(state.diagram.edits.current[1]).to.deep.include({ |
| 66 | + type: 'SetModel', |
| 67 | + model: newModel, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should apply a valid AddRelationship edit', function () { |
| 72 | + const diagram = { |
| 73 | + id: 'diagram-id', |
| 74 | + name: 'diagram-name', |
| 75 | + connectionId: 'connection-id', |
| 76 | + edits: [{ type: 'SetModel', model: {} }], |
| 77 | + }; |
| 78 | + |
| 79 | + store.dispatch(openDiagram(diagram)); |
| 80 | + |
| 81 | + const newRelationship = { |
| 82 | + id: 'relationship1', |
| 83 | + relationship: [ |
| 84 | + { |
| 85 | + ns: 'db.sourceCollection', |
| 86 | + cardinality: 1, |
| 87 | + fields: ['field1'], |
| 88 | + }, |
| 89 | + { |
| 90 | + ns: 'db.targetCollection', |
| 91 | + cardinality: 1, |
| 92 | + fields: ['field2'], |
| 93 | + }, |
| 94 | + ], |
| 95 | + isInferred: false, |
| 96 | + }; |
| 97 | + store.dispatch( |
| 98 | + applyEdit({ type: 'AddRelationship', relationship: newRelationship }) |
| 99 | + ); |
| 100 | + |
| 101 | + const state = store.getState(); |
| 102 | + expect(state.diagram.editErrors).to.be.undefined; |
| 103 | + expect(state.diagram.edits.current[0]).to.deep.equal(diagram.edits[0]); |
| 104 | + expect(state.diagram.edits.current[1]).to.deep.include({ |
| 105 | + type: 'AddRelationship', |
| 106 | + relationship: newRelationship, |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it.only('should not apply invalid AddRelationship edit', function () { |
| 111 | + const diagram = { |
| 112 | + id: 'diagram-id', |
| 113 | + name: 'diagram-name', |
| 114 | + connectionId: 'connection-id', |
| 115 | + edits: [{ type: 'SetModel', model: {} }], |
| 116 | + }; |
| 117 | + |
| 118 | + store.dispatch(openDiagram(diagram)); |
| 119 | + |
| 120 | + const newRelationship = { |
| 121 | + id: 'relationship1', |
| 122 | + isInferred: false, |
| 123 | + }; |
| 124 | + store.dispatch( |
| 125 | + applyEdit({ type: 'AddRelationship', relationship: newRelationship }) |
| 126 | + ); |
| 127 | + |
| 128 | + const state = store.getState(); |
| 129 | + expect(state.diagram.editErrors).to.have.length(1); |
| 130 | + expect(state.diagram.editErrors[0]).to.equal( |
| 131 | + "'relationship,relationship' is required" |
| 132 | + ); |
| 133 | + expect(state.diagram.edits.current).to.deep.equal(diagram.edits); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments