Skip to content

Commit

Permalink
wrote Unit Test for hooks directory
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-karlovskiy committed Aug 15, 2019
1 parent b292a7e commit 420ff29
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 95 deletions.
78 changes: 78 additions & 0 deletions hooks/battery/battery.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'babel-polyfill';
import { renderHook, act } from '@testing-library/react-hooks';
import { useBatteryStatus } from './';

// TODO: addEventListener change trigger mocking

describe('useBatteryStatus', () => {
const unsupportMessage = require('./').unsupportMessage;
test(`should return ${unsupportMessage}`, () => {
const { result } = renderHook(() => useBatteryStatus());

expect(result.current.batteryStatus.unsupportMessage).toBe(unsupportMessage);
});

test('should update the batteryStatus state', () => {
const { result } = renderHook(() => useBatteryStatus());

act(() => result.current.updateBatteryStatus({
chargingTime: 20,
dischargingTime: 40,
charging: true,
level: 50
}));

expect(result.current.batteryStatus).toEqual({
chargingTime: '20 Seconds',
dichargeTime: '40 Seconds',
level: 50,
chargingState: 'Charging'
});
});

test('should return mockBattery status', async () => {
const originalError = console.error
console.error = jest.fn()

const mockBattery = jest.fn().mockImplementation(() => Promise.resolve({
chargingTime: 20,
dischargingTime: 40,
level: 50,
charging: true,
addEventListener: jest.fn()
}));

global.navigator.getBattery = mockBattery;

try {
const { result, waitForNextUpdate } = renderHook(() => useBatteryStatus());

await waitForNextUpdate();

expect(result.current.batteryStatus).toEqual({
chargingTime: '20 Seconds',
dichargeTime: '40 Seconds',
level: 50,
chargingState: 'Charging'
});
} finally {
console.error = originalError;
}
});
});
4 changes: 2 additions & 2 deletions hooks/battery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { useState, useEffect } from 'react';

const unsupportMessage = 'The Battery Status API is not supported on this platform.';
export const unsupportMessage = 'The Battery Status API is not supported on this platform.';

const useBatteryStatus = () => {
const [batteryStatus, setBatteryStatus] = useState(null);
Expand Down Expand Up @@ -50,7 +50,7 @@ const useBatteryStatus = () => {
// eslint-disable-next-line
}, []);

return batteryStatus;
return { batteryStatus, updateBatteryStatus };
};

export { useBatteryStatus };
40 changes: 40 additions & 0 deletions hooks/device-class/device-class.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { renderHook } from '@testing-library/react-hooks';
import { useDeviceClass } from './';

describe('useDeviceClass', () => {
test('should return light for Nexus 6', () => {
navigator.__defineGetter__('userAgent', () => {
return 'Mozilla/5.0 (Linux; Android 5.0; Nexus 6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.136 Mobile Safari/537.36' // customized user agent
});

const { result } = renderHook(() => useDeviceClass());

expect(result.current).toEqual('light');
});

test('should return heavy for Galaxy 10', () => {
navigator.__defineGetter__('userAgent', () => {
return 'Mozilla/5.0 (Linux; Android 9; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.136 Mobile Safari/537.36' // customized user agent
});

const { result } = renderHook(() => useDeviceClass());

expect(result.current).toEqual('heavy');
});
});
6 changes: 3 additions & 3 deletions hooks/memory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

import { useState, useEffect } from 'react';

const unsupportMessage = 'The Memory Status API is not supported on this platform.';
export const unsupportMessage = 'The Memory Status API is not supported on this platform.';

// Tune these for your application
const MAX_MEMORY_LIMIT = 50 * 1048576; // 20MB
const MAX_PERCENT_THRESHOLD = 90;
export const MAX_MEMORY_LIMIT = 50 * 1048576; // 20MB
export const MAX_PERCENT_THRESHOLD = 90;

const useMemoryStatus = () => {
const windowPerformance = window.performance;
Expand Down
53 changes: 53 additions & 0 deletions hooks/memory/memory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { renderHook } from '@testing-library/react-hooks';
import { useMemoryStatus } from './';

describe('useMemoryStatus', () => {
const unsupportMessage = require('./').unsupportMessage;
test(`should return ${unsupportMessage}`, () => {
const { result } = renderHook(() => useMemoryStatus());

expect(result.current.unsupportMessage).toBe(unsupportMessage);
});

test('should return mockMemory status', () => {
const totalJSHeapSize = 60;
const usedJSHeapSize = 40;
const jsHeapSizeLimit = 50;
global.window.performance.memory = {
totalJSHeapSize,
usedJSHeapSize,
jsHeapSizeLimit
};

const { result } = renderHook(() => useMemoryStatus());

const MAX_MEMORY_LIMIT = require('./').MAX_MEMORY_LIMIT;
const MAX_PERCENT_THRESHOLD = require('./').MAX_PERCENT_THRESHOLD;
const overUsedMemorySize = usedJSHeapSize - MAX_MEMORY_LIMIT;
const usedMemoryPercent = usedJSHeapSize / jsHeapSizeLimit * 100;
const overLoad = overUsedMemorySize > 0 || usedMemoryPercent > MAX_PERCENT_THRESHOLD;

expect(result.current).toEqual({
totalJSHeapSize,
usedJSHeapSize,
jsHeapSizeLimit,
overLoad
});
});
});
5 changes: 2 additions & 3 deletions hooks/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ const useEffectiveConnectionType = () => {
const updateECTStatus = () => {
setEffectiveConnectionType(getEffectiveConnectionType());
};

useEffect(() => {
navigatorConnection && navigatorConnection.addEventListener('change', updateECTStatus);
return () => {
navigatorConnection && navigatorConnection.removeEventListener('change', updateECTStatus);
};
// eslint-disable-next-line
}, []);

return effectiveConnectionType;
return { effectiveConnectionType, updateECTStatus };
};

export { useEffectiveConnectionType };
48 changes: 48 additions & 0 deletions hooks/network/network.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { renderHook, act } from '@testing-library/react-hooks';
import { useEffectiveConnectionType } from './';

// TODO: addEventListener change trigger mocking

describe('useEffectiveConnectionType', () => {
test('should return 4g of effectiveConnectionType', () => {
global.navigator.connection = {
effectiveType: '4g',
addEventListener: jest.fn(),
removeEventListener: jest.fn()
};

const { result } = renderHook(() => useEffectiveConnectionType());

expect(result.current.effectiveConnectionType).toEqual('4g');
});

test('should update the effectiveConnectionType state', () => {
global.navigator.connection = {
effectiveType: '2g',
addEventListener: jest.fn(),
removeEventListener: jest.fn()
};

const { result } = renderHook(() => useEffectiveConnectionType());

act(() => result.current.updateECTStatus());

expect(result.current.effectiveConnectionType).toEqual('2g');
});
});
Loading

0 comments on commit 420ff29

Please sign in to comment.