|
| 1 | +import React from 'react'; |
| 2 | +import Sinon from 'sinon'; |
| 3 | +import { |
| 4 | + CompassIndexesPlugin as CompassIndexesSubtab, |
| 5 | + CompassIndexesHadronPlugin, |
| 6 | +} from './index'; |
| 7 | +import { |
| 8 | + createDefaultConnectionInfo, |
| 9 | + createPluginTestHelpers, |
| 10 | + screen, |
| 11 | + userEvent, |
| 12 | + waitFor, |
| 13 | + within, |
| 14 | +} from '@mongodb-js/testing-library-compass'; |
| 15 | +import { expect } from 'chai'; |
| 16 | +import { |
| 17 | + indexCreationStarted, |
| 18 | + prepareInProgressIndex, |
| 19 | + rollingIndexTimeoutCheck, |
| 20 | +} from './modules/regular-indexes'; |
| 21 | + |
| 22 | +describe('CompassIndexesPlugin', function () { |
| 23 | + const sandbox = Sinon.createSandbox(); |
| 24 | + |
| 25 | + const dataService = { |
| 26 | + indexes: () => [], |
| 27 | + createIndex: () => ({}), |
| 28 | + }; |
| 29 | + |
| 30 | + const atlasService = { |
| 31 | + automationAgentRequest: () => ({}), |
| 32 | + automationAgentAwait: () => ({ response: [] }), |
| 33 | + authenticatedFetch: () => undefined, |
| 34 | + cloudEndpoint: () => undefined, |
| 35 | + }; |
| 36 | + |
| 37 | + const renderHelpers = createPluginTestHelpers( |
| 38 | + CompassIndexesHadronPlugin.withMockServices({ |
| 39 | + dataService, |
| 40 | + atlasService, |
| 41 | + instance: { |
| 42 | + on: () => undefined, |
| 43 | + removeListener: () => undefined, |
| 44 | + isWritable: true, |
| 45 | + }, |
| 46 | + collection: { |
| 47 | + on: () => undefined, |
| 48 | + removeListener: () => undefined, |
| 49 | + toJSON: () => ({}), |
| 50 | + }, |
| 51 | + } as any) |
| 52 | + ); |
| 53 | + |
| 54 | + function render() { |
| 55 | + return renderHelpers.renderWithActiveConnection( |
| 56 | + <CompassIndexesSubtab.content></CompassIndexesSubtab.content>, |
| 57 | + { |
| 58 | + ...createDefaultConnectionInfo(), |
| 59 | + atlasMetadata: { |
| 60 | + instanceSize: 'VERY BIG', |
| 61 | + metricsType: 'replicaSet', |
| 62 | + } as any, |
| 63 | + } |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + afterEach(function () { |
| 68 | + sandbox.reset(); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('rolling indexes', function () { |
| 72 | + it('should create a rolling index and show it in the table', async function () { |
| 73 | + const result = await render(); |
| 74 | + |
| 75 | + await waitFor(() => { |
| 76 | + expect(result.plugin.store.getState().regularIndexes).to.have.property( |
| 77 | + 'status', |
| 78 | + 'READY' |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + sandbox.stub(atlasService, 'automationAgentAwait').resolves({ |
| 83 | + response: [ |
| 84 | + { |
| 85 | + status: 'rolling build', |
| 86 | + indexName: 'field_a_1', |
| 87 | + indexProperties: {}, |
| 88 | + indexType: { label: 'regular' }, |
| 89 | + keys: [{ name: 'field_a', value: '1' }], |
| 90 | + }, |
| 91 | + ], |
| 92 | + }); |
| 93 | + |
| 94 | + /** Create rolling index */ |
| 95 | + |
| 96 | + userEvent.click(screen.getByRole('button', { name: 'Create Index' })); |
| 97 | + |
| 98 | + userEvent.type(screen.getByLabelText('Index fields'), 'field_a'); |
| 99 | + userEvent.click(screen.getByRole('option', { name: 'Field: "field_a"' })); |
| 100 | + |
| 101 | + userEvent.click(screen.getByText('Select a type')); |
| 102 | + userEvent.click(screen.getByText('1 (asc)')); |
| 103 | + |
| 104 | + userEvent.click(screen.getByTestId('create-index-modal-toggle-options')); |
| 105 | + |
| 106 | + userEvent.click( |
| 107 | + screen.getByRole('checkbox', { name: 'Build in rolling process' }), |
| 108 | + undefined, |
| 109 | + { skipPointerEventsCheck: true } |
| 110 | + ); |
| 111 | + |
| 112 | + userEvent.click( |
| 113 | + within(screen.getByTestId('create-index-modal')).getByRole('button', { |
| 114 | + name: 'Create Index', |
| 115 | + }) |
| 116 | + ); |
| 117 | + |
| 118 | + const inProgressIndex = |
| 119 | + result.plugin.store.getState().regularIndexes.inProgressIndexes[0]; |
| 120 | + |
| 121 | + expect(inProgressIndex).to.exist; |
| 122 | + |
| 123 | + /** Wait for the row to appear with in-progress bagde first */ |
| 124 | + |
| 125 | + expect( |
| 126 | + within(screen.getByTestId('indexes-row-field_a_1')).getByText( |
| 127 | + 'In Progress' |
| 128 | + ) |
| 129 | + ).to.exist; |
| 130 | + |
| 131 | + /** Now wait for the rolling index to show up */ |
| 132 | + |
| 133 | + await waitFor(() => { |
| 134 | + expect( |
| 135 | + within(screen.getByTestId('indexes-row-field_a_1')).getByText( |
| 136 | + 'Building' |
| 137 | + ) |
| 138 | + ).to.exist; |
| 139 | + }); |
| 140 | + |
| 141 | + /** At some point rolling index timeout check fires, but nothing changes */ |
| 142 | + |
| 143 | + result.plugin.store.dispatch( |
| 144 | + rollingIndexTimeoutCheck(inProgressIndex.id) |
| 145 | + ); |
| 146 | + |
| 147 | + expect( |
| 148 | + within(screen.getByTestId('indexes-row-field_a_1')).getByText( |
| 149 | + 'Building' |
| 150 | + ) |
| 151 | + ).to.exist; |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should remove in progress rolling index on a timeout', async function () { |
| 156 | + const result = await render(); |
| 157 | + |
| 158 | + await waitFor(() => { |
| 159 | + expect(result.plugin.store.getState().regularIndexes).to.have.property( |
| 160 | + 'status', |
| 161 | + 'READY' |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + /** This time let's just do all the setup with actions directly, we tested UI above */ |
| 166 | + |
| 167 | + const inProgressIndex = prepareInProgressIndex('test', { |
| 168 | + name: 'test_index', |
| 169 | + spec: {}, |
| 170 | + }); |
| 171 | + |
| 172 | + result.plugin.store.dispatch(indexCreationStarted(inProgressIndex)); |
| 173 | + |
| 174 | + expect(screen.getByTestId('indexes-row-test_index')).to.exist; |
| 175 | + |
| 176 | + /** Timeout check "fired" before index was returned from the API, index is removed */ |
| 177 | + |
| 178 | + result.plugin.store.dispatch(rollingIndexTimeoutCheck('test')); |
| 179 | + |
| 180 | + expect(() => screen.getByTestId('indexes-row-test_index')).to.throw(); |
| 181 | + }); |
| 182 | +}); |
0 commit comments