Skip to content

Commit feac4fd

Browse files
committed
Merge branch 'dev' into Ahnafkhvn/testing
2 parents 0c2d76d + d20ba13 commit feac4fd

28 files changed

+531
-183
lines changed

__tests__/BottomTabs.test.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
import React from 'react';
2-
import { Provider } from 'react-redux';
31
import '@testing-library/jest-dom';
2+
43
import {
4+
fireEvent,
55
render,
66
screen,
7-
fireEvent,
87
waitFor,
98
within
109
} from '@testing-library/react';
10+
1111
import BottomTabs from '../app/src/components/bottom/BottomTabs';
12-
import ContextManager from '../app/src/components/ContextAPIManager/ContextManager';
13-
import store from '../app/src/redux/store';
12+
import { BrowserRouter } from 'react-router-dom';
1413
import ComponentPanel from '../app/src/components/right/ComponentPanel';
15-
import HTMLPanel from '../app/src/components/left/HTMLPanel';
16-
import StateManager from '../app/src/components/StateManagement/StateManagement';
14+
import ContextManager from '../app/src/components/ContextAPIManager/ContextManager';
1715
import CustomizationPanel from '../app/src/containers/CustomizationPanel';
18-
import { BrowserRouter } from 'react-router-dom';
19-
import DragDropPanel from '../app/src/components/left/DragDropPanel';
20-
import MainContainer from '../app/src/containers/MainContainer';
2116
import { DndProvider } from 'react-dnd';
17+
import DragDropPanel from '../app/src/components/left/DragDropPanel';
2218
import { HTML5Backend } from 'react-dnd-html5-backend';
19+
import HTMLPanel from '../app/src/components/left/HTMLPanel';
20+
import MainContainer from '../app/src/containers/MainContainer';
21+
import { Provider } from 'react-redux';
22+
import React from 'react';
23+
import StateManager from '../app/src/components/StateManagement/StateManagement';
24+
import store from '../app/src/redux/store';
2325

2426
describe('Bottom Panel Render Test', () => {
25-
test('should render all seven tabs', () => {
27+
test('should render all six tabs', () => {
2628
render(
2729
<Provider store={store}>
2830
<BottomTabs />
2931
</Provider>
3032
);
31-
expect(screen.getAllByRole('tab')).toHaveLength(7);
32-
expect(screen.getByText('Code Preview')).toBeInTheDocument();
33+
expect(screen.getAllByRole('tab')).toHaveLength(6);
34+
// expect(screen.getByText('Code Preview')).toBeInTheDocument();
3335
expect(screen.getByText('Component Tree')).toBeInTheDocument();
3436
expect(screen.getByText('Creation Panel')).toBeInTheDocument();
3537
expect(screen.getByText('Customization')).toBeInTheDocument();

__tests__/DragAndDrop.test.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React from 'react';
21
import '@testing-library/jest-dom';
3-
import { DndProvider } from 'react-dnd';
4-
import { HTML5Backend } from 'react-dnd-html5-backend';
2+
53
import { fireEvent, render, screen } from '@testing-library/react';
6-
import DragDropPanel from '../app/src/components/left/DragDropPanel';
4+
75
import ComponentDrag from '../app/src/components/left/ComponentDrag';
6+
import { DndProvider } from 'react-dnd';
7+
import DragDropPanel from '../app/src/components/left/DragDropPanel';
8+
import { HTML5Backend } from 'react-dnd-html5-backend';
9+
import MainContainer from '../app/src/containers/MainContainer';
810
import { Provider } from 'react-redux';
11+
import React from 'react';
912
import store from '../app/src/redux/store';
10-
import MainContainer from '../app/src/containers/MainContainer';
1113
import { within } from '@testing-library/react';
1214

13-
1415
function TestContext(component) {
1516
return (
1617
<Provider store={store}>
@@ -48,7 +49,7 @@ describe('Drag and Drop Side Panel', () => {
4849
expect(screen.getByText('LinkTo')).toBeInTheDocument();
4950
});
5051

51-
test('Should render Roots Components and Reusbale components', () => {
52+
test.skip('Should render Roots Components and Reusbale components', () => {
5253
render(TestContext(<ComponentDrag />));
5354

5455
expect(screen.getByText('Root Components')).toBeInTheDocument();

__tests__/marketplace.test.tsx

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import '@testing-library/jest-dom/extend-expect';
2+
3+
import { fireEvent, render, screen } from '@testing-library/react';
4+
5+
import MarketplaceCard from '../app/src/components/marketplace/MarketplaceCard';
6+
import MarketplaceCardContainer from '../app/src/components/marketplace/MarketplaceCardContainer';
7+
import { Provider } from 'react-redux';
8+
import React from 'react';
9+
import SearchBar from '../app/src/components/marketplace/Searchbar';
10+
import axios from 'axios';
11+
import store from '../app/src/redux/store';
12+
13+
// Mocking the axios module to avoid actual network calls
14+
jest.mock('axios');
15+
jest.mock(
16+
'resources/marketplace_images/marketplace_image.png',
17+
() => 'mock-image-url'
18+
);
19+
20+
describe('MarketplaceCard Render Test', () => {
21+
const mockProject = {
22+
_id: 123,
23+
name: 'Sample Project',
24+
username: 'user123',
25+
forked: 'false',
26+
comments: [],
27+
createdAt: new Date(),
28+
likes: 0,
29+
project: {
30+
id: 'sample-project-id'
31+
},
32+
published: true,
33+
userId: 123456
34+
};
35+
36+
it('displays project name and username', () => {
37+
render(
38+
<Provider store={store}>
39+
<MarketplaceCard proj={mockProject} />
40+
</Provider>
41+
);
42+
43+
expect(screen.getByText('Sample Project')).toBeInTheDocument();
44+
expect(screen.getByText('user123')).toBeInTheDocument();
45+
});
46+
});
47+
48+
describe('MarketplaceContainer', () => {
49+
const mockProjects = [
50+
{
51+
_id: 1,
52+
name: 'Project 1',
53+
username: 'user1'
54+
},
55+
{
56+
_id: 2,
57+
name: 'Project 2',
58+
username: 'user2'
59+
}
60+
];
61+
62+
beforeEach(() => {
63+
// Set up mock axios call for every test
64+
axios.get = jest.fn().mockResolvedValue({ data: mockProjects });
65+
});
66+
67+
it('renders multiple MarketplaceCards', () => {
68+
render(
69+
<Provider store={store}>
70+
<MarketplaceCardContainer displayProjects={mockProjects} />
71+
</Provider>
72+
);
73+
74+
expect(screen.getByText('Project 1')).toBeInTheDocument();
75+
expect(screen.getByText('user1')).toBeInTheDocument();
76+
expect(screen.getByText('Project 2')).toBeInTheDocument();
77+
expect(screen.getByText('user2')).toBeInTheDocument();
78+
});
79+
});
80+
81+
const mockProjects = [
82+
{
83+
name: 'Sample Project',
84+
username: 'user123'
85+
},
86+
{
87+
name: 'Test Project',
88+
username: 'user_test'
89+
},
90+
{
91+
name: 'Hello Project',
92+
username: 'hello_user'
93+
}
94+
];
95+
96+
describe('SearchBar Component', () => {
97+
it('updates the text field value on change', () => {
98+
const updateDisplayProjects = jest.fn();
99+
100+
render(
101+
<SearchBar
102+
marketplaceProjects={mockProjects}
103+
updateDisplayProjects={updateDisplayProjects}
104+
/>
105+
);
106+
107+
const textField = screen.getByLabelText('Search') as HTMLInputElement;
108+
fireEvent.change(textField, { target: { value: 'Sample' } });
109+
110+
expect(textField.value).toBe('Sample');
111+
});
112+
113+
it('filters projects by username', () => {
114+
const updateDisplayProjects = jest.fn();
115+
116+
render(
117+
<SearchBar
118+
marketplaceProjects={mockProjects}
119+
updateDisplayProjects={updateDisplayProjects}
120+
/>
121+
);
122+
123+
const textField = screen.getByLabelText('Search');
124+
fireEvent.change(textField, { target: { value: 'test' } });
125+
126+
// Using setImmediate to wait for useEffect to execute.
127+
setTimeout(() => {
128+
expect(updateDisplayProjects).toHaveBeenCalledWith([
129+
{ name: 'Test Project', username: 'user_test' }
130+
]);
131+
});
132+
});
133+
});

__tests__/projects.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const http = require('http')
1111
const {state, projectToSave } = mockData
1212

1313
// save and get projects endpoint testing
14-
xdescribe('Project endpoints tests', () => {
14+
describe('Project endpoints tests', () => {
1515
let server;
1616
beforeAll((done) => {
1717
server = http.createServer(app);
@@ -22,7 +22,7 @@ xdescribe('Project endpoints tests', () => {
2222
server.close(done);
2323
});
2424
// test saveProject endpoint
25-
xdescribe('/saveProject', () => {
25+
describe('/saveProject', () => {
2626
describe('/POST', () => {
2727
it('responds with a status of 200 and json object equal to project sent', () => {
2828
return request(server)
@@ -36,7 +36,7 @@ xdescribe('Project endpoints tests', () => {
3636
});
3737
});
3838
// test getProjects endpoint
39-
xdescribe('/getProjects', () => {
39+
describe('/getProjects', () => {
4040
describe('POST', () => {
4141
it('responds with status of 200 and json object equal to an array of user projects', () => {
4242
return request(server)
@@ -53,7 +53,7 @@ xdescribe('Project endpoints tests', () => {
5353
});
5454
});
5555
// test deleteProject endpoint
56-
xdescribe('/deleteProject', () => {
56+
describe('/deleteProject', () => {
5757
describe('DELETE', () => {
5858
const { name, userId } = projectToSave;
5959
it('responds with status of 200 and json object equal to deleted project', () => {

__tests__/server.test.tsx

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import marketplaceController from '../server/controllers/marketplaceController';
2+
import app from '../server/server';
3+
import mockData from '../mockData';
4+
const request = require('supertest');
5+
const mongoose = require('mongoose');
6+
const mockNext = jest.fn(); // Mock nextFunction
7+
const MONGO_DB = process.env.MONGO_DB_TEST;
8+
const { state, projectToSave, user } = mockData
9+
const PORT = 8080;
10+
11+
beforeAll(async () => {
12+
await mongoose.connect(MONGO_DB, {
13+
useNewUrlParser: true,
14+
useUnifiedTopology: true,
15+
});
16+
});
17+
18+
afterAll(async () => {
19+
await mongoose.connection.close();
20+
});
21+
22+
describe('Server endpoint tests', () => {
23+
it('should pass this test request', async () => {
24+
const response = await request(app).get('/test');
25+
expect(response.status).toBe(200);
26+
expect(response.text).toBe('test request is working');
27+
});
28+
describe('Marketplace endpoint testing', () => {
29+
it('get requests to /getMarketplaceProjects should return an array of projects', async () => {
30+
const response = await request(app).get('/getMarketplaceProjects');
31+
expect(response.status).toBe(200);
32+
expect(Array.isArray(response.body)).toBe(true);
33+
});
34+
it('the return array should be populated with project objects', async () => {
35+
const response = await request(app).get('/getMarketplaceProjects');
36+
expect(response.status).toBe(200);
37+
expect(Array.isArray(response.body)).toBe(true);
38+
});
39+
});
40+
// test saveProject endpoint
41+
describe('/saveProject', () => {
42+
describe('/POST', () => {
43+
it('responds with a status of 200 and json object equal to project sent', async () => {
44+
// const response = await request(app).post('/saveProject').set('Accept', 'application/json').send(projectToSave);
45+
// console.log(response);
46+
// console.log(response.body);
47+
// expect(response.status).toBe(200);
48+
return request(app)
49+
.post('/saveProject')
50+
.set('Accept', 'application/json')
51+
.send(projectToSave)
52+
.expect(200)
53+
.expect('Content-Type', /application\/json/)
54+
.then((res) => expect(res.body.name).toBe(projectToSave.name));
55+
});
56+
// });
57+
});
58+
// test getProjects endpoint
59+
describe('/getProjects', () => {
60+
describe('POST', () => {
61+
it('responds with status of 200 and json object equal to an array of user projects', () => {
62+
return request(app)
63+
.post('/getProjects')
64+
.set('Accept', 'application/json')
65+
.send({ userId: projectToSave.userId })
66+
.expect(200)
67+
.expect('Content-Type', /json/)
68+
.then((res) => {
69+
expect(Array.isArray(res.body)).toBeTruthy;
70+
expect(res.body[0].name).toBe(state.name);
71+
});
72+
});
73+
});
74+
});
75+
// test deleteProject endpoint
76+
describe('/deleteProject', () => {
77+
describe('DELETE', () => {
78+
it('responds with status of 200 and json object equal to deleted project', async () => {
79+
const response: Response = await request(app).post('/getProjects').set('Accept', 'application/json').send({ userId: projectToSave.userId });
80+
const _id: String = response.body[0]._id;
81+
const userId: String = user.username;
82+
console.log(_id, userId);
83+
return request(app)
84+
.delete('/deleteProject')
85+
.set('Content-Type', 'application/json')
86+
.send({ _id, userId })
87+
.expect(200)
88+
.then((res) => expect(res.body.name).toBe(projectToSave.name)); // @Denton might want to check more of these fields
89+
});
90+
});
91+
});
92+
});
93+
});
94+
95+
96+
97+
98+
99+
100+
101+
// describe('marketplaceController Middleware', () => {
102+
// describe('getProjects tests', () => {
103+
// it('should add the projects as an array to res.locals', () => {
104+
// const req = {};
105+
// const res = { locals: {} };
106+
// console.log(marketplaceController.getPublishedProjects);
107+
// console.log(typeof marketplaceController.getPublishedProjects);
108+
// marketplaceController.getPublishedProjects(req, res, mockNext);
109+
// expect(Array.isArray(res.locals.publishedProjects)).toBe(true);
110+
// expect(mockNext).toHaveBeenCalled();
111+
// });
112+
// });
113+
114+
115+
// it('should send an error response if there is an error in the middleware', () => {
116+
// const req = { user: { isAuthenticated: false } };
117+
// const res = mockResponse();
118+
119+
// marketplaceController.authenticateMiddleware(req, res, mockNext);
120+
121+
// expect(res.status).toHaveBeenCalledWith(500);
122+
// expect(res.json).toHaveBeenCalledWith({ err: 'Error in marketplaceController.getPublishedProjects, check server logs for details' });
123+
// });
124+
// });

0 commit comments

Comments
 (0)