Skip to content

Commit

Permalink
chore(test): coverage for nw.App (#26)
Browse files Browse the repository at this point in the history
* Add nw.Window.isDevToolsOpen polyfill
  • Loading branch information
ayushmanchhabra authored Aug 25, 2024
1 parent dbbfec2 commit 9619482
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 13 deletions.
96 changes: 87 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@
"scripts": {
"postinstall": "base-volta-off-of-nwjs",
"lint": "exit 0",
"test": "vitest run"
"test": "vitest run",
"dev": "nw ./tests/fixtures/custom"
},
"devDependencies": {
"@types/chrome": "^0.0.270",
"@types/node": "^22.4.0",
"@types/nw.js": "^0.90.0",
"@types/selenium-webdriver": "^4.1.25",
"selenium-webdriver": "^4.23.0",
"vitest": "^2.0.5"
},
"dependencies": {
"base-volta-off-of-nwjs": "^1.0.5",
"commander": "^12.1.0",
"nw": "0.90.0",
"nw-builder": "^4.8.1"
"nw": "^0.90.0-1",
"nw-builder": "^4.9.0"
},
"volta": {
"node": "22.0.0"
"node": "22.2.0"
}
}
29 changes: 29 additions & 0 deletions src/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Query the status of devtools window..
*
* @function
* @param {(status: boolean) => void} callback - Callback function
* @returns {void}
*/
function isDevToolsOpen(callback) {
chrome.windows.getAll({
populate: true,
windowTypes: ['devtools']
},
function (wins) {
callback(wins.length > 0);
})
}

/**
* Polyfill function for missing NW.js functionality.
*
* The environment is checked before adding the relevant functions.
*/
export default function polyfill () {
if (process.versions['nw-flavor'] === 'sdk' && typeof nw.Window.isDevToolsOpen !== 'function') {
nw.Window.isDevToolsOpen = isDevToolsOpen;
global.nw.Window.isDevToolsOpen = isDevToolsOpen;
window.nw.Window.isDevToolsOpen = isDevToolsOpen;
}
}
15 changes: 15 additions & 0 deletions tests/fixtures/custom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic</title>
<script src="./index.js"></script>
</head>

<body id="test">
Hello, World!
</body>

</html>
11 changes: 11 additions & 0 deletions tests/fixtures/custom/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
nw.Window.isDevToolsOpen = isDevToolsOpen;

function isDevToolsOpen(callback) {
chrome.windows.getAll({
populate: true,
windowTypes: ['devtools']
},
function (wins) {
callback(wins.length > 0);
})
}
6 changes: 6 additions & 0 deletions tests/fixtures/custom/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "custom-app",
"main": "index.html",
"description": "Placeholder app to test various things in NW.js",
"type": "module"
}
52 changes: 52 additions & 0 deletions tests/fixtures/nw/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('nw-app-argv').innerText = nw.App.argv;
document.getElementById('nw-app-fullargv').innerText = nw.App.fullArgv;
document.getElementById('nw-app-filteredargv').innerText = nw.App.filteredArgv;
document.getElementById('nw-app-startpath').innerText = nw.App.startPath;
document.getElementById('nw-app-datapath').innerText = nw.App.dataPath;
document.getElementById('nw-app-manifest').innerText = JSON.stringify(nw.App.manifest);
});
</script>
<title>nw.App fixture</title>
</head>

<body>
<div>
<span>nw.App.argv: </span>
<span id="nw-app-argv"></span>
</div>
<br>
<div>
<span>nw.App.fullArgv: </span>
<span id="nw-app-fullargv"></span>
</div>
<br>
<div>
<span>nw.App.filteredArgv: </span>
<span id="nw-app-filteredargv"></span>
</div>
<br>
<div>
<span>nw.App.startPath: </span>
<span id="nw-app-startpath"></span>
</div>
<br>
<div>
<span>nw.App.dataPath: </span>
<span id="nw-app-datapath"></span>
</div>
<br>
<div>
<span>nw.App.manifest: </span>
<span id="nw-app-manifest"></span>
</div>
</body>

</html>
4 changes: 4 additions & 0 deletions tests/fixtures/nw/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "nw-app",
"main": "index.html"
}
Loading

0 comments on commit 9619482

Please sign in to comment.