@@ -22,7 +22,12 @@ import { DestinationConfig } from '../../types';
2222import { useDataCatalog } from '../../../../utils/hooks/useDataCatalog/useDataCatalog' ;
2323
2424const mockUseDataCatalog = {
25- loading : false ,
25+ loading : {
26+ connector : false ,
27+ compute : false ,
28+ database : false ,
29+ table : false
30+ } ,
2631 databases : [ 'database1' , 'database2' ] ,
2732 database : 'database1' ,
2833 connectors : [
@@ -35,6 +40,7 @@ const mockUseDataCatalog = {
3540 { id : 'compute2' , name : 'Compute 2' }
3641 ] ,
3742 compute : { id : 'compute1' , name : 'Compute 1' } ,
43+ tables : [ ] ,
3844 setCompute : jest . fn ( ) ,
3945 setConnector : jest . fn ( ) ,
4046 setDatabase : jest . fn ( )
@@ -79,20 +85,26 @@ describe('DestinationSettings Component', () => {
7985 expect ( screen . getByDisplayValue ( 'test_table' ) ) . toBeInTheDocument ( ) ;
8086 } ) ;
8187
82- it ( 'should hide compute field when only one compute is available' , ( ) => {
88+ it ( 'should not have compute field when only one compute is available' , async ( ) => {
8389 const mockUseDataCatalogSingleCompute = {
8490 ...mockUseDataCatalog ,
85- computes : [ { id : 'compute1' , name : 'Compute 1' } ]
91+ computes : [ { id : 'compute1' , name : 'Compute 1' } ] ,
92+ compute : { id : 'compute1' , name : 'Compute 1' }
8693 } ;
8794
88- ( useDataCatalog as jest . Mock ) . mockReturnValueOnce ( mockUseDataCatalogSingleCompute ) ;
95+ ( useDataCatalog as jest . Mock ) . mockReturnValue ( mockUseDataCatalogSingleCompute ) ;
8996
9097 render ( < DestinationSettings { ...defaultProps } /> ) ;
9198
92- expect ( screen . queryByLabelText ( 'Compute' ) ) . not . toBeInTheDocument ( ) ;
99+ await waitFor ( ( ) => {
100+ expect ( screen . getByLabelText ( 'Engine' ) ) . toBeInTheDocument ( ) ;
101+ expect ( screen . getByLabelText ( 'Database' ) ) . toBeInTheDocument ( ) ;
102+ expect ( screen . getByLabelText ( 'Table Name' ) ) . toBeInTheDocument ( ) ;
103+ expect ( screen . queryByLabelText ( 'Compute' ) ) . not . toBeInTheDocument ( ) ;
104+ } ) ;
93105 } ) ;
94106
95- it ( 'should show compute field when multiple computes are available' , ( ) => {
107+ it ( 'should show compute field when multiple computes are available' , async ( ) => {
96108 render ( < DestinationSettings { ...defaultProps } /> ) ;
97109
98110 expect ( screen . getByLabelText ( 'Compute' ) ) . toBeInTheDocument ( ) ;
@@ -149,54 +161,20 @@ describe('DestinationSettings Component', () => {
149161 id : 'compute2' ,
150162 name : 'Compute 2'
151163 } ) ;
152- expect ( defaultProps . onChange ) . toHaveBeenCalledWith ( 'compute ' , 'compute2' ) ;
164+ expect ( defaultProps . onChange ) . toHaveBeenCalledWith ( 'computeId ' , 'compute2' ) ;
153165 } ) ;
154166
155- it ( 'should update table name input and not call onChange immediately' , ( ) => {
167+ it ( 'should update table name input and call onChange when input changes' , async ( ) => {
156168 render ( < DestinationSettings { ...defaultProps } /> ) ;
157169
158170 const tableNameInput = screen . getByLabelText ( 'Table Name' ) ;
159171 fireEvent . change ( tableNameInput , { target : { value : 'new_table_name' } } ) ;
160172
161173 expect ( screen . getByDisplayValue ( 'new_table_name' ) ) . toBeInTheDocument ( ) ;
162- expect ( defaultProps . onChange ) . not . toHaveBeenCalled ( ) ;
174+ expect ( defaultProps . onChange ) . toHaveBeenCalledWith ( 'tableName' , 'new_table_name' ) ;
163175 } ) ;
164176
165- it ( 'should show loader in select dropdown when loading state is true' , ( ) => {
166- const mockUseDataCatalogLoading = {
167- ...mockUseDataCatalog ,
168- loading : true
169- } ;
170-
171- ( useDataCatalog as jest . Mock ) . mockReturnValueOnce ( mockUseDataCatalogLoading ) ;
172-
173- render ( < DestinationSettings { ...defaultProps } /> ) ;
174-
175- const selectDropdowns = document . querySelectorAll ( '.ant-select' ) ;
176- selectDropdowns . forEach ( select => {
177- expect ( select ) . toBeInTheDocument ( ) ;
178- expect ( select ) . toHaveClass ( 'ant-select-loading' ) ;
179- } ) ;
180- } ) ;
181-
182- it ( 'should not show loader in select dropdown when loading state is false' , ( ) => {
183- const mockUseDataCatalogLoading = {
184- ...mockUseDataCatalog ,
185- loading : false
186- } ;
187-
188- ( useDataCatalog as jest . Mock ) . mockReturnValueOnce ( mockUseDataCatalogLoading ) ;
189-
190- render ( < DestinationSettings { ...defaultProps } /> ) ;
191-
192- const selectDropdowns = document . querySelectorAll ( '.ant-select' ) ;
193- selectDropdowns . forEach ( select => {
194- expect ( select ) . toBeInTheDocument ( ) ;
195- expect ( select ) . not . toHaveClass ( 'ant-select-loading' ) ;
196- } ) ;
197- } ) ;
198-
199- it ( 'should set default values from props on component mount' , ( ) => {
177+ it ( 'should set default values from props on component mount' , async ( ) => {
200178 const mockSetConnector = jest . fn ( ) ;
201179 const mockSetDatabase = jest . fn ( ) ;
202180 const mockSetCompute = jest . fn ( ) ;
@@ -223,7 +201,7 @@ describe('DestinationSettings Component', () => {
223201 } ) ;
224202 } ) ;
225203
226- it ( 'should not call setters when no matching items found in defaultValues' , ( ) => {
204+ it ( 'should not call setters when no matching items found in defaultValues' , async ( ) => {
227205 const mockSetConnector = jest . fn ( ) ;
228206 const mockSetDatabase = jest . fn ( ) ;
229207 const mockSetCompute = jest . fn ( ) ;
@@ -246,4 +224,49 @@ describe('DestinationSettings Component', () => {
246224 expect ( mockSetDatabase ) . not . toHaveBeenCalled ( ) ;
247225 expect ( mockSetCompute ) . not . toHaveBeenCalled ( ) ;
248226 } ) ;
227+
228+ it ( 'should show alert when table name already exists' , async ( ) => {
229+ const mockUseDataCatalogWithExistingTables = {
230+ ...mockUseDataCatalog ,
231+ tables : [ { name : 'existing_table' } , { name : 'another_table' } ]
232+ } ;
233+
234+ ( useDataCatalog as jest . Mock ) . mockReturnValue ( mockUseDataCatalogWithExistingTables ) ;
235+
236+ render ( < DestinationSettings { ...defaultProps } /> ) ;
237+
238+ const tableNameInput = screen . getByLabelText ( 'Table Name' ) ;
239+
240+ fireEvent . change ( tableNameInput , { target : { value : 'existing_table' } } ) ;
241+
242+ await waitFor ( ( ) => {
243+ expect ( screen . getByRole ( 'alert' ) ) . toBeInTheDocument ( ) ;
244+ expect ( screen . getByText ( 'Table name already exists in the database' ) ) . toBeInTheDocument ( ) ;
245+ } ) ;
246+ } ) ;
247+
248+ it ( 'should hide alert when table name is unique' , async ( ) => {
249+ const mockUseDataCatalogWithExistingTables = {
250+ ...mockUseDataCatalog ,
251+ tables : [ { name : 'existing_table' } , { name : 'another_table' } ]
252+ } ;
253+
254+ ( useDataCatalog as jest . Mock ) . mockReturnValue ( mockUseDataCatalogWithExistingTables ) ;
255+
256+ render ( < DestinationSettings { ...defaultProps } /> ) ;
257+
258+ const tableNameInput = screen . getByLabelText ( 'Table Name' ) ;
259+
260+ fireEvent . change ( tableNameInput , { target : { value : 'existing_table' } } ) ;
261+
262+ await waitFor ( ( ) => {
263+ expect ( screen . getByRole ( 'alert' ) ) . toBeInTheDocument ( ) ;
264+ } ) ;
265+
266+ fireEvent . change ( tableNameInput , { target : { value : 'unique_table_name' } } ) ;
267+
268+ await waitFor ( ( ) => {
269+ expect ( screen . queryByRole ( 'alert' ) ) . not . toBeInTheDocument ( ) ;
270+ } ) ;
271+ } ) ;
249272} ) ;
0 commit comments