Skip to content

Commit 3c9f95a

Browse files
[core] Bugfix/consistent naming (#53)
* doc: improved code styling in readme * infra: uninstalled react-dom * chore: cleaned up * infra: extracted supported from hook in useMediaCapabilities * doc: cleaned code snippet for useMediaCapabilities in README * [chore] updated initialStatus argument naming in hooks
1 parent 283fdd4 commit 3c9f95a

File tree

4 files changed

+113
-113
lines changed

4 files changed

+113
-113
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ const MyComponent = () => {
100100

101101
`saveData` values can be `true` or `false`.
102102

103-
This hook accepts an optional `initialSaveDataStatus` boolean argument, which can be used to provide a `saveData` state value when the user's browser does not support the relevant [NetworkInformation API](https://wicg.github.io/netinfo/). Passing an initial value can also prove useful for server-side rendering, where the developer can pass a server [Save-Data Client Hint](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/client-hints#save-data) that has been converted to a boolean to detect the user's data saving preference.
103+
This hook accepts an optional `initialSaveData` boolean argument, which can be used to provide a `saveData` state value when the user's browser does not support the relevant [NetworkInformation API](https://wicg.github.io/netinfo/). Passing an initial value can also prove useful for server-side rendering, where the developer can pass a server [Save-Data Client Hint](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/client-hints#save-data) that has been converted to a boolean to detect the user's data saving preference.
104104

105105
```js
106106
// Inside of a functional React component
107-
const initialSaveDataStatus = true;
108-
const { saveData } = useSaveData(initialSaveDataStatus);
107+
const initialSaveData = true;
108+
const { saveData } = useSaveData(initialSaveData);
109109
```
110110

111111
### CPU Cores / Hardware Concurrency

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
"react",
3535
"performance"
3636
]
37-
}
37+
}

save-data/index.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
/*
2-
* Copyright 2019 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
let unsupported;
18-
19-
const useSaveData = (initialSaveDataStatus = null) => {
20-
if (typeof navigator !== 'undefined' && 'connection' in navigator && 'saveData' in navigator.connection) {
21-
unsupported = false;
22-
} else {
23-
unsupported = true;
24-
}
25-
26-
return {
27-
unsupported,
28-
saveData: unsupported
29-
? initialSaveDataStatus
30-
: navigator.connection.saveData === true
31-
};
32-
};
33-
34-
export { useSaveData };
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
let unsupported;
18+
19+
const useSaveData = (initialSaveData = null) => {
20+
if (typeof navigator !== 'undefined' && 'connection' in navigator && 'saveData' in navigator.connection) {
21+
unsupported = false;
22+
} else {
23+
unsupported = true;
24+
}
25+
26+
return {
27+
unsupported,
28+
saveData: unsupported
29+
? initialSaveData
30+
: navigator.connection.saveData === true
31+
};
32+
};
33+
34+
export { useSaveData };

save-data/save-data.test.js

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
1-
/*
2-
* Copyright 2019 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the 'License');
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an 'AS IS' BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
import { renderHook, act } from '@testing-library/react-hooks';
18-
19-
afterEach(function() {
20-
// Reload hook for every test
21-
jest.resetModules();
22-
});
23-
24-
describe('useSaveData', () => {
25-
test(`should return "true" for unsupported case`, () => {
26-
const { useSaveData } = require('./');
27-
const { result } = renderHook(() => useSaveData());
28-
29-
expect(result.current.unsupported).toBe(true);
30-
expect(result.current.saveData).toEqual(null);
31-
});
32-
33-
test('should return initialSaveDataStatus for unsupported case', () => {
34-
const initialSaveDataStatus = true;
35-
const { useSaveData } = require('./');
36-
const { result } = renderHook(() => useSaveData(initialSaveDataStatus));
37-
38-
expect(result.current.unsupported).toBe(true);
39-
expect(result.current.saveData).toBe(initialSaveDataStatus);
40-
});
41-
42-
test(`should return "true" for enabled save data`, () => {
43-
global.navigator.connection = {
44-
saveData: true
45-
};
46-
const { useSaveData } = require('./');
47-
const { result } = renderHook(() => useSaveData());
48-
49-
expect(result.current.unsupported).toBe(false);
50-
expect(result.current.saveData).toEqual(navigator.connection.saveData);
51-
});
52-
53-
test(`should return "false" for disabled save data`, () => {
54-
global.navigator.connection = {
55-
saveData: false
56-
};
57-
const { useSaveData } = require('./');
58-
const { result } = renderHook(() => useSaveData());
59-
60-
expect(result.current.unsupported).toBe(false);
61-
expect(result.current.saveData).toEqual(navigator.connection.saveData);
62-
});
63-
64-
test('should not return initialSaveDataStatus for supported case', () => {
65-
const initialSaveDataStatus = false;
66-
global.navigator.connection = {
67-
saveData: true
68-
};
69-
const { useSaveData } = require('./');
70-
const { result } = renderHook(() => useSaveData(initialSaveDataStatus));
71-
72-
expect(result.current.unsupported).toBe(false);
73-
expect(result.current.saveData).toEqual(navigator.connection.saveData);
74-
});
75-
});
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the 'License');
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an 'AS IS' BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { renderHook, act } from '@testing-library/react-hooks';
18+
19+
afterEach(function() {
20+
// Reload hook for every test
21+
jest.resetModules();
22+
});
23+
24+
describe('useSaveData', () => {
25+
test(`should return "true" for unsupported case`, () => {
26+
const { useSaveData } = require('./');
27+
const { result } = renderHook(() => useSaveData());
28+
29+
expect(result.current.unsupported).toBe(true);
30+
expect(result.current.saveData).toEqual(null);
31+
});
32+
33+
test('should return initialSaveData for unsupported case', () => {
34+
const initialSaveData = true;
35+
const { useSaveData } = require('./');
36+
const { result } = renderHook(() => useSaveData(initialSaveData));
37+
38+
expect(result.current.unsupported).toBe(true);
39+
expect(result.current.saveData).toBe(initialSaveData);
40+
});
41+
42+
test(`should return "true" for enabled save data`, () => {
43+
global.navigator.connection = {
44+
saveData: true
45+
};
46+
const { useSaveData } = require('./');
47+
const { result } = renderHook(() => useSaveData());
48+
49+
expect(result.current.unsupported).toBe(false);
50+
expect(result.current.saveData).toEqual(navigator.connection.saveData);
51+
});
52+
53+
test(`should return "false" for disabled save data`, () => {
54+
global.navigator.connection = {
55+
saveData: false
56+
};
57+
const { useSaveData } = require('./');
58+
const { result } = renderHook(() => useSaveData());
59+
60+
expect(result.current.unsupported).toBe(false);
61+
expect(result.current.saveData).toEqual(navigator.connection.saveData);
62+
});
63+
64+
test('should not return initialSaveData for supported case', () => {
65+
const initialSaveData = false;
66+
global.navigator.connection = {
67+
saveData: true
68+
};
69+
const { useSaveData } = require('./');
70+
const { result } = renderHook(() => useSaveData(initialSaveData));
71+
72+
expect(result.current.unsupported).toBe(false);
73+
expect(result.current.saveData).toEqual(navigator.connection.saveData);
74+
});
75+
});

0 commit comments

Comments
 (0)