Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move constant data out of hooks #1

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 7 additions & 12 deletions hardware-concurrency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
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