11import React from 'react' ;
2- import { mount } from 'enzyme' ;
2+ import {
3+ renderWithConnections ,
4+ screen ,
5+ userEvent ,
6+ } from '@mongodb-js/testing-library-compass' ;
37import sinon from 'sinon' ;
48import { expect } from 'chai' ;
59
610import Settings from './settings' ;
711import { INITIAL_STATE } from '../../modules/settings' ;
812
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+
941describe ( 'Settings [Component]' , function ( ) {
10- let state : any ;
11- let component : ReturnType < typeof mount > ;
1242 let applySettingsSpy : sinon . SinonSpy ;
1343 let toggleSettingsIsExpandedSpy : sinon . SinonSpy ;
14- let toggleSettingsIsCommentModeSpy : sinon . SinonSpy ;
15- let setSettingsSampleSizeSpy : sinon . SinonSpy ;
16- let setSettingsLimitSpy : sinon . SinonSpy ;
17- let runStageSpy : sinon . SinonSpy ;
1844
1945 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-
4346 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 ;
4649 } ) ;
4750
4851 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 ;
5263 } ) ;
5364
5465 describe ( 'When opened' , function ( ) {
66+ beforeEach ( function ( ) {
67+ applySettingsSpy = sinon . spy ( ) ;
68+ toggleSettingsIsExpandedSpy = sinon . spy ( ) ;
69+ } ) ;
70+
5571 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' ) ) ;
6278 expect ( toggleSettingsIsExpandedSpy . calledOnce ) . to . equal ( true ) ;
6379 } ) ;
6480
6581 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' ) ) ;
7388
7489 expect ( applySettingsSpy . calledOnce ) . to . equal ( true ) ;
7590 expect ( toggleSettingsIsExpandedSpy . calledOnce ) . to . equal ( true ) ;
@@ -79,32 +94,18 @@ describe('Settings [Component]', function () {
7994
8095 context ( 'when the component is atlas deployed' , function ( ) {
8196 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+ ) ;
102105 } ) ;
103106
104107 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 ;
108109 } ) ;
109110 } ) ;
110111} ) ;
0 commit comments