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