-
Notifications
You must be signed in to change notification settings - Fork 58
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
Dec 6, 2024
1 parent
fb0aa62
commit b880164
Showing
12 changed files
with
697 additions
and
277 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
132 changes: 132 additions & 0 deletions
132
client/test/preview/integration/components/asset/AssetLibrary.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,132 @@ | ||
import * as React from 'react'; | ||
import { render, screen, waitFor, fireEvent } from '@testing-library/react'; | ||
import { Provider } from 'react-redux'; | ||
import { combineReducers, configureStore } from '@reduxjs/toolkit'; | ||
import AssetLibrary from 'preview/components/asset/AssetLibrary'; | ||
import assetsReducer, { setAssets } from 'preview/store/assets.slice'; | ||
import digitalTwinReducer from 'preview/store/digitalTwin.slice'; | ||
import snackbarSlice from 'preview/store/snackbar.slice'; | ||
import fileSlice from 'preview/store/file.slice'; | ||
import libraryConfigFilesSlice from 'preview/store/libraryConfigFiles.slice'; | ||
import LibraryAsset from 'preview/util/libraryAsset'; | ||
import { enableFetchMocks } from 'jest-fetch-mock'; | ||
import { mockGitlabInstance } from 'test/preview/__mocks__/global_mocks'; | ||
|
||
enableFetchMocks(); | ||
|
||
jest.mock('preview/util/init', () => ({ | ||
fetchLibraryAssets: jest.fn(), | ||
})); | ||
|
||
const mockLibraryAsset1 = new LibraryAsset( | ||
'Asset 1', | ||
'path/to/assets', | ||
false, | ||
'Digital Twins', | ||
mockGitlabInstance, | ||
); | ||
mockLibraryAsset1.fullDescription = 'Library Asset 1 Description'; | ||
|
||
const mockLibraryAsset2 = new LibraryAsset( | ||
'Asset 2', | ||
'path/to/assets', | ||
false, | ||
'Digital Twins', | ||
mockGitlabInstance, | ||
); | ||
mockLibraryAsset2.fullDescription = 'Library Asset 2 Description'; | ||
|
||
const store = configureStore({ | ||
reducer: combineReducers({ | ||
assets: assetsReducer, | ||
digitalTwin: digitalTwinReducer, | ||
snackbar: snackbarSlice, | ||
files: fileSlice, | ||
libraryConfigFiles: libraryConfigFilesSlice, | ||
}), | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware({ | ||
serializableCheck: false, | ||
}), | ||
}); | ||
|
||
describe('AssetLibrary Integration Tests', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
store.dispatch(setAssets([mockLibraryAsset1, mockLibraryAsset2])); | ||
}); | ||
|
||
it.skip('renders loading spinner initially', async () => { | ||
render( | ||
<Provider store={store}> | ||
<AssetLibrary pathToAssets="path/to/assets" privateRepo={false} /> | ||
</Provider>, | ||
); | ||
|
||
expect(screen.getByRole('progressbar')).toBeInTheDocument(); | ||
}); | ||
|
||
it.skip('renders AssetLibrary with assets after loading', async () => { | ||
render( | ||
<Provider store={store}> | ||
<AssetLibrary pathToAssets="path/to/assets" privateRepo={false} /> | ||
</Provider>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText('Library Asset 1 Description'), | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText('Library Asset 2 Description'), | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it.skip('filters assets based on filter input', async () => { | ||
render( | ||
<Provider store={store}> | ||
<AssetLibrary pathToAssets="path/to/assets" privateRepo={false} /> | ||
</Provider>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText('Library Asset 1 Description'), | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText('Library Asset 2 Description'), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
fireEvent.change(screen.getByRole('textbox'), { | ||
target: { value: 'Asset 1' }, | ||
}); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText('Library Asset 1 Description'), | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.queryByText('Library Asset 2 Description'), | ||
).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it.skip('displays error message if there is an error', async () => { | ||
const errorMessage = 'Error fetching assets'; | ||
jest.spyOn(store, 'dispatch').mockImplementation(() => { | ||
throw new Error(errorMessage); | ||
}); | ||
|
||
render( | ||
<Provider store={store}> | ||
<AssetLibrary pathToAssets="path/to/assets" privateRepo={false} /> | ||
</Provider>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(errorMessage)).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
204 changes: 182 additions & 22 deletions
204
client/test/preview/integration/route/digitaltwins/editor/SidebarFunctions.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 |
---|---|---|
@@ -1,29 +1,189 @@ | ||
/* import { handleFileClick } from "preview/route/digitaltwins/editor/sidebarFunctions"; | ||
import { FileState } from "preview/store/file.slice"; | ||
describe('SidebarFunctions', () => { | ||
const fileName = 'example.md'; | ||
const asset = null; | ||
const mockSetFileName = jest.fn(); | ||
const mockSetFileContent = jest.fn(); | ||
const mockSetFileType = jest.fn(); | ||
const mockSetFilePrivacy = jest.fn(); | ||
const mockSetIsLibraryFile = jest.fn(); | ||
const mockSetLibraryAssetPath = jest.fn(); | ||
const mockDispatch = jest.fn(); | ||
afterEach(() => { | ||
import { useDispatch } from 'react-redux'; | ||
import { | ||
handleFileClick, | ||
handleCreateFileClick, | ||
handleReconfigureFileClick, | ||
handleAddFileClick, | ||
handleCloseFileNameDialog, | ||
handleFileSubmit, | ||
} from 'preview/route/digitaltwins/editor/sidebarFunctions'; | ||
import { FileState } from 'preview/store/file.slice'; | ||
import { updateFileState } from 'preview/util/fileUtils'; | ||
|
||
jest.mock('react-redux', () => ({ | ||
useDispatch: jest.fn(), | ||
})); | ||
|
||
jest.mock('preview/route/digitaltwins/editor/sidebarFetchers', () => ({ | ||
fetchAndSetFileContent: jest.fn(), | ||
fetchAndSetFileLibraryContent: jest.fn(), | ||
})); | ||
|
||
jest.mock('preview/util/fileUtils', () => ({ | ||
updateFileState: jest.fn(), | ||
getFileTypeFromExtension: jest.fn(), | ||
})); | ||
|
||
describe('sidebarFunctions integration tests', () => { | ||
const dispatch = jest.fn(); | ||
(useDispatch as unknown as jest.Mock).mockReturnValue(dispatch); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('calls handleFileClick if tab is "create"', () => { | ||
const mockFiles: FileState[] = [ | ||
{ name: fileName, content: 'content', isNew: true, isModified: false }, | ||
test.skip('handleFileClick with tab "create"', () => { | ||
const setFileName = jest.fn(); | ||
const setFileContent = jest.fn(); | ||
const setFileType = jest.fn(); | ||
const setFilePrivacy = jest.fn(); | ||
const setIsLibraryFile = jest.fn(); | ||
const setLibraryAssetPath = jest.fn(); | ||
const files: FileState[] = [ | ||
{ name: 'testFile', isNew: true, content: 'content', isModified: false }, | ||
]; | ||
handleFileClick(fileName, asset, mockSetFileName, mockSetFileContent, mockSetFileType, mockSetFilePrivacy, mockFiles, 'create', mockSetIsLibraryFile, mockSetLibraryAssetPath, mockDispatch); | ||
|
||
handleFileClick( | ||
'testFile', | ||
null, | ||
setFileName, | ||
setFileContent, | ||
setFileType, | ||
setFilePrivacy, | ||
files, | ||
'create', | ||
setIsLibraryFile, | ||
setLibraryAssetPath, | ||
); | ||
|
||
expect(setFileName).toHaveBeenCalled(); | ||
expect(setFileContent).toHaveBeenCalled(); | ||
expect(setFileType).toHaveBeenCalled(); | ||
expect(setFilePrivacy).toHaveBeenCalled(); | ||
}); | ||
|
||
test('handleCreateFileClick with DigitalTwin asset', () => { | ||
const setFileName = jest.fn(); | ||
const setFileContent = jest.fn(); | ||
const setFileType = jest.fn(); | ||
const setFilePrivacy = jest.fn(); | ||
const setIsLibraryFile = jest.fn(); | ||
const setLibraryAssetPath = jest.fn(); | ||
const files: FileState[] = [ | ||
{ name: 'testFile', isNew: true, content: 'content', isModified: false }, | ||
]; | ||
|
||
handleCreateFileClick( | ||
'testFile', | ||
null, | ||
files, | ||
setFileName, | ||
setFileContent, | ||
setFileType, | ||
setFilePrivacy, | ||
setIsLibraryFile, | ||
setLibraryAssetPath, | ||
dispatch, | ||
); | ||
|
||
expect(updateFileState).toHaveBeenCalled(); | ||
expect(setIsLibraryFile).toHaveBeenCalledWith(false); | ||
expect(setLibraryAssetPath).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
test('handleReconfigureFileClick with modified file', async () => { | ||
const setFileName = jest.fn(); | ||
const setFileContent = jest.fn(); | ||
const setFileType = jest.fn(); | ||
const setFilePrivacy = jest.fn(); | ||
const setIsLibraryFile = jest.fn(); | ||
const setLibraryAssetPath = jest.fn(); | ||
const files = [ | ||
{ name: 'testFile', isModified: true, isNew: false, content: 'content' }, | ||
]; | ||
|
||
await handleReconfigureFileClick( | ||
'testFile', | ||
null, | ||
files, | ||
setFileName, | ||
setFileContent, | ||
setFileType, | ||
setFilePrivacy, | ||
setIsLibraryFile, | ||
setLibraryAssetPath, | ||
dispatch, | ||
); | ||
|
||
expect(updateFileState).toHaveBeenCalled(); | ||
expect(setIsLibraryFile).toHaveBeenCalledWith(false); | ||
expect(setLibraryAssetPath).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
expect(mockSetFileName).toHaveBeenCalledWith(fileName); | ||
expect(mockSetFileContent).toHaveBeenCalledWith('content'); | ||
test('handleAddFileClick', () => { | ||
const setIsFileNameDialogOpen = jest.fn(); | ||
|
||
handleAddFileClick(setIsFileNameDialogOpen); | ||
|
||
expect(setIsFileNameDialogOpen).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
test('handleCloseFileNameDialog', () => { | ||
const setIsFileNameDialogOpen = jest.fn(); | ||
const setNewFileName = jest.fn(); | ||
const setErrorMessage = jest.fn(); | ||
|
||
handleCloseFileNameDialog( | ||
setIsFileNameDialogOpen, | ||
setNewFileName, | ||
setErrorMessage, | ||
); | ||
|
||
expect(setIsFileNameDialogOpen).toHaveBeenCalledWith(false); | ||
expect(setNewFileName).toHaveBeenCalledWith(''); | ||
expect(setErrorMessage).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
test('handleFileSubmit with existing file', () => { | ||
const files: FileState[] = [ | ||
{ name: 'testFile', content: 'content', isNew: false, isModified: false }, | ||
]; | ||
const setErrorMessage = jest.fn(); | ||
const setIsFileNameDialogOpen = jest.fn(); | ||
const setNewFileName = jest.fn(); | ||
|
||
handleFileSubmit( | ||
files, | ||
'testFile', | ||
setErrorMessage, | ||
dispatch, | ||
setIsFileNameDialogOpen, | ||
setNewFileName, | ||
); | ||
|
||
expect(setErrorMessage).toHaveBeenCalledWith( | ||
'A file with this name already exists.', | ||
); | ||
}); | ||
|
||
test('handleFileSubmit with new file', () => { | ||
const files: FileState[] = []; | ||
const setErrorMessage = jest.fn(); | ||
const setIsFileNameDialogOpen = jest.fn(); | ||
const setNewFileName = jest.fn(); | ||
|
||
handleFileSubmit( | ||
files, | ||
'newFile', | ||
setErrorMessage, | ||
dispatch, | ||
setIsFileNameDialogOpen, | ||
setNewFileName, | ||
); | ||
|
||
expect(setErrorMessage).toHaveBeenCalledWith(''); | ||
expect(dispatch).toHaveBeenCalled(); | ||
expect(setIsFileNameDialogOpen).toHaveBeenCalledWith(false); | ||
expect(setNewFileName).toHaveBeenCalledWith(''); | ||
}); | ||
}); | ||
*/ |
Oops, something went wrong.