|
| 1 | +import React from 'react'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { screen, waitFor } from '@mongodb-js/testing-library-compass'; |
| 4 | +import AnalysisProgressStatus from './analysis-progress-status'; |
| 5 | +import { renderWithStore, testConnections } from '../../test/setup-store'; |
| 6 | +import { |
| 7 | + AnalysisProcessActionTypes, |
| 8 | + startAnalysis, |
| 9 | +} from '../store/analysis-process'; |
| 10 | +import { createSandboxFromDefaultPreferences } from 'compass-preferences-model'; |
| 11 | + |
| 12 | +describe('AnalysisProgressStatus', () => { |
| 13 | + async function renderAnalysisProgressStatus({ |
| 14 | + automaticallyInferRelations = false, |
| 15 | + } = {}) { |
| 16 | + const preferences = await createSandboxFromDefaultPreferences(); |
| 17 | + const { store } = renderWithStore(<AnalysisProgressStatus />, { |
| 18 | + services: { |
| 19 | + preferences, |
| 20 | + }, |
| 21 | + }); |
| 22 | + void store.dispatch( |
| 23 | + startAnalysis( |
| 24 | + 'My Diagram', |
| 25 | + testConnections[0].id, |
| 26 | + 'testDB', |
| 27 | + ['coll1', 'coll2', 'coll3'], |
| 28 | + { automaticallyInferRelations } |
| 29 | + ) |
| 30 | + ); |
| 31 | + return store; |
| 32 | + } |
| 33 | + |
| 34 | + it('Allows cancellation', async () => { |
| 35 | + const store = await renderAnalysisProgressStatus(); |
| 36 | + expect(screen.getByText('Sampling collections…')).to.be.visible; |
| 37 | + expect(screen.getByText('Cancel')).to.be.visible; |
| 38 | + screen.getByText('Cancel').click(); |
| 39 | + await waitFor(() => { |
| 40 | + expect(store.getState().analysisProgress.step).to.equal('IDLE'); |
| 41 | + expect(store.getState().diagram).to.be.null; |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('Keeps showing progress along the way', () => { |
| 46 | + it('Without relationship inferring', async () => { |
| 47 | + const store = await renderAnalysisProgressStatus({ |
| 48 | + automaticallyInferRelations: false, |
| 49 | + }); |
| 50 | + expect(screen.getByText('Sampling collections…')).to.be.visible; |
| 51 | + expect(screen.getByText('0/3')).to.be.visible; |
| 52 | + |
| 53 | + // 2 out of 3 samples fetched, 1 analyzed |
| 54 | + store.dispatch({ |
| 55 | + type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED, |
| 56 | + }); |
| 57 | + store.dispatch({ |
| 58 | + type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED, |
| 59 | + }); |
| 60 | + store.dispatch({ |
| 61 | + type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED, |
| 62 | + }); |
| 63 | + |
| 64 | + expect(screen.getByText('Sampling collections…')).to.be.visible; |
| 65 | + expect(screen.getByText('2/3')).to.be.visible; |
| 66 | + |
| 67 | + // Last sample fetched |
| 68 | + store.dispatch({ |
| 69 | + type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(screen.getByText('Analyzing collection schemas…')).to.be.visible; |
| 73 | + expect(screen.getByText('1/3')).to.be.visible; |
| 74 | + |
| 75 | + // Finish analyzing |
| 76 | + store.dispatch({ |
| 77 | + type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED, |
| 78 | + }); |
| 79 | + store.dispatch({ |
| 80 | + type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED, |
| 81 | + }); |
| 82 | + |
| 83 | + expect(screen.queryByText('Inferring relationships between collections…')) |
| 84 | + .not.to.exist; |
| 85 | + expect(screen.getByText('Preparing diagram…')).to.be.visible; |
| 86 | + }); |
| 87 | + |
| 88 | + it('With relationship inferring', async () => { |
| 89 | + const store = await renderAnalysisProgressStatus({ |
| 90 | + automaticallyInferRelations: true, |
| 91 | + }); |
| 92 | + expect(screen.getByText('Sampling collections…')).to.be.visible; |
| 93 | + expect(screen.getByText('0/3')).to.be.visible; |
| 94 | + |
| 95 | + // Fetch and analyze all samples |
| 96 | + store.dispatch({ |
| 97 | + type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED, |
| 98 | + }); |
| 99 | + store.dispatch({ |
| 100 | + type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED, |
| 101 | + }); |
| 102 | + store.dispatch({ |
| 103 | + type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED, |
| 104 | + }); |
| 105 | + store.dispatch({ |
| 106 | + type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED, |
| 107 | + }); |
| 108 | + store.dispatch({ |
| 109 | + type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED, |
| 110 | + }); |
| 111 | + store.dispatch({ |
| 112 | + type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED, |
| 113 | + }); |
| 114 | + |
| 115 | + expect(screen.getByText('Inferring relationships between collections…')) |
| 116 | + .to.be.visible; |
| 117 | + expect(screen.queryByText('0/3')).not.to.exist; |
| 118 | + |
| 119 | + // Infer some relationships |
| 120 | + store.dispatch({ |
| 121 | + type: AnalysisProcessActionTypes.NAMESPACE_RELATIONS_INFERRED, |
| 122 | + }); |
| 123 | + store.dispatch({ |
| 124 | + type: AnalysisProcessActionTypes.NAMESPACE_RELATIONS_INFERRED, |
| 125 | + }); |
| 126 | + |
| 127 | + expect(screen.getByText('Inferring relationships between collections…')) |
| 128 | + .to.be.visible; |
| 129 | + expect(screen.queryByText('2/3')).not.to.exist; |
| 130 | + |
| 131 | + // Finish inferring |
| 132 | + store.dispatch({ |
| 133 | + type: AnalysisProcessActionTypes.NAMESPACE_RELATIONS_INFERRED, |
| 134 | + }); |
| 135 | + |
| 136 | + expect(screen.getByText('Preparing diagram…')).to.be.visible; |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments