|
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 | + }); |
3 | 118 | }); |
0 commit comments