-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vanessa
committed
Nov 7, 2024
1 parent
791b6d9
commit 667b76f
Showing
11 changed files
with
824 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
client/test/preview/integration/route/digitaltwins/create/ChangeFileNameDialog.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as React from 'react'; | ||
import ChangeFileNameDialog from 'preview/route/digitaltwins/create/ChangeFileNameDialog'; | ||
import { Provider } from 'react-redux'; | ||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
import { | ||
combineReducers, | ||
configureStore, | ||
getDefaultMiddleware, | ||
} from '@reduxjs/toolkit'; | ||
import fileSlice from 'preview/store/file.slice'; | ||
import { act } from 'react'; | ||
|
||
const store = configureStore({ | ||
reducer: combineReducers({ | ||
files: fileSlice, | ||
}), | ||
middleware: getDefaultMiddleware({ | ||
serializableCheck: false, | ||
}), | ||
}); | ||
|
||
describe('ChangeFileNameDialog', () => { | ||
const setShowDialog = jest.fn(); | ||
const setFileName = jest.fn(); | ||
const setFileType = jest.fn(); | ||
const fileName = 'testName'; | ||
|
||
beforeEach(() => { | ||
act(() => { | ||
render( | ||
<Provider store={store}> | ||
<ChangeFileNameDialog | ||
open={true} | ||
setOpenChangeFileNameDialog={setShowDialog} | ||
fileName={fileName} | ||
setFileName={setFileName} | ||
setFileType={setFileType} | ||
/> | ||
</Provider>, | ||
); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('handles click on change button', () => { | ||
const changeButton = screen.getByRole('button', { name: /Change/i }); | ||
act(() => { | ||
changeButton.click(); | ||
}); | ||
|
||
expect(setFileName).toHaveBeenCalled(); | ||
expect(setFileType).toHaveBeenCalled(); | ||
}); | ||
|
||
it('handles click on cancel button', () => { | ||
const cancelButton = screen.getByRole('button', { name: /Cancel/i }); | ||
act(() => { | ||
cancelButton.click(); | ||
}); | ||
|
||
expect(setShowDialog).toHaveBeenCalled(); | ||
}); | ||
|
||
it('handles change in text field', () => { | ||
const textField = screen.getByRole('textbox'); | ||
|
||
act(() => { | ||
fireEvent.change(textField, { | ||
target: { value: 'modifiedDTName' }, | ||
}); | ||
}); | ||
|
||
waitFor(() => { | ||
expect(textField).toHaveValue('modifiedDTName'); | ||
}); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
client/test/preview/integration/route/digitaltwins/create/ConfirmDeleteDialog.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as React from 'react'; | ||
import ConfirmDeleteDialog from 'preview/route/digitaltwins/create/ConfirmDeleteDialog'; | ||
import { act } from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { | ||
combineReducers, | ||
configureStore, | ||
getDefaultMiddleware, | ||
} from '@reduxjs/toolkit'; | ||
import { Provider } from 'react-redux'; | ||
import fileSlice, { addOrUpdateFile } from 'preview/store/file.slice'; | ||
|
||
const store = configureStore({ | ||
reducer: combineReducers({ | ||
files: fileSlice, | ||
}), | ||
middleware: getDefaultMiddleware({ | ||
serializableCheck: false, | ||
}), | ||
}); | ||
|
||
describe('ConfirmDeleteDialog', () => { | ||
const setOpenConfirmDeleteDialog = jest.fn(); | ||
const setFileName = jest.fn(); | ||
const setFileContent = jest.fn(); | ||
const setFileType = jest.fn(); | ||
const setNewDigitalTwinName = jest.fn(); | ||
|
||
beforeEach(() => { | ||
act(() => { | ||
render( | ||
<Provider store={store}> | ||
<ConfirmDeleteDialog | ||
open={true} | ||
setOpenConfirmDeleteDialog={setOpenConfirmDeleteDialog} | ||
setFileName={setFileName} | ||
setFileContent={setFileContent} | ||
setFileType={setFileType} | ||
setNewDigitalTwinName={setNewDigitalTwinName} | ||
/> | ||
</Provider>, | ||
); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('handles fileExists functionality', () => { | ||
const file = { | ||
name: 'description.md', | ||
content: 'test', | ||
isNew: true, | ||
isModified: false, | ||
}; | ||
|
||
act(() => { | ||
store.dispatch(addOrUpdateFile(file)); | ||
}); | ||
|
||
const yesButton = screen.getByRole('button', { name: /Yes/i }); | ||
act(() => { | ||
yesButton.click(); | ||
}); | ||
|
||
expect(setOpenConfirmDeleteDialog).toHaveBeenCalled(); | ||
}); | ||
|
||
it('handles cancel', () => { | ||
const cancelButton = screen.getByRole('button', { name: /Cancel/i }); | ||
|
||
act(() => { | ||
cancelButton.click(); | ||
}); | ||
|
||
expect(setOpenConfirmDeleteDialog).toHaveBeenCalled(); | ||
}); | ||
}); |
118 changes: 118 additions & 0 deletions
118
client/test/preview/integration/route/digitaltwins/create/CreateDTDialog.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import * as React from 'react'; | ||
import CreateDTDialog from 'preview/route/digitaltwins/create/CreateDTDialog'; | ||
import { act, render, screen } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import { | ||
combineReducers, | ||
configureStore, | ||
getDefaultMiddleware, | ||
} from '@reduxjs/toolkit'; | ||
import fileSlice from 'preview/store/file.slice'; | ||
import { validateFiles } from 'preview/util/fileUtils'; | ||
import { initDigitalTwin } from 'preview/util/init'; | ||
|
||
jest.mock('preview/util/fileUtils', () => ({ | ||
validateFiles: jest.fn(), | ||
addDefaultFiles: jest.fn(), | ||
})); | ||
jest.mock('preview/util/init', () => ({ | ||
initDigitalTwin: jest.fn(), | ||
})); | ||
|
||
const store = configureStore({ | ||
reducer: combineReducers({ | ||
files: fileSlice, | ||
}), | ||
middleware: getDefaultMiddleware({ | ||
serializableCheck: false, | ||
}), | ||
}); | ||
|
||
describe('CreateDTDialog - handleConfirm function', () => { | ||
const newDigitalTwinName = 'newDTName'; | ||
const errorMessage = ''; | ||
const setOpenCreateDTDialog = jest.fn(); | ||
const setNewDigitalTwinName = jest.fn(); | ||
const setErrorMessage = jest.fn(); | ||
const setFileName = jest.fn(); | ||
const setFileContent = jest.fn(); | ||
const setFileType = jest.fn(); | ||
|
||
beforeEach(() => { | ||
act(() => { | ||
render( | ||
<Provider store={store}> | ||
<CreateDTDialog | ||
open={true} | ||
setOpenCreateDTDialog={setOpenCreateDTDialog} | ||
newDigitalTwinName={newDigitalTwinName} | ||
setNewDigitalTwinName={setNewDigitalTwinName} | ||
errorMessage={errorMessage} | ||
setErrorMessage={setErrorMessage} | ||
setFileName={setFileName} | ||
setFileContent={setFileContent} | ||
setFileType={setFileType} | ||
/> | ||
</Provider>, | ||
); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('does not proceed if file validation fails', async () => { | ||
(validateFiles as jest.Mock).mockReturnValue(true); | ||
|
||
const confirmButton = screen.getByRole('button', { name: /Confirm/i }); | ||
await act(async () => { | ||
confirmButton.click(); | ||
}); | ||
|
||
expect(validateFiles).toHaveBeenCalled(); | ||
expect(initDigitalTwin).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('handles error if digitalTwin.create returns an error', async () => { | ||
(validateFiles as jest.Mock).mockReturnValue(false); | ||
const mockDigitalTwin = { | ||
create: jest.fn().mockResolvedValue('Error: creation failed'), | ||
}; | ||
(initDigitalTwin as jest.Mock).mockResolvedValue(mockDigitalTwin); | ||
|
||
const confirmButton = screen.getByRole('button', { name: /Confirm/i }); | ||
await act(async () => { | ||
confirmButton.click(); | ||
}); | ||
|
||
expect(initDigitalTwin).toHaveBeenCalledWith(newDigitalTwinName); | ||
expect(mockDigitalTwin.create).toHaveBeenCalled(); | ||
}); | ||
|
||
it('handles success if digitalTwin.create is successful', async () => { | ||
(validateFiles as jest.Mock).mockReturnValue(false); | ||
const mockDigitalTwin = { create: jest.fn().mockResolvedValue('Success') }; | ||
(initDigitalTwin as jest.Mock).mockResolvedValue(mockDigitalTwin); | ||
|
||
const confirmButton = screen.getByRole('button', { name: /Confirm/i }); | ||
await act(async () => { | ||
confirmButton.click(); | ||
}); | ||
|
||
expect(initDigitalTwin).toHaveBeenCalledWith(newDigitalTwinName); | ||
expect(mockDigitalTwin.create).toHaveBeenCalled(); | ||
}); | ||
|
||
it('resets dialog after clicking cancel', async () => { | ||
const cancelButton = screen.getByRole('button', { name: /Cancel/i }); | ||
await act(async () => { | ||
cancelButton.click(); | ||
}); | ||
|
||
expect(setOpenCreateDTDialog).toHaveBeenCalled(); | ||
expect(setFileName).toHaveBeenCalledWith(''); | ||
expect(setFileContent).toHaveBeenCalledWith(''); | ||
expect(setFileType).toHaveBeenCalledWith(''); | ||
}); | ||
}); |
99 changes: 99 additions & 0 deletions
99
client/test/preview/integration/route/digitaltwins/create/CreatePage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as React from 'react'; | ||
import CreatePage from 'preview/route/digitaltwins/create/CreatePage'; | ||
import { | ||
act, | ||
fireEvent, | ||
render, | ||
screen, | ||
waitFor, | ||
} from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import { | ||
combineReducers, | ||
configureStore, | ||
getDefaultMiddleware, | ||
} from '@reduxjs/toolkit'; | ||
import digitalTwinReducer from 'preview/store/digitalTwin.slice'; | ||
import snackbarSlice from 'preview/store/snackbar.slice'; | ||
import fileSlice from 'preview/store/file.slice'; | ||
|
||
const store = configureStore({ | ||
reducer: combineReducers({ | ||
digitalTwin: digitalTwinReducer, | ||
snackbar: snackbarSlice, | ||
files: fileSlice, | ||
}), | ||
middleware: getDefaultMiddleware({ | ||
serializableCheck: false, | ||
}), | ||
}); | ||
|
||
describe('CreatePage', () => { | ||
const setNewDigitalTwinName = jest.fn(); | ||
|
||
beforeEach(() => { | ||
act(() => { | ||
render( | ||
<Provider store={store}> | ||
<CreatePage | ||
newDigitalTwinName="newDTName" | ||
setNewDigitalTwinName={setNewDigitalTwinName} | ||
/> | ||
</Provider>, | ||
); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
/* | ||
it('renders CreatePage', () => { | ||
expect(screen.getByText('Please select a file to edit')).toBeInTheDocument(); | ||
}); | ||
*/ | ||
|
||
it('handles cancel when clicking on cancel button and confirm', () => { | ||
act(() => { | ||
screen.getByText('Cancel').click(); | ||
}); | ||
|
||
expect( | ||
screen.getByText( | ||
'Are you sure you want to delete the inserted files and their content?', | ||
), | ||
).toBeInTheDocument(); | ||
|
||
act(() => { | ||
screen.getByText('Yes').click(); | ||
}); | ||
|
||
expect(setNewDigitalTwinName).toHaveBeenCalled(); | ||
}); | ||
|
||
it('opens confirm dialog when clicking on save button', () => { | ||
act(() => { | ||
screen.getByText('Save').click(); | ||
}); | ||
|
||
expect( | ||
screen.getByText(/Are you sure you want to create/i), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('changes the digital twin name', () => { | ||
const textField = screen.getByRole('textbox'); | ||
|
||
act(() => { | ||
fireEvent.change(textField, { | ||
target: { value: 'modifiedDTName' }, | ||
}); | ||
}); | ||
|
||
waitFor(() => { | ||
expect(textField).toHaveValue('modifiedDTName'); | ||
}); | ||
expect(setNewDigitalTwinName).toHaveBeenCalled(); | ||
}); | ||
}); |
Oops, something went wrong.