jest-wake-lock-mock
Version:
Mock Screen Wake Lock API `navigator.wakeLock` with ease and run your tests using Jest
91 lines (63 loc) ⢠2.85 kB
Markdown
<h1 align="center">Welcome to jest-wake-lock-mock š</h1>
<p>
<img alt="npm" src="https://img.shields.io/npm/v/jest-wake-lock-mock?style=for-the-badge">
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/workflow/status/jorisre/jest-wake-lock-mock/CI?style=for-the-badge">
<img alt="Codecov" src="https://img.shields.io/codecov/c/github/jorisre/jest-wake-lock-mock?style=for-the-badge&token=D75F3R5OEO">
<a href="https://github.com/jorisre/jest-wake-lock-mock/blob/master/LICENSE" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/github/license/jorisre/jest-wake-lock-mock?style=for-the-badge" />
</a>
<a href="https://twitter.com/_jorisre" target="_blank">
<img alt="Twitter: _jorisre" src="https://img.shields.io/twitter/follow/_jorisre.svg?style=for-the-badge" />
</a>
</p>
> Mock [Screen Wake Lock API](https://w3c.github.io/screen-wake-lock/) _(`navigator.wakeLock`)_ with ease and run your tests using Jest
- node >=10
```sh
npm i -D jest-wake-lock-mock
yarn add -D jest-wake-lock-mock
```
In your `jest.config.js` or `package.json` under `jest` section create a [`setupFiles`](https://jestjs.io/docs/en/configuration#setupfiles-array) array and add `jest-wake-lock-mock` to it.
```js
{
setupFiles: ['jest-wake-lock-mock'],
// jest config...
}
```
Write your tests with confidence using the same [Screen Wake Lock API](https://w3c.github.io/screen-wake-lock/) api as in the browser.
**Example** ([More](https://github.com/jorisre/jest-wake-lock-mock/blob/master/test/jest-wake-lock-mock.test.ts)):
```js
const requestWakeLock = async () => {
try {
const wakeLock = await navigator.wakeLock.request('screen');
return { wakeLock };
} catch (error) {
return { error };
}
};
test('wakeLock request with success', async () => {
const { wakeLock, error } = await requestWakeLock(handleRelease);
expect(error).not.toBeDefined();
expect(wakeLock).toBeDefined();
expect(wakeLock?.type).toEqual('screen');
expect(wakeLock?.released).toBe(false);
});
```
š¤ **Joris**
- Twitter: [@\_jorisre](https://twitter.com/_jorisre)
- Github: [@jorisre](https://github.com/jorisre)
Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](https://github.com/jorisre/jest-wake-lock-mock/issues).
Give a āļø if this project helped you!
Copyright Ā© 2020 [Joris](https://github.com/jorisre).<br />
This project is [MIT](https://github.com/jorisre/jest-wake-lock-mock/blob/master/LICENSE) licensed.
---
_This README was generated with ā¤ļø by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_