Skip to content

Commit e47687e

Browse files
committed
tests
1 parent 39793bd commit e47687e

File tree

5 files changed

+424
-4
lines changed

5 files changed

+424
-4
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from 'react';
2+
import { expect } from 'chai';
3+
import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
4+
import { DiagramEditorToolbar } from './diagram-editor-toolbar';
5+
import sinon from 'sinon';
6+
7+
function renderDiagramEditorToolbar(
8+
props: Partial<React.ComponentProps<typeof DiagramEditorToolbar>> = {}
9+
) {
10+
render(
11+
<DiagramEditorToolbar
12+
step="EDITING"
13+
hasUndo={true}
14+
hasRedo={true}
15+
onUndoClick={() => {}}
16+
onRedoClick={() => {}}
17+
onExportClick={() => {}}
18+
{...props}
19+
/>
20+
);
21+
}
22+
23+
describe('DiagramEditorToolbar', function () {
24+
it('throws if step is NO_DIAGRAM_SELECTED', function () {
25+
expect(() => {
26+
renderDiagramEditorToolbar({ step: 'NO_DIAGRAM_SELECTED' });
27+
}).to.throw('Unexpected');
28+
});
29+
30+
it('renders nothing if step is not EDITING', function () {
31+
renderDiagramEditorToolbar({ step: 'ANALYSIS_CANCELED' });
32+
expect(() => screen.getByTestId('diagram-editor-toolbar')).to.throw;
33+
});
34+
35+
context('undo button', function () {
36+
it('renders it disabled if hasUndo is false', function () {
37+
renderDiagramEditorToolbar({ hasUndo: false });
38+
const undoButton = screen.getByRole('button', { name: 'Undo' });
39+
expect(undoButton).to.have.attribute('aria-disabled', 'true');
40+
});
41+
it('renders it enabled if hasUndo is true and calls onUndoClick', function () {
42+
const undoSpy = sinon.spy();
43+
renderDiagramEditorToolbar({ hasUndo: true, onUndoClick: undoSpy });
44+
const undoButton = screen.getByRole('button', { name: 'Undo' });
45+
expect(undoButton).to.have.attribute('aria-disabled', 'false');
46+
userEvent.click(undoButton);
47+
expect(undoSpy).to.have.been.calledOnce;
48+
});
49+
});
50+
51+
context('redo button', function () {
52+
it('renders it disabled if hasRedo is false', function () {
53+
renderDiagramEditorToolbar({ hasRedo: false });
54+
const redoButton = screen.getByRole('button', { name: 'Redo' });
55+
expect(redoButton).to.have.attribute('aria-disabled', 'true');
56+
});
57+
it('renders it enabled if hasRedo is true and calls onRedoClick', function () {
58+
const redoSpy = sinon.spy();
59+
renderDiagramEditorToolbar({ hasRedo: true, onRedoClick: redoSpy });
60+
const redoButton = screen.getByRole('button', { name: 'Redo' });
61+
expect(redoButton).to.have.attribute('aria-disabled', 'false');
62+
userEvent.click(redoButton);
63+
expect(redoSpy).to.have.been.calledOnce;
64+
});
65+
});
66+
67+
it('renders export buttona and calls onExportClick', function () {
68+
const exportSpy = sinon.spy();
69+
renderDiagramEditorToolbar({ onExportClick: exportSpy });
70+
const exportButton = screen.getByRole('button', { name: 'Export' });
71+
expect(exportButton).to.exist;
72+
userEvent.click(exportButton);
73+
expect(exportSpy).to.have.been.calledOnce;
74+
});
75+
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { DataModelingState } from '../store/reducer';
44
import { redoEdit, showExportModal, undoEdit } from '../store/diagram';
55
import { Icon, IconButton } from '@mongodb-js/compass-components';
66

7-
const DiagramEditorToolbar: React.FunctionComponent<{
7+
export const DiagramEditorToolbar: React.FunctionComponent<{
88
step: DataModelingState['step'];
99
hasUndo: boolean;
1010
hasRedo: boolean;
@@ -19,7 +19,7 @@ const DiagramEditorToolbar: React.FunctionComponent<{
1919
return null;
2020
}
2121
return (
22-
<>
22+
<div data-testid="diagram-editor-toolbar">
2323
<IconButton aria-label="Undo" disabled={!hasUndo} onClick={onUndoClick}>
2424
<Icon glyph="Undo"></Icon>
2525
</IconButton>
@@ -29,7 +29,7 @@ const DiagramEditorToolbar: React.FunctionComponent<{
2929
<IconButton aria-label="Export" onClick={onExportClick}>
3030
<Icon glyph="Export"></Icon>
3131
</IconButton>
32-
</>
32+
</div>
3333
);
3434
};
3535

packages/compass-data-modeling/src/components/export-diagram-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const ExportDiagramModal = ({
7474
title="Export data model"
7575
subtitle={
7676
<div>
77-
Export the data modal to JSON Schema format.
77+
Export the data modal to JSON format.
7878
{nbsp}
7979
<Link
8080
href="https://www.mongodb.com/docs/compass/current/data-modeling/export-diagram/"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect } from 'chai';
2+
import { getExportJsonFromModel } from './export-diagram';
3+
import FlightModel from '../../test/fixtures/flights-model.json';
4+
5+
describe('export-diagram', function () {
6+
context('json export', function () {
7+
it('should convert a model to JSON', function () {
8+
const json = getExportJsonFromModel(FlightModel);
9+
10+
const expectedCollections = Object.fromEntries(
11+
FlightModel.collections
12+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
13+
.map(({ displayPosition, indexes, ...rest }) => [rest.ns, rest])
14+
);
15+
expect(json.collections).to.deep.equal(expectedCollections);
16+
17+
expect(json.relationships.length).to.equal(
18+
FlightModel.relationships.length
19+
);
20+
});
21+
});
22+
});

0 commit comments

Comments
 (0)