Skip to content

Commit

Permalink
fix: dependency free
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisre authored Oct 2, 2022
2 parents b7f5483 + 48c1540 commit 7685bfb
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 712 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ Tiniest React implementation of the Screen Wake Lock API. <br/>It provides a way
## Features

- 🌐 Follows the **[W3C Screen Wake Lock API specifications](https://w3c.github.io/screen-wake-lock/)**
- 🎣 **Easy to use** - just one react hook `useWakeLock`
- 🪶 **Lightweight** - _less than **[370b](https://bundlephobia.com/result?p=react-screen-wake-lock)**_
- 🔌 **Easily integration** - _it works without additional configuration (React, remix, Next.js...)_
- 🪝 **Easy to use** - Just one react hook `useWakeLock`
- 🪶 **Lightweight & 0 Dependency** - _Less than **[650b](https://bundlephobia.com/result?p=react-screen-wake-lock)**_
- 🔌 **Easily integration** - _It works without additional configuration (React, remix, Next.js...)_
- 🧪 **Ready to test** - Mocks the Screen Wake Lock with [Jest](https://github.com/jorisre/jest-wake-lock-mock#readme)
- ⚠️ **Browser Support** - [Screen Wake Lock API](https://caniuse.com/wake-lock)

<details>
Expand Down
4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"serve": "vite preview"
},
"dependencies": {
"react-screen-wake-lock": "1.1.5"
"react-screen-wake-lock": "^3.0.0"
},
"devDependencies": {
"@types/react": "^18.0.13",
"@types/react-dom": "^18.0.5",
"@vitejs/plugin-react-refresh": "^1.3.1",
"typescript": "^4.1.2",
"vite": "^2.1.3"
"vite": "^3.1.4"
},
"alias": {
"react": "../node_modules/react",
Expand Down
508 changes: 0 additions & 508 deletions example/yarn.lock

This file was deleted.

19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"node": ">=14.16"
},
"scripts": {
"postinstall": "yarn husky install",
"postinstall": "husky install",
"prepack": "pinst --disable",
"postpack": "pinst --enable",
"start": "microbundle watch",
"prebuild": "rimraf dist",
"build": "npm-run-all --parallel build:*",
Expand All @@ -53,9 +55,9 @@
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"devDependencies": {
"@testing-library/dom": "^7.30.1",
"@testing-library/react": "^11.2.5",
"@testing-library/react-hooks": "^5.1.0",
"@testing-library/dom": "^8.18.1",
"@testing-library/react": "^13.4.0",
"@testing-library/react-hooks": "^8.0.1",
"@types/dom-screen-wake-lock": "^1.0.0",
"@types/jest": "^29.1.1",
"@types/react": "^18.0.13",
Expand All @@ -65,19 +67,20 @@
"eslint": "^8.24.0",
"eslint-plugin-react": "^7.23.1",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-testing-library": "^5.7.0",
"eslint-plugin-testing-library": "^5.7.2",
"husky": "^8.0.1",
"jest": "^29.1.2",
"jest-environment-jsdom": "^29.1.2",
"jest-wake-lock-mock": "^1.1.0",
"microbundle": "^0.15.1",
"nano-staged": "^0.8.0",
"npm-run-all": "^4.1.5",
"pinst": "^3.0.0",
"prettier": "^2.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-test-renderer": "18.2.0",
"rimraf": "^3.0.2",
"tiny-warning": "^1.0.3",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.2.3"
Expand All @@ -86,7 +89,5 @@
"*.{ts,tsx}": "eslint --fix",
"*.{js,css,md}": "prettier --write"
},
"dependencies": {
"husky": "^8.0.1"
}
"dependencies": {}
}
28 changes: 13 additions & 15 deletions src/use-wake-lock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as React from 'react';
import warning from 'tiny-warning';

const warn = (content: string) =>
console.warn('[react-screen-wake-lock]: ' + content);

export interface WakeLockOptions {
onError?: (error: Error) => void;
Expand All @@ -21,16 +23,15 @@ export const useWakeLock = ({
const request = React.useCallback(
async (type: WakeLockType = 'screen') => {
const isWakeLockAlreadyDefined = wakeLock.current != null;
if (!isSupported || isWakeLockAlreadyDefined) {
warning(
!isSupported,
if (!isSupported) {
return warn(
"Calling the `request` function has no effect, Wake Lock Screen API isn't supported"
);
warning(
isWakeLockAlreadyDefined,
}
if (isWakeLockAlreadyDefined) {
return warn(
'Calling `request` multiple times without `release` has no effect'
);
return;
}

try {
Expand All @@ -54,17 +55,14 @@ export const useWakeLock = ({

const release = React.useCallback(async () => {
const isWakeLockUndefined = wakeLock.current == null;
if (!isSupported || isWakeLockUndefined) {
warning(
!isSupported,
if (!isSupported) {
return warn(
"Calling the `release` function has no effect, Wake Lock Screen API isn't supported"
);
}

warning(
isWakeLockUndefined,
'Calling `release` before `request` has no effect.'
);
return;
if (isWakeLockUndefined) {
return warn('Calling `release` before `request` has no effect.');
}

wakeLock.current && (await wakeLock.current.release());
Expand Down
39 changes: 19 additions & 20 deletions test/use-wake-lock.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { act, renderHook } from '@testing-library/react-hooks';
import warning from 'tiny-warning';
import { useWakeLock, WakeLockOptions } from '../src';

jest.mock('tiny-warning');
const noop = () => {};

test("useWakeLock returns `isSupported: false` if Screen Wake Lock API isn't supported", async () => {
const { wakeLock, ...navigator } = window.navigator;
Expand All @@ -18,6 +17,7 @@ test('in development|test mode there are warnings displayed if `request` or `rel
const { wakeLock, ...navigator } = window.navigator;
//@ts-expect-error
jest.spyOn(window, 'navigator', 'get').mockReturnValue(navigator);
const spyedConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(noop);

const { result } = renderHook(() => useWakeLock());

Expand All @@ -31,9 +31,8 @@ test('in development|test mode there are warnings displayed if `request` or `rel

expect(result.current.released).not.toBeDefined();
expect(result.current.type).not.toBeDefined();
expect(warning).toBeCalledWith(
true,
"Calling the `request` function has no effect, Wake Lock Screen API isn't supported"
expect(spyedConsoleWarn).toBeCalledWith(
"[react-screen-wake-lock]: Calling the `request` function has no effect, Wake Lock Screen API isn't supported"
);

await act(async () => {
Expand All @@ -42,9 +41,8 @@ test('in development|test mode there are warnings displayed if `request` or `rel

expect(result.current.released).not.toBeDefined();
expect(result.current.type).not.toBeDefined();
expect(warning).toBeCalledWith(
true,
"Calling the `release` function has no effect, Wake Lock Screen API isn't supported"
expect(spyedConsoleWarn).toBeCalledWith(
"[react-screen-wake-lock]: Calling the `release` function has no effect, Wake Lock Screen API isn't supported"
);
});

Expand Down Expand Up @@ -85,7 +83,7 @@ test('useWakeLock handles request and throw an error', async () => {
const { result } = renderHook<
WakeLockOptions,
ReturnType<typeof useWakeLock>
>(props => useWakeLock(props), {
>((props) => useWakeLock(props), {
initialProps: { onError: handleError },
});

Expand All @@ -102,19 +100,21 @@ test('useWakeLock handles request and throw an error', async () => {
});

test('in development|test mode, a warning is displayed when calling `release` before `request`', async () => {
const spyedConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(noop);

const { result } = renderHook(() => useWakeLock());

await act(async () => {
await result.current.release();
});

expect(warning).toHaveBeenCalledWith(
true,
'Calling `release` before `request` has no effect.'
expect(spyedConsoleWarn).toHaveBeenCalledWith(
'[react-screen-wake-lock]: Calling `release` before `request` has no effect.'
);
});

test('once WakeLock released and in development|test mode, a warning is displayed when calling `release` before `request`', async () => {
const spyedConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(noop);
const { result } = renderHook(() => useWakeLock());

await act(async () => {
Expand All @@ -123,9 +123,8 @@ test('once WakeLock released and in development|test mode, a warning is displaye
await result.current.release();
});

expect(warning).toHaveBeenCalledWith(
true,
'Calling `release` before `request` has no effect.'
expect(spyedConsoleWarn).toHaveBeenCalledWith(
'[react-screen-wake-lock]: Calling `release` before `request` has no effect.'
);
});

Expand All @@ -134,7 +133,7 @@ test('useWakeLock should call `onRequest` when request done with success', async
const { result } = renderHook<
WakeLockOptions,
ReturnType<typeof useWakeLock>
>(props => useWakeLock(props), {
>((props) => useWakeLock(props), {
initialProps: { onRequest: handleRequest },
});

Expand All @@ -144,16 +143,16 @@ test('useWakeLock should call `onRequest` when request done with success', async
});

test('in development|test show a warning if `request` is called more than once without `release`', async () => {
const spyedConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(noop);
const { result } = renderHook(() => useWakeLock());

await act(async () => {
await result.current.request();
await result.current.request();
});

expect(warning).toHaveBeenCalledWith(
true,
'Calling `request` multiple times without `release` has no effect'
expect(spyedConsoleWarn).toHaveBeenCalledWith(
'[react-screen-wake-lock]: Calling `request` multiple times without `release` has no effect'
);
expect(window.navigator.wakeLock.request).toHaveBeenCalledTimes(1);
});
Expand All @@ -163,7 +162,7 @@ test('useWakeLock should call `onRelease` when wakeLockSentinel is released', as
const { result } = renderHook<
WakeLockOptions,
ReturnType<typeof useWakeLock>
>(props => useWakeLock(props), {
>((props) => useWakeLock(props), {
initialProps: { onRelease: handleRelease },
});

Expand Down
Loading

0 comments on commit 7685bfb

Please sign in to comment.