|
| 1 | +import React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import sinon from 'sinon'; |
| 4 | + |
| 5 | +import { |
| 6 | + render, |
| 7 | + screen, |
| 8 | + userEvent, |
| 9 | + within, |
| 10 | +} from '@mongodb-js/testing-library-compass'; |
| 11 | +import { setupStore } from '../../../test/setup-store'; |
| 12 | +import { Provider } from 'react-redux'; |
| 13 | + |
| 14 | +import CreateIndexActions from '.'; |
| 15 | + |
| 16 | +describe('CreateIndexActions Component', function () { |
| 17 | + let clearErrorSpy; |
| 18 | + let onCreateIndexClickSpy; |
| 19 | + let closeCreateIndexModalSpy; |
| 20 | + const store = setupStore(); |
| 21 | + |
| 22 | + beforeEach(function () { |
| 23 | + clearErrorSpy = sinon.spy(); |
| 24 | + onCreateIndexClickSpy = sinon.spy(); |
| 25 | + closeCreateIndexModalSpy = sinon.spy(); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(function () { |
| 29 | + clearErrorSpy = null; |
| 30 | + onCreateIndexClickSpy = null; |
| 31 | + closeCreateIndexModalSpy = null; |
| 32 | + }); |
| 33 | + |
| 34 | + const renderComponent = (error?: string) => { |
| 35 | + render( |
| 36 | + <Provider store={store}> |
| 37 | + <CreateIndexActions |
| 38 | + error={error || null} |
| 39 | + onErrorBannerCloseClick={clearErrorSpy} |
| 40 | + onCreateIndexClick={onCreateIndexClickSpy} |
| 41 | + onCancelCreateIndexClick={closeCreateIndexModalSpy} |
| 42 | + showIndexesGuidanceVariant={false} |
| 43 | + /> |
| 44 | + </Provider> |
| 45 | + ); |
| 46 | + }; |
| 47 | + it('renders a cancel button', function () { |
| 48 | + renderComponent(); |
| 49 | + |
| 50 | + const button = screen.getByTestId('create-index-actions-cancel-button'); |
| 51 | + expect(button.textContent).to.be.equal('Cancel'); |
| 52 | + }); |
| 53 | + |
| 54 | + context('onCancel', function () { |
| 55 | + it('calls the closeCreateIndexModal function', function () { |
| 56 | + renderComponent(); |
| 57 | + |
| 58 | + const button = screen.getByTestId('create-index-actions-cancel-button'); |
| 59 | + userEvent.click(button); |
| 60 | + expect(closeCreateIndexModalSpy).to.have.been.calledOnce; |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + context('onConfirm', function () { |
| 65 | + it('calls the onCreateIndexClick function', function () { |
| 66 | + renderComponent(); |
| 67 | + |
| 68 | + const button = screen.getByTestId( |
| 69 | + 'create-index-actions-create-index-button' |
| 70 | + ); |
| 71 | + userEvent.click(button); |
| 72 | + expect(onCreateIndexClickSpy).to.have.been.calledOnce; |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('renders a create index button', function () { |
| 77 | + renderComponent(); |
| 78 | + const button = screen.getByTestId( |
| 79 | + 'create-index-actions-create-index-button' |
| 80 | + ); |
| 81 | + expect(button.textContent).to.be.equal('Create Index'); |
| 82 | + }); |
| 83 | + |
| 84 | + context('with error', function () { |
| 85 | + it('renders error banner', function () { |
| 86 | + renderComponent('Some error happened!'); |
| 87 | + |
| 88 | + const errorBanner = screen.getByTestId( |
| 89 | + 'create-index-actions-error-banner-wrapper' |
| 90 | + ); |
| 91 | + expect(errorBanner).to.contain.text('Some error happened!'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('closes error banner', function () { |
| 95 | + renderComponent('Some error happened!'); |
| 96 | + |
| 97 | + const errorBanner = screen.getByTestId( |
| 98 | + 'create-index-actions-error-banner-wrapper' |
| 99 | + ); |
| 100 | + const closeIcon = within(errorBanner).getByLabelText('X Icon'); |
| 101 | + |
| 102 | + userEvent.click(closeIcon); |
| 103 | + expect(clearErrorSpy).to.have.been.calledOnce; |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + context('without error', function () { |
| 108 | + it('does not render error banner', function () { |
| 109 | + renderComponent(); |
| 110 | + |
| 111 | + const errorBanner = screen.queryByTestId( |
| 112 | + 'create-index-actions-error-banner-wrapper' |
| 113 | + ); |
| 114 | + expect(errorBanner).to.not.exist; |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments