Skip to content

Commit

Permalink
use isBrowserSupported in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Jan 12, 2023
1 parent 53884e6 commit b7b1156
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
23 changes: 23 additions & 0 deletions src/helpers/check-compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Checks if current browser is supported
* To be injected to every scriptlet at build time
*
* isBrowserSupported is included to simplify injection
* by skipping dependencies attachment
*
* @throws error on unsupported browser
*/
export function checkCompatibility() {
Expand All @@ -23,3 +26,23 @@ export function checkCompatibility() {
throw new Error('Browser is not supported by AdGuard Scriptlets.');
}
}

/**
* Checks if current browser is supported
* For usage in tests
*
* @returns {boolean} true if current browser is supported
*/
export const isBrowserSupported = () => {
// arr.every() is not used because missing global method
// would throw ReferenceError when added to array
return typeof fetch !== 'undefined'
&& typeof Proxy !== 'undefined'
&& typeof Reflect !== 'undefined'
&& typeof Response !== 'undefined'
&& typeof Array.prototype.includes !== 'undefined'
&& typeof String.prototype.includes !== 'undefined'
&& typeof String.prototype.startsWith !== 'undefined'
&& typeof String.prototype.endsWith !== 'undefined'
&& typeof Element.prototype.attachShadow !== 'undefined';
};
8 changes: 2 additions & 6 deletions tests/scriptlets/hide-in-shadow-dom.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-underscore-dangle */
import { runScriptlet, clearGlobalProps } from '../helpers';
import { isBrowserSupported } from '../../src/helpers';

const { test, module } = QUnit;
const name = 'hide-in-shadow-dom';
Expand All @@ -22,12 +23,7 @@ const afterEach = () => {

module(name, { beforeEach, afterEach });

// some browsers do not support ShadowRoot
// for example, Firefox 52 which is used for browserstack tests
// https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot
const isSupported = typeof Element.prototype.attachShadow !== 'undefined';

if (!isSupported) {
if (!isBrowserSupported()) {
test('unsupported', (assert) => {
assert.ok(true, 'Browser does not support it');
});
Expand Down
11 changes: 6 additions & 5 deletions tests/scriptlets/prevent-fetch.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable no-underscore-dangle, no-console */
import { runScriptlet, clearGlobalProps } from '../helpers';
import { startsWith } from '../../src/helpers/string-utils';
import { isEmptyObject } from '../../src/helpers/object-utils';
import {
isEmptyObject,
startsWith,
isBrowserSupported,
} from '../../src/helpers';

const { test, module } = QUnit;
const name = 'prevent-fetch';
Expand All @@ -26,10 +29,8 @@ const afterEach = () => {

module(name, { beforeEach, afterEach });

const isSupported = typeof fetch !== 'undefined' && typeof Proxy !== 'undefined' && typeof Response !== 'undefined';

// TODO: add testcases with POST requests
if (!isSupported) {
if (!isBrowserSupported()) {
test('unsupported', (assert) => {
assert.ok(true, 'Browser does not support it');
});
Expand Down
8 changes: 2 additions & 6 deletions tests/scriptlets/remove-in-shadow-dom.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-underscore-dangle */
import { runScriptlet, clearGlobalProps } from '../helpers';
import { isBrowserSupported } from '../../src/helpers';

const { test, module } = QUnit;
const name = 'remove-in-shadow-dom';
Expand All @@ -22,12 +23,7 @@ const afterEach = () => {

module(name, { beforeEach, afterEach });

// some browsers do not support ShadowRoot
// for example, Firefox 52 which is used for browserstack tests
// https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot
const isSupported = typeof Element.prototype.attachShadow !== 'undefined';

if (!isSupported) {
if (!isBrowserSupported()) {
test('unsupported', (assert) => {
assert.ok(true, 'Browser does not support it');
});
Expand Down
6 changes: 2 additions & 4 deletions tests/scriptlets/trusted-replace-fetch-response.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-underscore-dangle, no-console */
import { runScriptlet, clearGlobalProps } from '../helpers';
import { startsWith } from '../../src/helpers/string-utils';
import { startsWith, isBrowserSupported } from '../../src/helpers';

const { test, module } = QUnit;
const name = 'trusted-replace-fetch-response';
Expand All @@ -25,9 +25,7 @@ const afterEach = () => {

module(name, { beforeEach, afterEach });

const isSupported = typeof fetch !== 'undefined' && typeof Proxy !== 'undefined' && typeof Response !== 'undefined';

if (!isSupported) {
if (!isBrowserSupported()) {
test('unsupported', (assert) => {
assert.ok(true, 'Browser does not support it');
});
Expand Down
6 changes: 2 additions & 4 deletions tests/scriptlets/xml-prune.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-underscore-dangle, no-console */
import { runScriptlet, clearGlobalProps } from '../helpers';
import { startsWith } from '../../src/helpers/string-utils';
import { startsWith, isBrowserSupported } from '../../src/helpers';

const { test, module } = QUnit;
const name = 'xml-prune';
Expand All @@ -26,9 +26,7 @@ const afterEach = () => {

module(name, { beforeEach, afterEach });

const isSupported = typeof fetch !== 'undefined' && typeof Proxy !== 'undefined' && typeof Response !== 'undefined';

if (!isSupported) {
if (!isBrowserSupported()) {
test('unsupported', (assert) => {
assert.ok(true, 'Browser does not support it');
});
Expand Down

0 comments on commit b7b1156

Please sign in to comment.