Skip to content
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
7 changes: 1 addition & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ name: CI
jobs:
build:
name: Lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['16.x']
os: [ubuntu-latest, windows-latest, macOS-latest]
runs-on: ubuntu-latest

steps:
- name: Checkout repo
Expand Down
192 changes: 95 additions & 97 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,99 +1,97 @@
{
"name": "react-screen-wake-lock",
"version": "0.0.0-development",
"description": "React implementation of the Screen Wake Lock API. It provides a way to prevent devices from dimming or locking the screen when an application needs to keep running.",
"author": {
"name": "Joris",
"email": "[email protected]",
"url": "https://joris.re"
},
"license": "MIT",
"homepage": "https://github.com/jorisre/react-screen-wake-lock#readme",
"repository": {
"type": "git",
"url": "https://github.com/jorisre/react-screen-wake-lock.git"
},
"bugs": {
"url": "https://github.com/jorisre/react-screen-wake-lock/issues"
},
"keywords": [
"wakeLock",
"wake-lock",
"react",
"hook",
"Screen Wake Lock",
"navigator.wakeLock"
],
"sideEffects": false,
"type": "module",
"source": "src/index.ts",
"main": "dist/react-screen-wake-lock.cjs",
"nodule": "./dist/react-screen-wake-lock.esm.js",
"exports": {
"types": "./dist/index.d.ts",
"require": "./dist/react-screen-wake-lock.cjs",
"default": "./dist/react-screen-wake-lock.esm.js"
},
"umd:main": "dist/react-screen-wake-lock.umd.js",
"unpkg": "dist/react-screen-wake-lock.umd.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"engines": {
"node": ">=v14.21.3"
},
"scripts": {
"prepare": "husky install",
"prepack": "pinst --disable",
"postpack": "pinst --enable",
"start": "microbundle watch",
"prebuild": "rimraf dist",
"build": "npm-run-all --parallel build:*",
"build:other": "microbundle --define process.env.NODE_ENV=production -f cjs,umd",
"build:es": "microbundle -f es",
"test": "jest",
"lint": "eslint --ignore-path .gitignore --ext .ts,.tsx ."
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"devDependencies": {
"@testing-library/dom": "^9.3.1",
"@testing-library/react": "^14.0.0",
"@testing-library/react-hooks": "^8.0.1",
"@types/dom-screen-wake-lock": "^1.0.1",
"@types/jest": "^29.5.4",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint": "^8.49.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-testing-library": "^6.0.1",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-wake-lock-mock": "^1.1.0",
"lodash": "^4.17.21",
"microbundle": "^0.15.1",
"nano-staged": "^0.8.0",
"npm-run-all": "^4.1.5",
"pinst": "^3.0.0",
"prettier": "^3.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-test-renderer": "18.2.0",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"nano-staged": {
"*.{ts,tsx}": "eslint --fix",
"*.{js,css,md}": "prettier --write"
},
"dependencies": {}
"name": "react-screen-wake-lock",
"version": "0.0.0-development",
"description": "React implementation of the Screen Wake Lock API. It provides a way to prevent devices from dimming or locking the screen when an application needs to keep running.",
"author": {
"name": "Joris",
"email": "[email protected]",
"url": "https://joris.re"
},
"license": "MIT",
"homepage": "https://github.com/jorisre/react-screen-wake-lock#readme",
"repository": {
"type": "git",
"url": "https://github.com/jorisre/react-screen-wake-lock.git"
},
"bugs": {
"url": "https://github.com/jorisre/react-screen-wake-lock/issues"
},
"keywords": [
"wakeLock",
"wake-lock",
"react",
"hook",
"Screen Wake Lock",
"navigator.wakeLock"
],
"sideEffects": false,
"type": "module",
"source": "src/index.ts",
"main": "dist/react-screen-wake-lock.cjs",
"nodule": "./dist/react-screen-wake-lock.esm.js",
"exports": {
"types": "./dist/index.d.ts",
"require": "./dist/react-screen-wake-lock.cjs",
"default": "./dist/react-screen-wake-lock.esm.js"
},
"umd:main": "dist/react-screen-wake-lock.umd.js",
"unpkg": "dist/react-screen-wake-lock.umd.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"engines": {
"node": ">=v14.21.3"
},
"scripts": {
"prepare": "husky install",
"prepack": "pinst --disable",
"postpack": "pinst --enable",
"start": "microbundle watch",
"prebuild": "rimraf dist",
"build": "npm-run-all --parallel build:*",
"build:other": "microbundle --define process.env.NODE_ENV=production -f cjs,umd",
"build:es": "microbundle -f es",
"test": "jest",
"lint": "eslint --ignore-path .gitignore --ext .ts,.tsx ."
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
},
"devDependencies": {
"@testing-library/dom": "^10.4.0",
"@testing-library/react": "^16.2.0",
"@types/dom-screen-wake-lock": "^1.0.3",
"@types/jest": "^29.5.4",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint": "^8.49.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-testing-library": "^6.0.1",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"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": "^3.0.3",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-test-renderer": "18.2.0",
"rimraf": "^5.0.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"nano-staged": {
"*.{ts,tsx}": "eslint --fix",
"*.{js,css,md}": "prettier --write"
},
"dependencies": {}
}
64 changes: 30 additions & 34 deletions test/use-wake-lock.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { act, renderHook } from '@testing-library/react-hooks';
import { useWakeLock, WakeLockOptions } from '../src';
import { renderHook, act } from '@testing-library/react';
import { useWakeLock } from '../src';

const noop = () => {};

Expand Down Expand Up @@ -31,8 +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(spyedConsoleWarn).toBeCalledWith(
"[react-screen-wake-lock]: Calling the `request` function has no effect, Wake Lock Screen API isn't supported"
expect(spyedConsoleWarn).toHaveBeenCalledWith(
"[react-screen-wake-lock]: Calling the `request` function has no effect, Wake Lock Screen API isn't supported",
);

await act(async () => {
Expand All @@ -41,8 +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(spyedConsoleWarn).toBeCalledWith(
"[react-screen-wake-lock]: Calling the `release` function has no effect, Wake Lock Screen API isn't supported"
expect(spyedConsoleWarn).toHaveBeenCalledWith(
"[react-screen-wake-lock]: Calling the `release` function has no effect, Wake Lock Screen API isn't supported",
);
});

Expand Down Expand Up @@ -72,20 +72,16 @@ test('useWakeLock handles request then release with success', async () => {
});

test('useWakeLock handles request and throw an error', async () => {
const requestError = new Error(
'An error occured during `navigator.wakeLock.request` 💥'
);
const requestError = new Error('An error occured during `navigator.wakeLock.request` 💥');
const handleError = jest.fn();
jest
.spyOn(window.navigator.wakeLock, 'request')
.mockRejectedValueOnce(requestError);

const { result } = renderHook<
WakeLockOptions,
ReturnType<typeof useWakeLock>
>((props) => useWakeLock(props), {
initialProps: { onError: handleError },
});
jest.spyOn(window.navigator.wakeLock, 'request').mockRejectedValueOnce(requestError);

const { result } = renderHook(
(props) => useWakeLock(props),
{
initialProps: { onError: handleError },
},
);

// Handle wakeLock request
await act(async () => {
Expand All @@ -109,7 +105,7 @@ test('in development|test mode, a warning is displayed when calling `release` be
});

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

Expand All @@ -124,18 +120,18 @@ test('once WakeLock released and in development|test mode, a warning is displaye
});

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

test('useWakeLock should call `onRequest` when request done with success', async () => {
const handleRequest = jest.fn();
const { result } = renderHook<
WakeLockOptions,
ReturnType<typeof useWakeLock>
>((props) => useWakeLock(props), {
initialProps: { onRequest: handleRequest },
});
const { result } = renderHook(
(props) => useWakeLock(props),
{
initialProps: { onRequest: handleRequest },
},
);

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

Expand All @@ -152,19 +148,19 @@ test('in development|test show a warning if `request` is called more than once w
});

expect(spyedConsoleWarn).toHaveBeenCalledWith(
'[react-screen-wake-lock]: Calling `request` multiple times without `release` has no effect'
'[react-screen-wake-lock]: Calling `request` multiple times without `release` has no effect',
);
expect(window.navigator.wakeLock.request).toHaveBeenCalledTimes(1);
});

test('useWakeLock should call `onRelease` when wakeLockSentinel is released', async () => {
const handleRelease = jest.fn();
const { result } = renderHook<
WakeLockOptions,
ReturnType<typeof useWakeLock>
>((props) => useWakeLock(props), {
initialProps: { onRelease: handleRelease },
});
const { result } = renderHook(
(props) => useWakeLock(props),
{
initialProps: { onRelease: handleRelease },
},
);

expect(handleRelease).not.toHaveBeenCalled();

Expand Down
Loading
Loading