Skip to content

Commit

Permalink
Update to latest ESLint and associated config
Browse files Browse the repository at this point in the history
Also update code to use const/let instead of var, and reformat
redirect-test.js.
  • Loading branch information
robertknight committed Apr 18, 2023
1 parent d64f2d2 commit 548ad64
Show file tree
Hide file tree
Showing 4 changed files with 2,686 additions and 1,382 deletions.
4 changes: 2 additions & 2 deletions bouncer/scripts/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ export function redirect(navigateTo, settings) {
return;
}

var chrome = window.chrome;
const chrome = window.chrome;
if (chrome && chrome.runtime && chrome.runtime.sendMessage) {
// The user is using Chrome, redirect them to our Chrome extension if they
// have it installed, via otherwise.
chrome.runtime.sendMessage(
settings.chromeExtensionId,
{type: 'ping'},
function (response) {
var url;
let url;

if (response && !chrome.runtime.lastError) {
// The user has our Chrome extension installed :)
Expand Down
95 changes: 60 additions & 35 deletions bouncer/scripts/test/redirect-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { redirect } from '../redirect.js';

describe('#redirect', function () {
var settings;
let settings;
beforeEach(function () {
window.chrome = undefined;
settings = {
chromeExtensionId: 'test-extension-id',
extensionUrl: 'http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q',
viaUrl: 'https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q',
extensionUrl:
'http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q',
viaUrl:
'https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q',
};
sinon.stub(window.console, 'error');
});
Expand All @@ -17,59 +19,68 @@ describe('#redirect', function () {
});

it('reads settings from the page by default', function () {
var settings = {
const settings = {
chromeExtensionId: 'a-b-c',
extensionUrl: 'https://example.org/#annotations:123',
viaUrl: 'https://proxy.it/#annotations:123',
};
var settingsEl = document.createElement('script');
const settingsEl = document.createElement('script');
settingsEl.type = 'application/json';
settingsEl.className = 'js-bouncer-settings';
settingsEl.textContent = JSON.stringify(settings);
document.body.appendChild(settingsEl);
var navigateTo = sinon.stub();
const navigateTo = sinon.stub();

redirect(navigateTo);

assert.isTrue(navigateTo.calledWith(settings.viaUrl));
});

it('redirects to Via if not Chrome', function () {
window.chrome = undefined; // The user isn't using Chrome.
var navigateTo = sinon.stub();
window.chrome = undefined; // The user isn't using Chrome.
const navigateTo = sinon.stub();

redirect(navigateTo, settings);

assert.equal(navigateTo.calledOnce, true);
assert.equal(
navigateTo.calledWithExactly('https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'),
true);
navigateTo.calledWithExactly(
'https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'
),
true
);
});

it('redirects to Via if window.chrome but no chrome.runtime', function () {
// Some browsers define window.chrome but not chrome.runtime.
window.chrome = {};
var navigateTo = sinon.stub();
const navigateTo = sinon.stub();

redirect(navigateTo, settings);

assert.equal(navigateTo.calledOnce, true);
assert.equal(
navigateTo.calledWithExactly('https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'),
true);
navigateTo.calledWithExactly(
'https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'
),
true
);
});

it('redirects to Via if window.chrome but no sendMessage', function () {
// Some browsers might window.chrome but not chrome.runtime.sendMessage.
window.chrome = {runtime: {}};
var navigateTo = sinon.stub();
window.chrome = { runtime: {} };
const navigateTo = sinon.stub();

redirect(navigateTo, settings);

assert.equal(navigateTo.calledOnce, true);
assert.equal(
navigateTo.calledWithExactly('https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'),
true);
navigateTo.calledWithExactly(
'https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'
),
true
);
});

it('redirects to Via if Chrome but no extension', function () {
Expand All @@ -80,14 +91,17 @@ describe('#redirect', function () {
},
},
};
var navigateTo = sinon.stub();
const navigateTo = sinon.stub();

redirect(navigateTo, settings);

assert.equal(navigateTo.calledOnce, true);
assert.equal(
navigateTo.calledWithExactly('https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'),
true);
navigateTo.calledWithExactly(
'https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'
),
true
);
});

it('redirects to Via if lastError is defined', function () {
Expand All @@ -96,17 +110,20 @@ describe('#redirect', function () {
sendMessage: function (id, message, callbackFunction) {
callbackFunction('Hey!');
},
lastError: {message: 'There was an error'},
lastError: { message: 'There was an error' },
},
};
var navigateTo = sinon.stub();
const navigateTo = sinon.stub();

redirect(navigateTo, settings);

assert.equal(navigateTo.calledOnce, true);
assert.equal(
navigateTo.calledWithExactly('https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'),
true);
navigateTo.calledWithExactly(
'https://via.hypothes.is/http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'
),
true
);
});

it('logs an error if lastError is defined', function () {
Expand All @@ -115,7 +132,7 @@ describe('#redirect', function () {
sendMessage: function (id, message, callbackFunction) {
callbackFunction('Hey!');
},
lastError: {message: 'There was an error'},
lastError: { message: 'There was an error' },
},
};

Expand All @@ -134,12 +151,17 @@ describe('#redirect', function () {
redirect(function () {}, settings);

assert.equal(window.chrome.runtime.sendMessage.calledOnce, true);
assert.equal(window.chrome.runtime.sendMessage.firstCall.args[0],
'test-extension-id');
assert.deepEqual(window.chrome.runtime.sendMessage.firstCall.args[1],
{type: 'ping'});
assert.equal(typeof(window.chrome.runtime.sendMessage.firstCall.args[2]),
'function');
assert.equal(
window.chrome.runtime.sendMessage.firstCall.args[0],
'test-extension-id'
);
assert.deepEqual(window.chrome.runtime.sendMessage.firstCall.args[1], {
type: 'ping',
});
assert.equal(
typeof window.chrome.runtime.sendMessage.firstCall.args[2],
'function'
);
});

it('redirects to Chrome extension if installed', function () {
Expand All @@ -150,19 +172,22 @@ describe('#redirect', function () {
},
},
};
var navigateTo = sinon.stub();
const navigateTo = sinon.stub();

redirect(navigateTo, settings);

assert.equal(navigateTo.calledOnce, true);
assert.equal(
navigateTo.calledWithExactly('http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'),
true);
navigateTo.calledWithExactly(
'http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q'
),
true
);
});

it('redirects to original URL if no Via URL provided', function () {
settings.viaUrl = null;
var navigateTo = sinon.stub();
const navigateTo = sinon.stub();

redirect(navigateTo, settings);

Expand Down
Loading

0 comments on commit 548ad64

Please sign in to comment.