refactor: implement ajax() in tests using native fetch instead of jQuery (#32579)

This commit is contained in:
Milan Burda 2022-01-24 10:34:23 +01:00 committed by GitHub
parent 7032be660d
commit 9d054755d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 116 additions and 160 deletions

View file

@ -2,7 +2,6 @@
"env": {
"browser": true,
"mocha": true,
"jquery": true,
"serviceworker": true
},
"globals": {

View file

@ -1599,67 +1599,42 @@ describe('asar package', function () {
});
describe('asar protocol', function () {
it('can request a file in package', function (done) {
it('can request a file in package', async function () {
const p = path.resolve(asarDir, 'a.asar', 'file1');
$.get('file://' + p, function (data) {
try {
expect(data.trim()).to.equal('file1');
done();
} catch (e) {
done(e);
}
});
const response = await fetch('file://' + p);
const data = await response.text();
expect(data.trim()).to.equal('file1');
});
it('can request a file in package with unpacked files', function (done) {
it('can request a file in package with unpacked files', async function () {
const p = path.resolve(asarDir, 'unpack.asar', 'a.txt');
$.get('file://' + p, function (data) {
try {
expect(data.trim()).to.equal('a');
done();
} catch (e) {
done(e);
}
});
const response = await fetch('file://' + p);
const data = await response.text();
expect(data.trim()).to.equal('a');
});
it('can request a linked file in package', function (done) {
it('can request a linked file in package', async function () {
const p = path.resolve(asarDir, 'a.asar', 'link2', 'link1');
$.get('file://' + p, function (data) {
try {
expect(data.trim()).to.equal('file1');
done();
} catch (e) {
done(e);
}
});
const response = await fetch('file://' + p);
const data = await response.text();
expect(data.trim()).to.equal('file1');
});
it('can request a file in filesystem', function (done) {
it('can request a file in filesystem', async function () {
const p = path.resolve(asarDir, 'file');
$.get('file://' + p, function (data) {
try {
expect(data.trim()).to.equal('file');
done();
} catch (e) {
done(e);
}
});
const response = await fetch('file://' + p);
const data = await response.text();
expect(data.trim()).to.equal('file');
});
it('gets 404 when file is not found', function (done) {
it('gets error when file is not found', async function () {
const p = path.resolve(asarDir, 'a.asar', 'no-exist');
$.ajax({
url: 'file://' + p,
error: function (err) {
try {
expect(err.status).to.equal(404);
done();
} catch (e) {
done(e);
}
}
});
try {
const response = await fetch('file://' + p);
expect(response.status).to.equal(404);
} catch (error) {
expect(error.message).to.equal('Failed to fetch');
}
});
});

View file

@ -1,22 +1,21 @@
<html>
<body>
<script src="../../static/jquery-2.0.3.min.js"></script>
<script type="text/javascript" charset="utf-8">
var ipcRenderer = require('electron').ipcRenderer;
var port = location.search.substr("?port=".length);
$.ajax({
type: "GET",
url: "http://127.0.0.1:" + port,
headers: {
"Authorization": "Basic " + btoa("test:test")
},
success: function(result) {
ipcRenderer.sendToHost(result);
},
error: function(xhr, status, error) {
const { ipcRenderer } = require('electron');
const url = new URL(location.href);
const port = url.searchParams.get('port');
(async () => {
try {
const response = await fetch(`http://127.0.0.1:${port}`, {
headers: {
'Authorization': `Basic ${btoa('test:test')}`
}
});
ipcRenderer.sendToHost(await response.text());
} catch (error) {
ipcRenderer.sendToHost(error);
},
});
}
})();
</script>
</body>
</html>

View file

@ -1,8 +1,8 @@
<html>
<body>
<script src="../../static/jquery-2.0.3.min.js"></script>
<script type="text/javascript" charset="utf-8">
var port = location.search.substr("?port=".length);
const url = new URL(location.href);
const port = url.searchParams.get('port');
document.write("<img src='http://127.0.0.1:" + port + "/logo.png' />");
</script>
</body>

View file

@ -1,5 +1,4 @@
<body>
<script src="jquery-2.0.3.min.js"></script>
<script type="text/javascript" charset="utf-8">
(async function() {
// Deprecated APIs are still supported and should be tested.

File diff suppressed because one or more lines are too long