Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vanessa committed Nov 7, 2024
1 parent 791b6d9 commit 667b76f
Show file tree
Hide file tree
Showing 11 changed files with 824 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,19 @@ describe('AssetBoard Integration Tests', () => {
expect(screen.queryByText('Asset 1')).not.toBeInTheDocument();
});
});

it('shows an error message', async () => {
const error = 'An error occurred';
jest.spyOn(React, 'useState').mockReturnValue([error, jest.fn()]);

act(() => {
render(
<Provider store={store}>
<AssetBoard tab="Manage" />
</Provider>,
);
});

expect(screen.getByText(error)).toBeInTheDocument();
});
});
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');
});
});
});
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();
});
});
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('');
});
});
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();
});
});
Loading

0 comments on commit 667b76f

Please sign in to comment.