1
1
import React from 'react' ;
2
- import { mount } from 'enzyme' ;
2
+ import {
3
+ renderWithConnections ,
4
+ screen ,
5
+ userEvent ,
6
+ } from '@mongodb-js/testing-library-compass' ;
3
7
import sinon from 'sinon' ;
4
8
import { expect } from 'chai' ;
5
9
6
10
import Settings from './settings' ;
7
11
import { INITIAL_STATE } from '../../modules/settings' ;
8
12
13
+ function renderSettings (
14
+ props : Partial < React . ComponentProps < typeof Settings > > = { } ,
15
+ preferences : {
16
+ enableAggregationBuilderExtraOptions ?: boolean ;
17
+ } = { }
18
+ ) {
19
+ return renderWithConnections (
20
+ < Settings
21
+ isExpanded = { false }
22
+ isCommenting = { false }
23
+ applySettings = { ( ) => { } }
24
+ toggleSettingsIsExpanded = { ( ) => { } }
25
+ toggleSettingsIsCommentMode = { ( ) => { } }
26
+ setSettingsSampleSize = { ( ) => { } }
27
+ setSettingsLimit = { ( ) => { } }
28
+ limit = { INITIAL_STATE . sampleSize }
29
+ largeLimit = { INITIAL_STATE . limit }
30
+ settings = { INITIAL_STATE }
31
+ { ...props }
32
+ /> ,
33
+ {
34
+ preferences,
35
+ }
36
+ ) ;
37
+ }
38
+
39
+ const largeLimitOptionTestId = 'aggregation-settings-limit-input' ;
40
+
9
41
describe ( 'Settings [Component]' , function ( ) {
10
- let state : any ;
11
- let component : ReturnType < typeof mount > ;
12
42
let applySettingsSpy : sinon . SinonSpy ;
13
43
let toggleSettingsIsExpandedSpy : sinon . SinonSpy ;
14
- let toggleSettingsIsCommentModeSpy : sinon . SinonSpy ;
15
- let setSettingsSampleSizeSpy : sinon . SinonSpy ;
16
- let setSettingsLimitSpy : sinon . SinonSpy ;
17
- let runStageSpy : sinon . SinonSpy ;
18
44
19
45
context ( 'when the component is not atlas deployed' , function ( ) {
20
- beforeEach ( function ( ) {
21
- applySettingsSpy = sinon . spy ( ) ;
22
- toggleSettingsIsExpandedSpy = sinon . spy ( ) ;
23
- toggleSettingsIsCommentModeSpy = sinon . spy ( ) ;
24
- setSettingsSampleSizeSpy = sinon . spy ( ) ;
25
- setSettingsLimitSpy = sinon . spy ( ) ;
26
- runStageSpy = sinon . spy ( ) ;
27
-
28
- state = {
29
- ...INITIAL_STATE ,
30
- applySettings : applySettingsSpy ,
31
- toggleSettingsIsExpanded : toggleSettingsIsExpandedSpy ,
32
- toggleSettingsIsCommentMode : toggleSettingsIsCommentModeSpy ,
33
- setSettingsSampleSize : setSettingsSampleSizeSpy ,
34
- setSettingsLimit : setSettingsLimitSpy ,
35
- isCommenting : true ,
36
- limit : INITIAL_STATE . sampleSize ,
37
- largeLimit : INITIAL_STATE . limit ,
38
- runStage : runStageSpy ,
39
- settings : INITIAL_STATE ,
40
- } ;
41
- } ) ;
42
-
43
46
it ( 'is hidden by default' , function ( ) {
44
- component = mount ( < Settings { ... state } /> ) ;
45
- expect ( Object . keys ( component ) . length ) . to . equal ( 0 ) ;
47
+ renderSettings ( ) ;
48
+ expect ( screen . queryByText ( 'Settings' ) ) . to . not . exist ;
46
49
} ) ;
47
50
48
51
it ( 'is rendered when isExpanded=true' , function ( ) {
49
- const props = { ...state , isExpanded : true } ;
50
- component = mount ( < Settings { ...props } /> ) ;
51
- expect ( component . text ( ) ) . to . contain ( 'Settings' ) ;
52
+ renderSettings ( {
53
+ isExpanded : true ,
54
+ } ) ;
55
+ expect ( screen . getByText ( 'Settings' ) ) . to . be . visible ;
56
+ } ) ;
57
+
58
+ it ( 'shows the large limit option' , function ( ) {
59
+ renderSettings ( {
60
+ isExpanded : true ,
61
+ } ) ;
62
+ expect ( screen . getByTestId ( largeLimitOptionTestId ) ) . to . be . visible ;
52
63
} ) ;
53
64
54
65
describe ( 'When opened' , function ( ) {
66
+ beforeEach ( function ( ) {
67
+ applySettingsSpy = sinon . spy ( ) ;
68
+ toggleSettingsIsExpandedSpy = sinon . spy ( ) ;
69
+ } ) ;
70
+
55
71
it ( 'should close when Cancel is clicked' , function ( ) {
56
- const props = { ... state , isExpanded : true } ;
57
- component = mount ( < Settings { ... props } /> ) ;
58
- component
59
- . find ( '#aggregations-settings-cancel' )
60
- . hostNodes ( )
61
- . simulate ( 'click' ) ;
72
+ renderSettings ( {
73
+ isExpanded : true ,
74
+ applySettings : applySettingsSpy ,
75
+ toggleSettingsIsExpanded : toggleSettingsIsExpandedSpy ,
76
+ } ) ;
77
+ userEvent . click ( screen . getByTestId ( 'aggregation-settings-cancel' ) ) ;
62
78
expect ( toggleSettingsIsExpandedSpy . calledOnce ) . to . equal ( true ) ;
63
79
} ) ;
64
80
65
81
it ( 'should update the settings, re-run the pipeline, and Close' , function ( ) {
66
- const props = { ...state , isExpanded : true } ;
67
-
68
- component = mount ( < Settings { ...props } /> ) ;
69
- component
70
- . find ( '#aggregation-settings-apply' )
71
- . hostNodes ( )
72
- . simulate ( 'click' ) ;
82
+ renderSettings ( {
83
+ isExpanded : true ,
84
+ applySettings : applySettingsSpy ,
85
+ toggleSettingsIsExpanded : toggleSettingsIsExpandedSpy ,
86
+ } ) ;
87
+ userEvent . click ( screen . getByTestId ( 'aggregation-settings-apply' ) ) ;
73
88
74
89
expect ( applySettingsSpy . calledOnce ) . to . equal ( true ) ;
75
90
expect ( toggleSettingsIsExpandedSpy . calledOnce ) . to . equal ( true ) ;
@@ -79,32 +94,18 @@ describe('Settings [Component]', function () {
79
94
80
95
context ( 'when the component is atlas deployed' , function ( ) {
81
96
beforeEach ( function ( ) {
82
- applySettingsSpy = sinon . spy ( ) ;
83
- toggleSettingsIsExpandedSpy = sinon . spy ( ) ;
84
- toggleSettingsIsCommentModeSpy = sinon . spy ( ) ;
85
- setSettingsSampleSizeSpy = sinon . spy ( ) ;
86
- setSettingsLimitSpy = sinon . spy ( ) ;
87
- runStageSpy = sinon . spy ( ) ;
88
-
89
- state = {
90
- ...INITIAL_STATE ,
91
- applySettings : applySettingsSpy ,
92
- toggleSettingsIsExpanded : toggleSettingsIsExpandedSpy ,
93
- toggleSettingsIsCommentMode : toggleSettingsIsCommentModeSpy ,
94
- setSettingsSampleSize : setSettingsSampleSizeSpy ,
95
- setSettingsLimit : setSettingsLimitSpy ,
96
- isCommenting : true ,
97
- limit : INITIAL_STATE . sampleSize ,
98
- largeLimit : INITIAL_STATE . limit ,
99
- runStage : runStageSpy ,
100
- settings : INITIAL_STATE ,
101
- } ;
97
+ renderSettings (
98
+ {
99
+ isExpanded : true ,
100
+ } ,
101
+ {
102
+ enableAggregationBuilderExtraOptions : false ,
103
+ }
104
+ ) ;
102
105
} ) ;
103
106
104
107
it ( 'hides the large limit option' , function ( ) {
105
- const props = { ...state } ;
106
- component = mount ( < Settings { ...props } /> ) ;
107
- expect ( component . find ( 'label[innerText="Limit"]' ) ) . to . not . exist ;
108
+ expect ( screen . queryByTestId ( largeLimitOptionTestId ) ) . to . not . exist ;
108
109
} ) ;
109
110
} ) ;
110
111
} ) ;
0 commit comments