diff --git a/hardware-concurrency/hardware-concurrency.test.js b/hardware-concurrency/hardware-concurrency.test.js index 8b9f542..1dede29 100644 --- a/hardware-concurrency/hardware-concurrency.test.js +++ b/hardware-concurrency/hardware-concurrency.test.js @@ -16,10 +16,14 @@ import { renderHook } from '@testing-library/react-hooks'; -import { useHardwareConcurrency } from './'; +afterEach(function() { + // Reload hook for every test + jest.resetModules(); +}); describe('useHardwareConcurrency', () => { test(`should return window.navigator.hardwareConcurrency`, () => { + const { useHardwareConcurrency } = require('./'); const { result } = renderHook(() => useHardwareConcurrency()); expect(result.current.numberOfLogicalProcessors).toBe(window.navigator.hardwareConcurrency); }); @@ -30,6 +34,7 @@ describe('useHardwareConcurrency', () => { configurable: true, writable: true }); + const { useHardwareConcurrency } = require('./'); const { result } = renderHook(() => useHardwareConcurrency()); expect(result.current.numberOfLogicalProcessors).toEqual(4); @@ -41,6 +46,7 @@ describe('useHardwareConcurrency', () => { configurable: true, writable: true }); + const { useHardwareConcurrency } = require('./'); const { result } = renderHook(() => useHardwareConcurrency()); expect(result.current.numberOfLogicalProcessors).toEqual(2); diff --git a/hardware-concurrency/index.js b/hardware-concurrency/index.js index c75962a..15d2dff 100644 --- a/hardware-concurrency/index.js +++ b/hardware-concurrency/index.js @@ -14,19 +14,14 @@ * limitations under the License. */ -import { useState } from 'react'; - +let initialHardwareConcurrency; +if (typeof navigator !== 'undefined' && 'hardwareConcurrency' in navigator) { + initialHardwareConcurrency = { numberOfLogicalProcessors: navigator.hardwareConcurrency }; +} else { + initialHardwareConcurrency = { unsupported: true }; +} const useHardwareConcurrency = () => { - let initialHardwareConcurrency; - if ('hardwareConcurrency' in navigator) { - initialHardwareConcurrency = {numberOfLogicalProcessors: navigator.hardwareConcurrency}; - } else { - initialHardwareConcurrency = {unsupported: true}; - } - - const [hardwareConcurrency] = useState(initialHardwareConcurrency); - - return { ...hardwareConcurrency }; + return { ...initialHardwareConcurrency }; }; export { useHardwareConcurrency }; diff --git a/memory/index.js b/memory/index.js index daf67fb..d45a946 100644 --- a/memory/index.js +++ b/memory/index.js @@ -14,33 +14,27 @@ * limitations under the License. */ -import { useState } from 'react'; - let unsupported; +if (typeof navigator !== 'undefined' && 'deviceMemory' in navigator) { + unsupported = false; +} else { + unsupported = true; +} +let initialMemoryStatus; +if (!unsupported) { + const performanceMemory = 'memory' in performance ? performance.memory : null; + initialMemoryStatus = { + deviceMemory: navigator.deviceMemory, + totalJSHeapSize: performanceMemory ? performanceMemory.totalJSHeapSize : null, + usedJSHeapSize: performanceMemory ? performanceMemory.usedJSHeapSize : null, + jsHeapSizeLimit: performanceMemory ? performanceMemory.jsHeapSizeLimit : null + }; +} else { + initialMemoryStatus = { unsupported }; +} const useMemoryStatus = () => { - if ('deviceMemory' in navigator) { - unsupported = false; - } else { - unsupported = true; - } - - let initialMemoryStatus; - if (!unsupported) { - const performanceMemory = ('memory' in performance) ? performance.memory : null; - initialMemoryStatus = { - deviceMemory: navigator.deviceMemory, - totalJSHeapSize: performanceMemory ? performanceMemory.totalJSHeapSize : null, - usedJSHeapSize: performanceMemory ? performanceMemory.usedJSHeapSize : null, - jsHeapSizeLimit: performanceMemory ? performanceMemory.jsHeapSizeLimit : null - }; - } else { - initialMemoryStatus = {unsupported}; - } - - const [memoryStatus] = useState(initialMemoryStatus); - - return { ...memoryStatus }; + return { ...initialMemoryStatus }; }; export { useMemoryStatus }; diff --git a/memory/memory.test.js b/memory/memory.test.js index 155c79a..febfaae 100644 --- a/memory/memory.test.js +++ b/memory/memory.test.js @@ -16,7 +16,10 @@ import { renderHook } from '@testing-library/react-hooks'; -import { useMemoryStatus } from './'; +afterEach(function() { + // Reload hook for every test + jest.resetModules(); +}); const getMemoryStatus = currentResult => ({ deviceMemory: currentResult.deviceMemory, @@ -27,6 +30,7 @@ const getMemoryStatus = currentResult => ({ describe('useMemoryStatus', () => { test(`should return "true" for unsupported case`, () => { + const { useMemoryStatus } = require('./'); const { result } = renderHook(() => useMemoryStatus()); expect(result.current.unsupported).toBe(true); @@ -48,6 +52,7 @@ describe('useMemoryStatus', () => { jsHeapSizeLimit: mockMemoryStatus.jsHeapSizeLimit }; + const { useMemoryStatus } = require('./'); const { result } = renderHook(() => useMemoryStatus()); expect(getMemoryStatus(result.current)).toEqual(mockMemoryStatus); diff --git a/save-data/index.js b/save-data/index.js index dc2cd26..bdc63dd 100644 --- a/save-data/index.js +++ b/save-data/index.js @@ -14,20 +14,16 @@ * limitations under the License. */ -import { useState } from 'react'; - let unsupported; +if ('connection' in navigator && 'saveData' in navigator.connection) { + unsupported = false; +} else { + unsupported = true; +} -const useSaveData = () => { - if ('connection' in navigator && 'saveData' in navigator.connection) { - unsupported = false; - } else { - unsupported = true; - } - - const initialSaveData = unsupported ? null : navigator.connection.saveData === true; - const [saveData] = useState(initialSaveData); +const saveData = unsupported ? null : navigator.connection.saveData === true; +const useSaveData = () => { return { unsupported, saveData }; }; diff --git a/save-data/save-data.test.js b/save-data/save-data.test.js index 13aa632..541cbb7 100644 --- a/save-data/save-data.test.js +++ b/save-data/save-data.test.js @@ -1,4 +1,3 @@ - /* * Copyright 2019 Google LLC * @@ -16,10 +15,15 @@ */ import { renderHook, act } from '@testing-library/react-hooks'; -import { useSaveData } from './'; + +afterEach(function() { + // Reload hook for every test + jest.resetModules(); +}); describe('useSaveData', () => { test(`should return "true" for unsupported case`, () => { + const { useSaveData } = require('./'); const { result } = renderHook(() => useSaveData()); expect(result.current.unsupported).toBe(true); }); @@ -28,6 +32,7 @@ describe('useSaveData', () => { global.navigator.connection = { saveData: true }; + const { useSaveData } = require('./'); const { result } = renderHook(() => useSaveData()); expect(result.current.saveData).toEqual(navigator.connection.saveData); @@ -37,6 +42,7 @@ describe('useSaveData', () => { global.navigator.connection = { saveData: false }; + const { useSaveData } = require('./'); const { result } = renderHook(() => useSaveData()); expect(result.current.saveData).toEqual(navigator.connection.saveData);