|
| 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 | +}); |
0 commit comments