1- import React from 'react' ;
21import { vi } from 'vitest' ;
32import { SECRET_MANAGER_ROUTES_URLS } from '@secret-manager/routes/routes.constants' ;
43import { mockSecret1 } from '@secret-manager/mocks/secrets/secrets.mock' ;
54import { screen , act , waitFor } from '@testing-library/react' ;
6- import userEvent from '@testing-library/user-event' ;
7- import { MOCK_DATA_VALID_JSON } from '@secret-manager/utils/tests/secret.constants' ;
5+ import userEvent , { UserEvent } from '@testing-library/user-event' ;
86import { SECRET_FORM_FIELD_TEST_IDS } from '@secret-manager/components/form/form.constants' ;
97import {
108 assertTextVisibility ,
@@ -13,10 +11,10 @@ import {
1311} from '@ovh-ux/manager-core-test-utils' ;
1412import * as secretVersionsApi from '@secret-manager/data/api/secretVersions' ;
1513import { getSecretMockWithData } from '@secret-manager/mocks/secrets/secretsMock.utils' ;
14+ import { SECRET_VALUE_TOGGLE_TEST_IDS } from '@secret-manager/components/secretValueToggle/secretValueToggle.constants' ;
1615import { okmsRoubaix1Mock } from '@/mocks/kms/okms.mock' ;
1716import { renderTestApp } from '@/utils/tests/renderTestApp' ;
1817import { labels } from '@/utils/tests/init.i18n' ;
19- import { changeOdsInputValueByTestId } from '@/utils/tests/uiTestHelpers' ;
2018import { CREATE_VERSION_DRAWER_TEST_IDS } from './CreateVersionDrawer.constants' ;
2119
2220const mockOkmsId = okmsRoubaix1Mock . id ;
@@ -26,26 +24,27 @@ const mockPageUrl = SECRET_MANAGER_ROUTES_URLS.versionListCreateVersionDrawer(
2624 mockedSecret . path ,
2725) ;
2826
29- // Mocking ODS Input component
27+ // Mocking ODS components
3028vi . mock ( '@ovhcloud/ods-components/react' , async ( ) => {
29+ const {
30+ odsInputMock,
31+ odsTextareaMock,
32+ odsSwitchMock,
33+ odsSwitchItemMock,
34+ } = await import ( '@/utils/tests/odsMocks' ) ;
3135 const original = await vi . importActual ( '@ovhcloud/ods-components/react' ) ;
3236 return {
3337 ...original ,
34- OdsTextarea : vi . fn (
35- // eslint-disable-next-line @typescript-eslint/no-unused-vars
36- ( { className, onOdsChange, onOdsBlur, isResizable, ...rest } ) => (
37- < textarea
38- data-testid = { rest [ 'data-testid' ] }
39- className = { className }
40- onChange = { ( e ) => onOdsChange && onOdsChange ( e . target . value ) }
41- onBlur = { ( ) => onOdsBlur && onOdsBlur ( ) }
42- { ...rest }
43- />
44- ) ,
45- ) ,
38+ OdsInput : vi . fn ( odsInputMock ) ,
39+ OdsTextarea : vi . fn ( odsTextareaMock ) ,
40+ OdsSwitch : vi . fn ( odsSwitchMock ) ,
41+ OdsSwitchItem : vi . fn ( odsSwitchItemMock ) ,
4642 } ;
4743} ) ;
4844
45+ /**
46+ * Renders the create version drawer page
47+ */
4948const renderPage = async ( { url = mockPageUrl } : { url ?: string } = { } ) => {
5049 const user = userEvent . setup ( ) ;
5150 const { container } = await renderTestApp ( url ) ;
@@ -61,11 +60,21 @@ const renderPage = async ({ url = mockPageUrl }: { url?: string } = {}) => {
6160
6261 // wait for the content to be displayed
6362 await assertTextVisibility ( labels . secretManager . add_new_version ) ;
64- await assertTextVisibility ( labels . secretManager . editor ) ;
63+ await assertTextVisibility ( labels . secretManager . key_value ) ;
6564
6665 return { user, container } ;
6766} ;
6867
68+ /**
69+ * Clicks on the JSON toggle
70+ */
71+ const clickJsonToggle = async ( user : UserEvent ) => {
72+ const jsonToggle = screen . getByTestId (
73+ SECRET_VALUE_TOGGLE_TEST_IDS . jsonToggle ,
74+ ) ;
75+ await act ( ( ) => user . click ( jsonToggle ) ) ;
76+ } ;
77+
6978describe ( 'Secret create version drawer page test suite' , ( ) => {
7079 it ( 'should display the create version drawer' , async ( ) => {
7180 const { container } = await renderPage ( ) ;
@@ -79,11 +88,13 @@ describe('Secret create version drawer page test suite', () => {
7988 } ) ;
8089
8190 it ( 'should display the current secret value' , async ( ) => {
82- await renderPage ( ) ;
91+ const { user } = await renderPage ( ) ;
8392
84- const dataInput = screen . getByTestId ( SECRET_FORM_FIELD_TEST_IDS . INPUT_DATA ) ;
93+ // Click on the JSON toggle to switch to the JSON editor
94+ await clickJsonToggle ( user ) ;
8595
8696 // Check if the data input contains the secret value
97+ const dataInput = screen . getByTestId ( SECRET_FORM_FIELD_TEST_IDS . INPUT_DATA ) ;
8798 expect ( dataInput ) . toBeInTheDocument ( ) ;
8899 expect ( dataInput ) . toHaveValue (
89100 JSON . stringify ( getSecretMockWithData ( mockedSecret ) . version . data ) ,
@@ -96,12 +107,21 @@ describe('Secret create version drawer page test suite', () => {
96107 getSecretMockWithData ( mockedSecret ) . version ,
97108 ) ;
98109
110+ // Click on the JSON toggle to switch to the JSON editor
111+ await clickJsonToggle ( user ) ;
112+
113+ const MOCK_NEW_DATA = '{"key1":"value1","key2":"value2"}' ;
99114 // Change the data input value
100- await changeOdsInputValueByTestId (
115+ const input = await screen . findByTestId (
101116 SECRET_FORM_FIELD_TEST_IDS . INPUT_DATA ,
102- MOCK_DATA_VALID_JSON ,
103117 ) ;
104118
119+ // clean first the input
120+ await act ( ( ) => user . clear ( input ) ) ;
121+ // then type the new value
122+ const escaped = MOCK_NEW_DATA . replace ( / { / g, '{{' ) ;
123+ await act ( ( ) => user . type ( input , escaped ) ) ;
124+
105125 // Submit the form
106126 // Button should be enabled after input change
107127 const submitButton = await getOdsButtonByLabel ( {
@@ -117,7 +137,7 @@ describe('Secret create version drawer page test suite', () => {
117137 expect ( secretVersionsApi . createSecretVersion ) . toHaveBeenCalledWith ( {
118138 okmsId : mockOkmsId ,
119139 path : mockedSecret . path ,
120- data : JSON . parse ( MOCK_DATA_VALID_JSON ) ,
140+ data : JSON . parse ( MOCK_NEW_DATA ) ,
121141 cas : mockedSecret . metadata . currentVersion ,
122142 } ) ;
123143 } ) ;
0 commit comments