Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vanessa committed Dec 6, 2024
1 parent fb0aa62 commit b880164
Show file tree
Hide file tree
Showing 12 changed files with 697 additions and 277 deletions.
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"eslint-plugin-jest": "^28.8.3",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-react": "^7.33.2",
"jest-fetch-mock": "^3.0.3",
"katex": "^0.16.11",
"markdown-it-katex": "^2.0.3",
"oidc-client-ts": "^3.0.1",
Expand Down
132 changes: 132 additions & 0 deletions client/test/preview/integration/components/asset/AssetLibrary.test.tsx
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();
});
});
});
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('');
});
});
*/
Loading

0 comments on commit b880164

Please sign in to comment.