Skip to content

Commit 0b0cb3a

Browse files
committed
Merge branch 'master' into app-tests
2 parents 2a1b3d1 + bf7596c commit 0b0cb3a

11 files changed

Lines changed: 412 additions & 63 deletions

File tree

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
1-
xdescribe('<ApplicationContext />', () => {
2-
xit('should');
1+
import React, { useContext } from 'react';
2+
import { mount } from 'enzyme';
3+
const { ipcRenderer } = require('electron');
4+
5+
import ApplicationContextProvider, {
6+
ApplicationContext,
7+
} from '../../../app/context/ApplicationContext';
8+
9+
// Setup mock ipc processes
10+
jest.mock('electron', () => ({ ipcRenderer: { on: jest.fn(), send: jest.fn() } }));
11+
12+
describe('<ApplicationContext />', () => {
13+
let wrapper: any;
14+
beforeEach(() => {
15+
// Test component that accesses Application Context
16+
const TestComponent = () => {
17+
const { connectToDB, fetchServicesNames, setServicesData, servicesData } = useContext(
18+
ApplicationContext
19+
);
20+
21+
return (
22+
<>
23+
<p>{servicesData}</p>
24+
<button id="connectToDB" onClick={() => connectToDB(1)}>
25+
Test connectToDB
26+
</button>
27+
<button id="fetchServicesNames" onClick={() => fetchServicesNames()}>
28+
Test fetchServicesNames
29+
</button>
30+
<button id="setServicesData" onClick={() => setServicesData('new data')}>
31+
Test setServicesData
32+
</button>
33+
</>
34+
);
35+
};
36+
37+
// Provide ApplicationContext to component
38+
wrapper = mount(
39+
<ApplicationContextProvider>
40+
<TestComponent />
41+
</ApplicationContextProvider>
42+
);
43+
});
44+
45+
it('should render', () => {
46+
expect(wrapper.exists).toBeTruthy;
47+
});
48+
49+
it("should emit the 'connect' event and pass in an 'index' when connectToDB is invoked", () => {
50+
const button = wrapper.find('#connectToDB');
51+
button.simulate('click');
52+
expect(ipcRenderer.send).toHaveBeenCalledWith('connect', 1);
53+
});
54+
55+
it("should emit the 'servicesRequest' event and listen on the 'servicesResponse' when invoking fetchServicesNames", () => {
56+
const button = wrapper.find('#fetchServicesNames');
57+
button.simulate('click');
58+
expect(ipcRenderer.send).toHaveBeenCalledWith('servicesRequest');
59+
expect(ipcRenderer.on).toBeCalledWith('servicesResponse', expect.any(Function));
60+
});
61+
62+
it('should update servicesData when setServicesData is invoked with new data', () => {
63+
const setter = wrapper.find('#setServicesData');
64+
setter.simulate('click');
65+
expect(wrapper.find('p').text()).toEqual('new data');
66+
});
367
});
Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,77 @@
1-
xdescribe('<DashboardContext />', () => {
2-
xit('should');
1+
import React, { useContext } from 'react';
2+
import { mount } from 'enzyme';
3+
import DashboardContextProvider, { DashboardContext } from '../../../app/context/DashboardContext';
4+
const { ipcRenderer } = require('electron');
5+
6+
interface IAppInfo {
7+
database: string;
8+
URI: string;
9+
name: string;
10+
}
11+
12+
// Setup mock ipc processes
13+
jest.mock('electron', () => ({ ipcRenderer: { sendSync: jest.fn() } }));
14+
15+
describe('<DashboardContext />', () => {
16+
let wrapper: any;
17+
let mockAppInfo: IAppInfo;
18+
beforeEach(() => {
19+
mockAppInfo = {
20+
database: 'mockType',
21+
URI: 'mockURI',
22+
name: 'mockName',
23+
};
24+
// Mock component that has access to Dashboard Context
25+
const TestComponent = () => {
26+
const { applications, getApplications, addApp, deleteApp } = useContext(DashboardContext);
27+
28+
return (
29+
<>
30+
<p>{JSON.stringify(applications)}</p>
31+
<button id="addApp" onClick={() => addApp(mockAppInfo)}>
32+
Test addApp
33+
</button>
34+
<button id="deleteApp" onClick={() => deleteApp(7)}>
35+
Test deleteApp
36+
</button>
37+
<button id="getApplications" onClick={() => getApplications()}>
38+
Test getApplications
39+
</button>
40+
</>
41+
);
42+
};
43+
44+
// Provide DashboardContext to component
45+
wrapper = mount(
46+
<DashboardContextProvider>
47+
<TestComponent />
48+
</DashboardContextProvider>
49+
);
50+
});
51+
52+
it('should render', () => {
53+
expect(wrapper.exists).toBeTruthy;
54+
});
55+
56+
it("should emit the 'addApp' event when invoking addApp", () => {
57+
const { name, database, URI } = mockAppInfo;
58+
const button = wrapper.find('#addApp');
59+
button.simulate('click');
60+
expect(ipcRenderer.sendSync).toHaveBeenCalledWith(
61+
'addApp',
62+
JSON.stringify([name, database, URI])
63+
);
64+
});
65+
66+
it("should emit the 'deleteApp' event when invoking deleteApp", () => {
67+
const button = wrapper.find('#deleteApp');
68+
button.simulate('click');
69+
expect(ipcRenderer.sendSync).toHaveBeenCalledWith('deleteApp', 7);
70+
});
71+
72+
it("should emit the 'getApps' event when invoking getApplications", () => {
73+
const button = wrapper.find('#getApplications');
74+
button.simulate('click');
75+
expect(ipcRenderer.sendSync).toHaveBeenCalledWith('getApps');
76+
});
377
});
Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,118 @@
1-
xdescribe('<HealthContext />', () => {
2-
xit('should');
1+
import React, { useContext, useState } from 'react';
2+
import { mount } from 'enzyme';
3+
const { ipcRenderer } = require('electron');
4+
5+
import HealthContextProvider, { HealthContext } from '../../../app/context/HealthContext';
6+
7+
// Setup electron mock
8+
jest.mock('electron', () => ({ ipcRenderer: { on: jest.fn(), send: jest.fn() } }));
9+
10+
describe('<HealthContext />', () => {
11+
let wrapper: any;
12+
beforeEach(() => {
13+
// Mock component that has access to HealthContext
14+
const TestComponent = () => {
15+
const { fetchHealthData, healthData, setHealthData, parseHealthData } = useContext(
16+
HealthContext
17+
);
18+
const [mockData, setMockData] = useState([
19+
{
20+
activememory: 12,
21+
blockedprocesses: 98,
22+
cpuloadpercent: 12,
23+
cpuspeed: 43,
24+
cputemp: 5,
25+
freememory: 5,
26+
id: 6,
27+
latency: 41,
28+
runningprocesses: 12,
29+
sleepingprocesses: 312,
30+
time: '',
31+
totalmemory: 1,
32+
usememory: 3,
33+
},
34+
{
35+
activememory: 13,
36+
blockedprocesses: 43,
37+
cpuloadpercent: 1,
38+
cpuspeed: 32,
39+
cputemp: 32,
40+
freememory: 76,
41+
id: 8,
42+
latency: 51,
43+
runningprocesses: 153,
44+
sleepingprocesses: 33,
45+
time: '',
46+
totalmemory: 4,
47+
usememory: 6,
48+
},
49+
]);
50+
51+
return (
52+
<>
53+
<div id="healthData">{JSON.stringify(healthData)}</div>
54+
<div id="parsedData">{JSON.stringify(mockData)}</div>
55+
<button id="fetchHealthData" onClick={() => fetchHealthData('books')}>
56+
Test fetchHealthData
57+
</button>
58+
<button id="setHealthData" onClick={() => setHealthData({ foo: 'bar' })}>
59+
Test setHealthData
60+
</button>
61+
<button
62+
id="parseHealthData"
63+
onClick={() => {
64+
const result = parseHealthData(mockData);
65+
setMockData(result);
66+
}}
67+
></button>
68+
</>
69+
);
70+
};
71+
72+
// Provide HealthContext to component
73+
wrapper = mount(
74+
<HealthContextProvider>
75+
<TestComponent />
76+
</HealthContextProvider>
77+
);
78+
});
79+
80+
it('should render', () => {
81+
expect(wrapper.exists).toBeTruthy;
82+
});
83+
84+
it('should parse incoming health data into an object of arrays for every data type', () => {
85+
const parsedData = {
86+
activememory: [12, 13],
87+
blockedprocesses: [98, 43],
88+
cpuloadpercent: [12, 1],
89+
cpuspeed: [43, 32],
90+
cputemp: [5, 32],
91+
freememory: [5, 76],
92+
id: [6, 8],
93+
latency: [41, 51],
94+
runningprocesses: [12, 153],
95+
sleepingprocesses: [312, 33],
96+
time: ['', ''],
97+
totalmemory: [1, 4],
98+
usememory: [3, 6],
99+
};
100+
101+
const button = wrapper.find('#parseHealthData');
102+
button.simulate('click');
103+
expect(wrapper.find('#parsedData').text()).toEqual(JSON.stringify(parsedData));
104+
});
105+
106+
it("should emit the 'healthRequest' event and listen on 'healthResponse' when invoking fetchHealthData", () => {
107+
const button = wrapper.find('#fetchHealthData');
108+
button.simulate('click');
109+
expect(ipcRenderer.send).toHaveBeenCalledWith('healthRequest', 'books');
110+
expect(ipcRenderer.on).toHaveBeenCalledWith('healthResponse', expect.any(Function));
111+
});
112+
113+
it('should update healthData when setHealthData is invoked with new data', () => {
114+
const button = wrapper.find('#setHealthData');
115+
button.simulate('click');
116+
expect(wrapper.find('#healthData').text()).toEqual(JSON.stringify({ foo: 'bar' }));
117+
});
3118
});
Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { mount, shallow } from 'enzyme';
2+
import { mount } from 'enzyme';
33

44
import AddModal from '../../../app/modals/AddModal';
55
import { DashboardContext } from '../../../app/context/DashboardContext';
@@ -16,23 +16,44 @@ describe('<AddModal />', () => {
1616
</DashboardContext.Provider>
1717
);
1818

19-
wrapper = shallow(<AddModal setOpen={setOpen}/>);
19+
wrapper = mount(mockAddModal);
2020
});
2121

2222
it('should render a <form> with an onSubmit property', () => {
23-
// const mockOnSubmit = jest.fn();
24-
// console.log(wrapper.debug());
25-
// const thing = wrapper.find('.add-container form');
26-
// console.log('thing ==>>', thing);
23+
const formTag = wrapper.find('.add-container form');
24+
expect(formTag).toBeDefined()
25+
expect(typeof formTag.prop('onSubmit')).toEqual('function')
2726
});
2827

29-
// it('should render a <select> with an onChange property');
28+
it('should render a <select> with an onChange property', () => {
29+
const selectTag = wrapper.find('#db-type')
30+
expect(selectTag).toBeDefined()
31+
expect(typeof selectTag.prop('onChange')).toEqual('function')
32+
});
3033

31-
// it('should render a <input> for db types with an onChange property');
34+
it('should render a <input> for db URI with an onChange property', () => {
35+
const inputTag = wrapper.find('#db-uri')
36+
expect(inputTag).toBeDefined()
37+
expect(typeof inputTag.prop('onChange')).toEqual('function')
38+
});
3239

33-
// it('should render a <input> for db URI with an onChange property');
40+
it('should render a <input> for db name with an onChange property', () => {
41+
const inputTag = wrapper.find('#db-name')
42+
expect(inputTag).toBeDefined()
43+
expect(typeof inputTag.prop('onChange')).toEqual('function')
44+
});
45+
46+
it("should render a <button> with a label 'Submit'", () => {
47+
const buttonTag = wrapper.find('form button')
48+
expect(buttonTag).toBeDefined()
49+
expect(buttonTag.text()).toEqual('Submit')
50+
});
3451

35-
// it('should render a <input> for db name with an onChange property');
52+
xit('should invoke onSubmit when the form button is clicked', () => {
53+
// const buttonTag = wrapper.find('form button')
54+
// const onSubmit = wrapper.find('.add-container form').prop('onSubmit')
55+
// const formTag = wrapper.find('.add-container form')
56+
})
3657

37-
// it("should render a <button> with a label 'Submit'");
58+
xit('should invoke setOpen when the submit button is clicked', () => {})
3859
});

0 commit comments

Comments
 (0)