Skip to content

Commit

Permalink
test: check existings of specific items
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Apr 27, 2022
1 parent b949200 commit 4753132
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
34 changes: 24 additions & 10 deletions spec-main/api-app-spec.ts
Expand Up @@ -1084,32 +1084,46 @@ describe('app module', () => {
const appName = fs.readJsonSync(path.join(appPath, 'package.json')).name;
const userDataPath = path.join(app.getPath('appData'), appName);
const tempBrowserDataPath = path.join(app.getPath('temp'), appName);
const defaultBrowserFile = path.join(userDataPath, 'Preferences');
const changedBrowserFile = path.join(tempBrowserDataPath, 'Preferences');

const sessionFiles = [
'Preferences',
'Code Cache',
'Local Storage',
'IndexedDB',
'Service Worker'
];
const hasSessionFiles = (dir: string) => {
for (const file of sessionFiles) {
if (!fs.existsSync(path.join(dir, file))) {
return false;
}
}
return true;
};

beforeEach(() => {
fs.removeSync(userDataPath);
fs.removeSync(tempBrowserDataPath);
});

it('writes to userData by default', () => {
expect(fs.existsSync(defaultBrowserFile)).to.equal(false);
expect(hasSessionFiles(userDataPath)).to.equal(false);
cp.spawnSync(process.execPath, [appPath]);
expect(fs.existsSync(defaultBrowserFile)).to.equal(true);
expect(hasSessionFiles(userDataPath)).to.equal(true);
});

it('can be changed', () => {
expect(fs.existsSync(changedBrowserFile)).to.equal(false);
expect(hasSessionFiles(userDataPath)).to.equal(false);
cp.spawnSync(process.execPath, [appPath, 'sessionData', tempBrowserDataPath]);
expect(fs.existsSync(defaultBrowserFile)).to.equal(false);
expect(fs.existsSync(changedBrowserFile)).to.equal(true);
expect(hasSessionFiles(userDataPath)).to.equal(false);
expect(hasSessionFiles(tempBrowserDataPath)).to.equal(true);
});

it('changing userData affects default sessionData', () => {
expect(fs.existsSync(changedBrowserFile)).to.equal(false);
expect(hasSessionFiles(userDataPath)).to.equal(false);
cp.spawnSync(process.execPath, [appPath, 'userData', tempBrowserDataPath]);
expect(fs.existsSync(defaultBrowserFile)).to.equal(false);
expect(fs.existsSync(changedBrowserFile)).to.equal(true);
expect(hasSessionFiles(userDataPath)).to.equal(false);
expect(hasSessionFiles(tempBrowserDataPath)).to.equal(true);
});
});
});
Expand Down
36 changes: 30 additions & 6 deletions spec-main/fixtures/apps/set-path/main.js
@@ -1,19 +1,43 @@
const http = require('http');
const { app, BrowserWindow } = require('electron');
const { app, ipcMain, BrowserWindow } = require('electron');

if (process.argv.length > 3) {
app.setPath(process.argv[2], process.argv[3]);
}

const html = `
<script>
async function main() {
localStorage.setItem('myCat', 'Tom')
const db = indexedDB.open('db-name', 1)
await new Promise(resolve => db.onsuccess = resolve)
await navigator.serviceWorker.register('sw.js', {scope: './'})
}
main().then(() => {
require('electron').ipcRenderer.send('success')
})
</script>
`;

const js = 'console.log("From service worker")';

app.once('ready', () => {
ipcMain.on('success', () => {
app.quit();
});

const server = http.createServer((request, response) => {
response.end('some text');
if (request.url === '/') {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(html);
} else if (request.url === '/sw.js') {
response.writeHead(200, { 'Content-Type': 'text/javascript' });
response.end(js);
}
}).listen(0, '127.0.0.1', () => {
const serverUrl = 'http://127.0.0.1:' + server.address().port;
const mainWindow = new BrowserWindow({ show: false });
mainWindow.webContents.once('did-finish-load', () => {
app.quit();
});
const mainWindow = new BrowserWindow({ show: false, webPreferences: { webSecurity: true, nodeIntegration: true, contextIsolation: false } });
mainWindow.loadURL(serverUrl);
});
});

0 comments on commit 4753132

Please sign in to comment.