Skip to content

Commit

Permalink
Added Test
Browse files Browse the repository at this point in the history
Config files changes to work with d3-scale (mui/mui-x#11568)
  • Loading branch information
joaopflausino committed Jun 18, 2024
1 parent 9ce36fe commit 6c90be0
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 7 deletions.
5 changes: 3 additions & 2 deletions frontend/goldinvesting/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"]
}

14 changes: 14 additions & 0 deletions frontend/goldinvesting/src/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
// ... other configurations
transform: {
"^.+\\.[t|j]sx?$": "babel-jest",
"^.+\\.svg$": "jest-transform-stub",
"^.+\\.css$": "jest-transform-stub",
},
moduleNameMapper: {
"\\.(css|less|scss|sass)$": "identity-obj-proxy",
},

setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
};

12 changes: 7 additions & 5 deletions frontend/goldinvesting/src/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
// setupTests.ts
import '@testing-library/jest-dom/extend-expect';

// Mocking the PieChart component globally
jest.mock('@mui/x-charts/PieChart', () => ({
PieChart: jest.fn().mockImplementation(({ children }) => children),
}));
83 changes: 83 additions & 0 deletions frontend/goldinvesting/src/tests/Login.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Login.test.tsx
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { Provider } from 'react-redux';
import { MemoryRouter } from 'react-router-dom';
import { createStore } from 'redux';
import Login from '../Pages/login/Login';
import rootReducer from '../store/rootReducer';
import LocalStorageService from '../app/services/localStorageService';

jest.mock('../app/services/localStorageService');

const store = createStore(rootReducer);

describe('Login Component', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('renders login form', () => {
render(
<Provider store={store}>
<MemoryRouter>
<Login />
</MemoryRouter>
</Provider>
);

expect(screen.getByLabelText(/E-mail/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Senha/i)).toBeInTheDocument();
expect(screen.getByText(/Login/i)).toBeInTheDocument();
});

test('shows error message when fields are empty', () => {
render(
<Provider store={store}>
<MemoryRouter>
<Login />
</MemoryRouter>
</Provider>
);

fireEvent.click(screen.getByText(/Entrar/i));

expect(screen.getByText(/Preencha todos os campos/i)).toBeInTheDocument();
});

test('shows error message with invalid credentials', () => {
render(
<Provider store={store}>
<MemoryRouter>
<Login />
</MemoryRouter>
</Provider>
);

fireEvent.change(screen.getByLabelText(/E-mail/i), { target: { value: 'wrong' } });
fireEvent.change(screen.getByLabelText(/Senha/i), { target: { value: 'wrong' } });
fireEvent.click(screen.getByText(/Entrar/i));

expect(screen.getByText(/Invalid username or password/i)).toBeInTheDocument();
});

test('calls login with correct credentials', () => {
const fakeUser = 'user';
const fakePassword = 'password';

render(
<Provider store={store}>
<MemoryRouter>
<Login />
</MemoryRouter>
</Provider>
);

fireEvent.change(screen.getByLabelText(/E-mail/i), { target: { value: fakeUser } });
fireEvent.change(screen.getByLabelText(/Senha/i), { target: { value: fakePassword } });
fireEvent.click(screen.getByText(/Entrar/i));

expect(LocalStorageService.addItem).toHaveBeenCalledWith('isAuthenticated', true);
expect(LocalStorageService.addItem).toHaveBeenCalledWith('user', fakeUser);
});
});
64 changes: 64 additions & 0 deletions frontend/goldinvesting/src/tests/News.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// InvestmentCalculator.test.tsx
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import InvestmentCalculator from '../Pages/simulateInvestment/SimulateInvestment';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import rootReducer from '../store/rootReducer';


// Mocking the PieChart component
jest.mock('@mui/x-charts/PieChart', () => ({
PieChart: jest.fn().mockImplementation(({ children }) => <div>{children}</div>),
}));

const store = createStore(rootReducer);

describe('InvestmentCalculator Component', () => {
test('renders the form fields correctly', () => {
render(
<Provider store={store}>
<InvestmentCalculator />
</Provider>
);

expect(screen.getByLabelText(/Initial Investment/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Monthly Inflation Rate/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Monthly Contribution/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Investment Period \(months\)/i)).toBeInTheDocument();
expect(screen.getByLabelText(/Monthly Interest Rate/i)).toBeInTheDocument();
});

test('shows error message when required fields are empty', () => {
render(
<Provider store={store}>
<InvestmentCalculator />
</Provider>
);

fireEvent.click(screen.getByText(/Calculate/i));

expect(screen.getByText(/Please fill in all required fields with valid numbers./i)).toBeInTheDocument();
});

test('calculates and displays results when form is submitted with valid inputs', () => {
render(
<Provider store={store}>
<InvestmentCalculator />
</Provider>
);

fireEvent.change(screen.getByLabelText(/Initial Investment/i), { target: { value: '1000' } });
fireEvent.change(screen.getByLabelText(/Monthly Inflation Rate/i), { target: { value: '0.5' } });
fireEvent.change(screen.getByLabelText(/Monthly Contribution/i), { target: { value: '100' } });
fireEvent.change(screen.getByLabelText(/Investment Period \(months\)/i), { target: { value: '12' } });
fireEvent.change(screen.getByLabelText(/Monthly Interest Rate/i), { target: { value: '1' } });

fireEvent.click(screen.getByText(/Calculate/i));

expect(screen.getByText(/Results:/i)).toBeInTheDocument();
expect(screen.getByText(/Future Value:/i)).toBeInTheDocument();
expect(screen.getByText(/Total Invested:/i)).toBeInTheDocument();
expect(screen.getByText(/Gross Profit:/i)).toBeInTheDocument();
});
});

0 comments on commit 6c90be0

Please sign in to comment.