Skip to content

Commit 4a3ae1d

Browse files
author
Jeffrey Na
committed
testing HealthChart and fixed Login.test
1 parent 4d59e51 commit 4a3ae1d

File tree

7 files changed

+116
-40
lines changed

7 files changed

+116
-40
lines changed

__tests__/charts/HealthChart.test.tsx

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import React from 'react';
2+
import HealthChart from '../../app/charts/HealthChart';
3+
import { render, screen } from '@testing-library/react';
4+
5+
// import mockData from '../mock_data.json';
6+
7+
const mockData = {
8+
ServiceName: {
9+
Metric: {
10+
time: [
11+
'2023-06-09T15:18:25.195Z',
12+
'2023-06-09T15:18:20.194Z',
13+
'2023-06-09T15:18:15.192Z',
14+
'2023-06-09T15:18:10.191Z',
15+
'2023-06-09T15:18:05.190Z',
16+
],
17+
value: [1208074240, 1282670592, 1243414528, 1278115840, 117178368],
18+
},
19+
},
20+
};
21+
22+
jest.mock('electron', () => ({
23+
ipcRenderer: {
24+
send: () => jest.fn(),
25+
on: () => mockData,
26+
},
27+
}));
28+
29+
jest.mock('react-plotly.js', () => (props) {
30+
const div = document.createElement('div');
31+
32+
})
33+
34+
describe('HealthChart', () => {
35+
const props = {
36+
key: 'testKey',
37+
dataType: 'Memory in Megabytes',
38+
chartData: mockData,
39+
categoryName: 'Test Name',
40+
sizing: 'all',
41+
colourGenerator: jest.fn(),
42+
};
43+
44+
let graph;
45+
beforeEach(() => {
46+
render(<HealthChart {...props} />);
47+
48+
graph = screen.getByTestId('Health Chart').firstChild;
49+
});
50+
51+
it('Should render', () => {
52+
expect(screen).toBeTruthy();
53+
});
54+
55+
it('Should render graph', () => {
56+
expect(graph).toBeTruthy();
57+
});
58+
59+
it('Should not scroll', () => {
60+
expect(graph.scrollWidth).toBe(0);
61+
expect(graph.scrollHeight).toBe(0);
62+
expect(graph.scrollLeft).toBe(0);
63+
expect(graph.scrollTop).toBe(0);
64+
});
65+
66+
it('Should have width 800, height 600, and white background', () => {
67+
// console.log('graph', graph.firstChild);
68+
console.log('graph.outerHTML: ', graph.outerHTML);
69+
70+
expect(graph.outerHTML.includes('width: 800px')).toBeTruthy();
71+
expect(graph.outerHTML.includes('height: 600px')).toBeTruthy();
72+
expect(graph.outerHTML.includes('style="background: white;"')).toBeTruthy();
73+
});
74+
75+
it('Should have correct colors', () => {
76+
expect(graph.outerHTML.includes('{fill: #fc4039;}')).toBeTruthy();
77+
});
78+
79+
it('Should have correct data on y-axis based off mock data', () => {
80+
console.log('GRAPH DATA: ', graph.data);
81+
expect(graph.data[0].y[0]).toBe((mockData.ServiceName.Metric.value[0] / 1000000).toFixed(2));
82+
expect(graph.data[0].y[1]).toBe((mockData.ServiceName.Metric.value[1] / 1000000).toFixed(2));
83+
});
84+
85+
it('Should have correct time on x-axis based off mock data', () => {
86+
const expectedOutput = ['15:18:25', '15:18:20', '15:18:15', '15:18:10', '15:18:05'];
87+
88+
expect(graph.data[0].x[0]).toEqual(expectedOutput[0]);
89+
expect(graph.data[0].x[1]).toEqual(expectedOutput[1]);
90+
});
91+
92+
// it('Should have the correct service name on the graph', () => {
93+
// expect(graph).outerHTML.includes('title').toBe('ServiceName');
94+
// });
95+
96+
// describe('prettyTimeInReverse', () => {
97+
// test('Should reverse time and format properly', () => {
98+
// const result = HealthChart.prettyTimeInReverse(mockData.ServiceName.Metric.time);
99+
// expect(result).toEqual(expectedOutput);
100+
// });
101+
// });
102+
});

__tests__/components/Login.test.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ describe('Create Admin Page', () => {
3636
it('Login button should submit username and password to addUser', () => {
3737
const element = screen.getByTestId('Login');
3838
const inputs = element.querySelectorAll('input');
39-
inputs[0].value = 'St1nky';
40-
inputs[1].value = 'me123';
41-
fireEvent.click(element);
39+
40+
const usernameInput = screen.getByPlaceholderText('username');
41+
const passwordInput = screen.getByPlaceholderText('password');
42+
const loginButton = screen.getByRole('button', { name: /Login/i });
43+
44+
fireEvent.change(usernameInput, { target: { value: 'St1nky' } });
45+
fireEvent.change(passwordInput, { target: { value: 'me123' } });
46+
47+
fireEvent.click(loginButton);
4248
// expect(ipcRenderer.sendSync).toHaveBeenCalled;
4349
// above passes test but below fails and says number of calls is zero
4450
expect(ipcRenderer.sendSync).toHaveBeenCalledWith('login', {

__tests__/mock_data.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
"time": ["", ""],
1414
"totalmemory": [1, 4],
1515
"usedmemory": [3, 6]
16-
}]
16+
}]
17+

app/charts/HealthChart.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const HealthChart: React.FC<HealthChartProps> = React.memo(props => {
5050
// generates an array of plotly data objects to be passed into our plotly chart's data prop
5151
const generatePlotlyDataObjects = (chartData: object): object[] => {
5252
const arrayOfPlotlyDataObjects: PlotlyData[] = [];
53+
console.log('chartData:::::::: ', chartData);
5354
// iterate through the chartData
5455
for (const serviceName in chartData) {
5556
// define the metrics for this service

app/components/Login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const Login = React.memo(() => {
5151
<button className="link" id="submitBtn" type="submit">
5252
Login
5353
</button>
54-
<button className="link needAccount" onClick={() => navigate('/signup')}>
54+
<button className="link needAccount" type="button" onClick={() => navigate('/signup')}>
5555
Need an account?
5656
</button>
5757
</form>

jest.config.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
"node_modules",
2424
"dist"
2525
],
26-
"include": ["./electron/**/*", "./electron/**/*.json", "jest.config.js"]
26+
"include": ["./electron/**/*", "./electron/**/*.json", "jest.config.ts"]
2727
}

0 commit comments

Comments
 (0)