Skip to content

Commit

Permalink
all: move constants out of hooks; guard navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Nov 12, 2019
1 parent b964f3e commit 37e3fdf
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 51 deletions.
8 changes: 7 additions & 1 deletion hardware-concurrency/hardware-concurrency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -30,6 +34,7 @@ describe('useHardwareConcurrency', () => {
configurable: true,
writable: true
});
const { useHardwareConcurrency } = require('./');
const { result } = renderHook(() => useHardwareConcurrency());

expect(result.current.numberOfLogicalProcessors).toEqual(4);
Expand All @@ -41,6 +46,7 @@ describe('useHardwareConcurrency', () => {
configurable: true,
writable: true
});
const { useHardwareConcurrency } = require('./');
const { result } = renderHook(() => useHardwareConcurrency());

expect(result.current.numberOfLogicalProcessors).toEqual(2);
Expand Down
20 changes: 8 additions & 12 deletions hardware-concurrency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@
* 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 };
}
console.log({ initialHardwareConcurrency });
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 };
42 changes: 18 additions & 24 deletions memory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
7 changes: 6 additions & 1 deletion memory/memory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -48,6 +52,7 @@ describe('useMemoryStatus', () => {
jsHeapSizeLimit: mockMemoryStatus.jsHeapSizeLimit
};

const { useMemoryStatus } = require('./');
const { result } = renderHook(() => useMemoryStatus());

expect(getMemoryStatus(result.current)).toEqual(mockMemoryStatus);
Expand Down
18 changes: 7 additions & 11 deletions save-data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

Expand Down
10 changes: 8 additions & 2 deletions save-data/save-data.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
* Copyright 2019 Google LLC
*
Expand All @@ -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);
});
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 37e3fdf

Please sign in to comment.