@@ -2,61 +2,165 @@ import React from 'react';
2
2
import { render , screen } from '@mongodb-js/testing-library-compass' ;
3
3
import IndexFlowSection from './index-flow-section' ;
4
4
import { expect } from 'chai' ;
5
+ import type { Field } from '../../modules/create-index' ;
5
6
6
7
describe ( 'IndexFlowSection' , ( ) => {
7
- const renderComponent = ( createIndexFieldsComponent ?: JSX . Element ) => {
8
+ const renderComponent = ( {
9
+ createIndexFieldsComponent,
10
+ fields,
11
+ } : {
12
+ createIndexFieldsComponent ?: JSX . Element ;
13
+ fields ?: Field [ ] ;
14
+ } ) => {
8
15
render (
9
16
< IndexFlowSection
10
17
createIndexFieldsComponent = { createIndexFieldsComponent ?? null }
11
- fields = { [ ] }
18
+ fields = { fields || [ ] }
12
19
dbName = { 'fakeDBName' }
13
20
collectionName = { 'fakeCollectionName' }
14
21
/>
15
22
) ;
16
23
} ;
17
- it ( 'renders the Input Index header' , ( ) => {
18
- renderComponent ( ) ;
19
- expect ( screen . getByText ( 'Input Index' ) ) . to . be . visible ;
20
- } ) ;
21
24
22
- it ( 'renders the Code Equivalent toggle' , ( ) => {
23
- renderComponent ( ) ;
24
- expect ( screen . getByLabelText ( 'Toggle Code Equivalent' ) ) . to . be . visible ;
25
- } ) ;
25
+ describe ( 'when the fields are not filled in' , ( ) => {
26
+ it ( 'renders the Input Index header' , ( ) => {
27
+ renderComponent ( { } ) ;
28
+ expect ( screen . getByText ( 'Input Index' ) ) . to . be . visible ;
29
+ } ) ;
26
30
27
- it ( 'renders the Show covered queries button ' , ( ) => {
28
- renderComponent ( ) ;
29
- expect ( screen . getByText ( 'Show covered queries ') ) . to . be . visible ;
30
- } ) ;
31
+ it ( 'does not render the Covered Queries header ' , ( ) => {
32
+ renderComponent ( { } ) ;
33
+ expect ( screen . queryByText ( 'Covered Queries ') ) . to . be . null ;
34
+ } ) ;
31
35
32
- it ( 'renders the Covered Queries header ' , ( ) => {
33
- renderComponent ( ) ;
34
- expect ( screen . getByText ( 'Covered Queries ') ) . to . be . visible ;
35
- } ) ;
36
+ it ( 'renders the Code Equivalent toggle ' , ( ) => {
37
+ renderComponent ( { } ) ;
38
+ expect ( screen . getByLabelText ( 'Toggle Code Equivalent ') ) . to . be . visible ;
39
+ } ) ;
36
40
37
- it ( 'renders the provided createIndexFieldsComponent' , ( ) => {
38
- const mockComponent = (
39
- < div data-testid = "mock-component" > Mock Component</ div >
40
- ) ;
41
- renderComponent ( mockComponent ) ;
42
- expect ( screen . getByTestId ( 'mock-component' ) ) . to . be . visible ;
41
+ it ( 'renders the Show covered queries button and it\\s disabled' , ( ) => {
42
+ renderComponent ( { } ) ;
43
+ const coveredQueriesButton = screen . getByTestId (
44
+ 'index-flow-section-covered-queries-button'
45
+ ) ;
46
+
47
+ expect ( coveredQueriesButton ) . to . be . visible ;
48
+ } ) ;
49
+
50
+ it ( 'does not render the covered queries examples' , ( ) => {
51
+ renderComponent ( { } ) ;
52
+ expect (
53
+ screen . queryByTestId ( 'index-flow-section-covered-queries-examples' )
54
+ ) . not . to . exist ;
55
+ } ) ;
56
+
57
+ it ( 'does not render the optimal query examples' , ( ) => {
58
+ renderComponent ( { } ) ;
59
+ expect (
60
+ screen . queryByTestId ( 'index-flow-section-optimal-queries-examples' )
61
+ ) . not . to . exist ;
62
+ } ) ;
63
+
64
+ it ( 'renders the provided createIndexFieldsComponent' , ( ) => {
65
+ const mockComponent = (
66
+ < div data-testid = "mock-component" > Mock Component</ div >
67
+ ) ;
68
+ renderComponent ( { createIndexFieldsComponent : mockComponent } ) ;
69
+ expect ( screen . getByTestId ( 'mock-component' ) ) . to . be . visible ;
70
+ } ) ;
43
71
} ) ;
44
72
45
- it ( 'renders the covered queries examples' , ( ) => {
46
- renderComponent ( ) ;
47
- expect ( screen . getByTestId ( 'index-flow-section-covered-queries-examples' ) ) . to
48
- . exist ;
73
+ describe ( 'when 3 index fields are filled in and user clicks on covered queries button' , ( ) => {
74
+ const fields : Field [ ] = [
75
+ { name : 'field1' , type : '1 (asc)' } ,
76
+ { name : 'field2' , type : '-1 (desc)' } ,
77
+ { name : 'field3' , type : '1 (asc)' } ,
78
+ ] ;
79
+
80
+ beforeEach ( ( ) => {
81
+ renderComponent ( { fields } ) ;
82
+ screen . getByTestId ( 'index-flow-section-covered-queries-button' ) . click ( ) ;
83
+ } ) ;
84
+
85
+ it ( 'renders the covered queries examples' , ( ) => {
86
+ const coveredQueriesExamples = screen . getByTestId (
87
+ 'index-flow-section-covered-queries-examples'
88
+ ) ;
89
+ expect ( coveredQueriesExamples ) . to . exist ;
90
+ expect ( coveredQueriesExamples ) . to . contain . text (
91
+ JSON . stringify ( {
92
+ field1 : 1 ,
93
+ field2 : 2 ,
94
+ field3 : 3 ,
95
+ } )
96
+ ) ;
97
+ } ) ;
98
+
99
+ it ( 'renders the optimal query examples' , ( ) => {
100
+ const optimalQueriesExamples = screen . getByTestId (
101
+ 'index-flow-section-optimal-queries-examples'
102
+ ) ;
103
+ expect ( optimalQueriesExamples ) . to . exist ;
104
+ expect ( optimalQueriesExamples ) . to . contain . text (
105
+ `{"field1":1,"field2":{"$gt":2}}.sort(field3: 1})`
106
+ ) ;
107
+ } ) ;
108
+
109
+ it ( 'renders the Learn More link' , ( ) => {
110
+ const link = screen . getByText ( 'Learn More' ) ;
111
+ expect ( link ) . to . be . visible ;
112
+ } ) ;
49
113
} ) ;
50
114
51
- it ( 'renders the optimal query examples' , ( ) => {
52
- renderComponent ( ) ;
53
- expect ( screen . getByTestId ( 'index-flow-section-optimal-query-examples' ) ) . to
54
- . exist ;
115
+ describe ( 'when 2 index fields are filled in and user clicks on covered queries button' , ( ) => {
116
+ const fields : Field [ ] = [
117
+ { name : 'field1' , type : '1 (asc)' } ,
118
+ { name : 'field2' , type : '1 (asc)' } ,
119
+ ] ;
120
+
121
+ beforeEach ( ( ) => {
122
+ renderComponent ( { fields } ) ;
123
+ screen . getByTestId ( 'index-flow-section-covered-queries-button' ) . click ( ) ;
124
+ } ) ;
125
+
126
+ it ( 'renders the covered queries examples' , ( ) => {
127
+ const coveredQueriesExamples = screen . getByTestId (
128
+ 'index-flow-section-covered-queries-examples'
129
+ ) ;
130
+ expect ( coveredQueriesExamples ) . to . exist ;
131
+ } ) ;
132
+
133
+ it ( 'renders the optimal query examples' , ( ) => {
134
+ const optimalQueriesExamples = screen . getByTestId (
135
+ 'index-flow-section-optimal-queries-examples'
136
+ ) ;
137
+ expect ( optimalQueriesExamples ) . to . exist ;
138
+ expect ( optimalQueriesExamples ) . to . contain . text (
139
+ `{"field1":1,"field2":{"$gt":2}}}`
140
+ ) ;
141
+ expect ( optimalQueriesExamples ) . to . contain . text (
142
+ `{"field1":1}.sort({"field2":2})`
143
+ ) ;
144
+ } ) ;
55
145
} ) ;
56
146
57
- it ( 'renders the Learn More link' , ( ) => {
58
- renderComponent ( ) ;
59
- const link = screen . getByText ( 'Learn More' ) ;
60
- expect ( link ) . to . be . visible ;
147
+ describe ( 'when 1 index field is filled in and user clicks on covered queries button' , ( ) => {
148
+ const fields : Field [ ] = [ { name : 'field1' , type : '1 (asc)' } ] ;
149
+
150
+ beforeEach ( ( ) => {
151
+ renderComponent ( { fields } ) ;
152
+ screen . getByTestId ( 'index-flow-section-covered-queries-button' ) . click ( ) ;
153
+ } ) ;
154
+
155
+ it ( 'renders the covered queries examples' , ( ) => {
156
+ expect ( screen . getByTestId ( 'index-flow-section-covered-queries-examples' ) )
157
+ . to . exist ;
158
+ } ) ;
159
+
160
+ it ( 'does not render the optimal query examples' , ( ) => {
161
+ expect (
162
+ screen . queryByTestId ( 'index-flow-section-optimal-queries-examples' )
163
+ ) . not . to . exist ;
164
+ } ) ;
61
165
} ) ;
62
166
} ) ;
0 commit comments