-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b292a7e
commit 420ff29
Showing
10 changed files
with
284 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
Oops, something went wrong.