|
| 1 | +import React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { |
| 4 | + createPluginTestHelpers, |
| 5 | + screen, |
| 6 | + waitFor, |
| 7 | + userEvent, |
| 8 | + within, |
| 9 | +} from '@mongodb-js/testing-library-compass'; |
| 10 | +import { DataModelingWorkspaceTab } from '../index'; |
| 11 | +import DiagramEditorSidePanel from './diagram-editor-side-panel'; |
| 12 | +import { |
| 13 | + getCurrentDiagramFromState, |
| 14 | + openDiagram, |
| 15 | + selectCollection, |
| 16 | + selectCurrentModel, |
| 17 | + selectRelationship, |
| 18 | +} from '../store/diagram'; |
| 19 | +import dataModel from '../../test/fixtures/data-model-with-relationships.json'; |
| 20 | +import type { MongoDBDataModelDescription } from '../services/data-model-storage'; |
| 21 | + |
| 22 | +async function comboboxSelectItem( |
| 23 | + label: string, |
| 24 | + value: string, |
| 25 | + visibleLabel = value |
| 26 | +) { |
| 27 | + userEvent.click(screen.getByRole('textbox', { name: label })); |
| 28 | + await waitFor(() => { |
| 29 | + screen.getByRole('option', { name: visibleLabel }); |
| 30 | + }); |
| 31 | + userEvent.click(screen.getByRole('option', { name: visibleLabel })); |
| 32 | + await waitFor(() => { |
| 33 | + expect(screen.getByRole('textbox', { name: label })).to.have.attribute( |
| 34 | + 'value', |
| 35 | + value |
| 36 | + ); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +describe('DiagramEditorSidePanel', function () { |
| 41 | + function renderDrawer() { |
| 42 | + const { renderWithConnections } = createPluginTestHelpers( |
| 43 | + DataModelingWorkspaceTab.provider.withMockServices({}) |
| 44 | + ); |
| 45 | + const result = renderWithConnections( |
| 46 | + <DiagramEditorSidePanel></DiagramEditorSidePanel> |
| 47 | + ); |
| 48 | + result.plugin.store.dispatch( |
| 49 | + openDiagram(dataModel as MongoDBDataModelDescription) |
| 50 | + ); |
| 51 | + return result; |
| 52 | + } |
| 53 | + |
| 54 | + it('should not render if no items are selected', function () { |
| 55 | + renderDrawer(); |
| 56 | + expect(screen.queryByTestId('data-modeling-drawer')).to.eq(null); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should render a collection context drawer when collection is clicked', function () { |
| 60 | + const result = renderDrawer(); |
| 61 | + result.plugin.store.dispatch(selectCollection('flights.airlines')); |
| 62 | + expect(screen.getByText('flights.airlines')).to.be.visible; |
| 63 | + }); |
| 64 | + |
| 65 | + it('should render a relationship context drawer when relations is clicked', function () { |
| 66 | + const result = renderDrawer(); |
| 67 | + result.plugin.store.dispatch( |
| 68 | + selectRelationship('204b1fc0-601f-4d62-bba3-38fade71e049') |
| 69 | + ); |
| 70 | + expect(screen.getByText('Edit Relationship')).to.be.visible; |
| 71 | + expect( |
| 72 | + document.querySelector( |
| 73 | + '[data-relationship-id="204b1fc0-601f-4d62-bba3-38fade71e049"]' |
| 74 | + ) |
| 75 | + ).to.be.visible; |
| 76 | + }); |
| 77 | + |
| 78 | + it('should change the content of the drawer when selecting different items', function () { |
| 79 | + const result = renderDrawer(); |
| 80 | + |
| 81 | + result.plugin.store.dispatch(selectCollection('flights.airlines')); |
| 82 | + expect(screen.getByText('flights.airlines')).to.be.visible; |
| 83 | + |
| 84 | + result.plugin.store.dispatch( |
| 85 | + selectCollection('flights.airports_coordinates_for_schema') |
| 86 | + ); |
| 87 | + expect(screen.getByText('flights.airports_coordinates_for_schema')).to.be |
| 88 | + .visible; |
| 89 | + |
| 90 | + result.plugin.store.dispatch( |
| 91 | + selectRelationship('204b1fc0-601f-4d62-bba3-38fade71e049') |
| 92 | + ); |
| 93 | + expect( |
| 94 | + document.querySelector( |
| 95 | + '[data-relationship-id="204b1fc0-601f-4d62-bba3-38fade71e049"]' |
| 96 | + ) |
| 97 | + ).to.be.visible; |
| 98 | + |
| 99 | + result.plugin.store.dispatch( |
| 100 | + selectRelationship('6f776467-4c98-476b-9b71-1f8a724e6c2c') |
| 101 | + ); |
| 102 | + expect( |
| 103 | + document.querySelector( |
| 104 | + '[data-relationship-id="6f776467-4c98-476b-9b71-1f8a724e6c2c"]' |
| 105 | + ) |
| 106 | + ).to.be.visible; |
| 107 | + |
| 108 | + result.plugin.store.dispatch(selectCollection('flights.planes')); |
| 109 | + expect(screen.getByText('flights.planes')).to.be.visible; |
| 110 | + }); |
| 111 | + |
| 112 | + it('should open and edit relationship starting from collection', async function () { |
| 113 | + const result = renderDrawer(); |
| 114 | + result.plugin.store.dispatch(selectCollection('flights.countries')); |
| 115 | + |
| 116 | + // Open relationshipt editing form |
| 117 | + const relationshipCard = document.querySelector<HTMLElement>( |
| 118 | + '[data-relationship-id="204b1fc0-601f-4d62-bba3-38fade71e049"]' |
| 119 | + ); |
| 120 | + userEvent.click( |
| 121 | + within(relationshipCard!).getByRole('button', { name: 'Edit' }) |
| 122 | + ); |
| 123 | + expect(screen.getByText('Edit Relationship')).to.be.visible; |
| 124 | + |
| 125 | + // Select new values |
| 126 | + await comboboxSelectItem('Local collection', 'planes'); |
| 127 | + await comboboxSelectItem('Local field', 'name'); |
| 128 | + await comboboxSelectItem('Foreign collection', 'countries'); |
| 129 | + await comboboxSelectItem('Foreign field', 'iso_code'); |
| 130 | + |
| 131 | + // We should be testing through rendered UI but as it's really hard to make |
| 132 | + // diagram rendering in tests property, we are just validating the final |
| 133 | + // model here |
| 134 | + const modifiedRelationship = selectCurrentModel( |
| 135 | + getCurrentDiagramFromState(result.plugin.store.getState()).edits |
| 136 | + ).relationships.find((r) => { |
| 137 | + return r.id === '204b1fc0-601f-4d62-bba3-38fade71e049'; |
| 138 | + }); |
| 139 | + |
| 140 | + expect(modifiedRelationship) |
| 141 | + .to.have.property('relationship') |
| 142 | + .deep.eq([ |
| 143 | + { |
| 144 | + ns: 'flights.planes', |
| 145 | + fields: ['name'], |
| 146 | + cardinality: 1, |
| 147 | + }, |
| 148 | + { |
| 149 | + ns: 'flights.countries', |
| 150 | + fields: ['iso_code'], |
| 151 | + cardinality: 1, |
| 152 | + }, |
| 153 | + ]); |
| 154 | + }); |
| 155 | +}); |
0 commit comments