Skip to content

Commit

Permalink
split helpers tests to separate files and fix typo in noopPromiseResolve
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 19, 2022
1 parent 1bc6e81 commit bd76d2c
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 146 deletions.
1 change: 0 additions & 1 deletion src/helpers/noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const noopPromiseResolve = (responseBody = '{}', responseUrl = '', respon
const response = new Response(responseBody, {
status: 200,
statusText: 'OK',
type: responseType,
});

// Mock response' url & type to avoid adb checks
Expand Down
88 changes: 88 additions & 0 deletions tests/helpers/get-number-from-string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
getNumberFromString,
} from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';

module(name);

test('Test getNumberFromString for all data types inputs', (assert) => {
let inputValue;

// Boolean
inputValue = true;
assert.strictEqual(getNumberFromString(inputValue), null);

// null
inputValue = null;
assert.strictEqual(getNumberFromString(inputValue), null);

// undefined
inputValue = undefined;
assert.strictEqual(getNumberFromString(inputValue), null);

// undefined
inputValue = undefined;
assert.strictEqual(getNumberFromString(inputValue), null);

// number
inputValue = 123;
assert.strictEqual(getNumberFromString(inputValue), 123);

// valid string
inputValue = '123parsable';
assert.strictEqual(getNumberFromString(inputValue), 123);

// invalid string
inputValue = 'not parsable 123';
assert.strictEqual(getNumberFromString(inputValue), null);

// object
inputValue = { test: 'test' };
assert.strictEqual(getNumberFromString(inputValue), null);

// array
inputValue = ['test'];
assert.strictEqual(getNumberFromString(inputValue), null);
});

test('Test getNumberFromString for all data types inputs', (assert) => {
let inputValue;

// Boolean
inputValue = true;
assert.strictEqual(getNumberFromString(inputValue), null);

// null
inputValue = null;
assert.strictEqual(getNumberFromString(inputValue), null);

// undefined
inputValue = undefined;
assert.strictEqual(getNumberFromString(inputValue), null);

// undefined
inputValue = undefined;
assert.strictEqual(getNumberFromString(inputValue), null);

// number
inputValue = 123;
assert.strictEqual(getNumberFromString(inputValue), 123);

// valid string
inputValue = '123parsable';
assert.strictEqual(getNumberFromString(inputValue), 123);

// invalid string
inputValue = 'not parsable 123';
assert.strictEqual(getNumberFromString(inputValue), null);

// object
inputValue = { test: 'test' };
assert.strictEqual(getNumberFromString(inputValue), null);

// array
inputValue = ['test'];
assert.strictEqual(getNumberFromString(inputValue), null);
});
148 changes: 5 additions & 143 deletions tests/helpers/index.test.js
Original file line number Diff line number Diff line change
@@ -1,143 +1,5 @@
import {
toRegExp,
getNumberFromString,
noopPromiseResolve,
matchStackTrace,
parseMatchProps,
} from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';

module(name);

test('Test toRegExp for valid inputs', (assert) => {
const DEFAULT_VALUE = '.?';
const defaultRegexp = new RegExp(DEFAULT_VALUE);
let inputStr;
let expRegex;

inputStr = '/abc/';
expRegex = /abc/;
assert.deepEqual(toRegExp(inputStr), expRegex);

inputStr = '/[a-z]{1,9}/';
expRegex = /[a-z]{1,9}/;
assert.deepEqual(toRegExp(inputStr), expRegex);

inputStr = '';
assert.deepEqual(toRegExp(inputStr), defaultRegexp);
});

test('Test toRegExp for invalid inputs', (assert) => {
let inputStr;

assert.throws(() => {
inputStr = '/\\/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/[/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/*/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/[0-9]++/';
toRegExp(inputStr);
});
});

test('Test getNumberFromString for all data types inputs', (assert) => {
let inputValue;

// Boolean
inputValue = true;
assert.strictEqual(getNumberFromString(inputValue), null);

// null
inputValue = null;
assert.strictEqual(getNumberFromString(inputValue), null);

// undefined
inputValue = undefined;
assert.strictEqual(getNumberFromString(inputValue), null);

// undefined
inputValue = undefined;
assert.strictEqual(getNumberFromString(inputValue), null);

// number
inputValue = 123;
assert.strictEqual(getNumberFromString(inputValue), 123);

// valid string
inputValue = '123parsable';
assert.strictEqual(getNumberFromString(inputValue), 123);

// invalid string
inputValue = 'not parsable 123';
assert.strictEqual(getNumberFromString(inputValue), null);

// object
inputValue = { test: 'test' };
assert.strictEqual(getNumberFromString(inputValue), null);

// array
inputValue = ['test'];
assert.strictEqual(getNumberFromString(inputValue), null);
});

test('Test noopPromiseResolve for valid response props', async (assert) => {
const TEST_URL = 'url';
const TEST_TYPE = 'opaque';
const objResponse = await noopPromiseResolve('{}');
const objBody = await objResponse.json();

const arrResponse = await noopPromiseResolve('[]');
const arrBody = await arrResponse.json();

const responseWithUrl = await noopPromiseResolve('{}', TEST_URL);
const responseWithType = await noopPromiseResolve('{}', '', TEST_TYPE);

assert.ok(responseWithUrl.url === TEST_URL);
assert.ok(typeof objBody === 'object' && !objBody.length);
assert.ok(Array.isArray(arrBody) && !arrBody.length);
assert.strictEqual(responseWithType.type, TEST_TYPE);
});

test('Test matchStackTrace for working with getNativeRegexpTest helper', async (assert) => {
const match = matchStackTrace('stack', new Error().stack);

assert.ok(!match);
});

test('Test parseMatchProps for working with different url inputs', (assert) => {
const URL_INPUT_1 = 'example.com';
const URL_INPUT_2 = 'http://example.com';
const URL_INPUT_3 = '/^https?://example.org/';
const URL_INPUT_4 = '/^https?://example.org/section#user:45/comments/';

const GET_METHOD = 'GET';
const MIXED_INPUT = `url:${URL_INPUT_3} method:${GET_METHOD}`;

assert.ok(parseMatchProps(URL_INPUT_1).url, URL_INPUT_1, 'No url match prop, no protocol, not regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_1}`).url, URL_INPUT_1, 'url match prop, no protocol, not regexp');

assert.ok(parseMatchProps(URL_INPUT_2).url, URL_INPUT_2, 'No url match prop, has protocol, not regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_2}`).url, URL_INPUT_2, 'url match prop, has protocol, not regexp');

assert.ok(parseMatchProps(URL_INPUT_3).url, URL_INPUT_3, 'No url match prop, has protocol, regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_3}`).url, URL_INPUT_3, 'url match prop, has protocol, regexp');

assert.ok(parseMatchProps(URL_INPUT_4).url, URL_INPUT_4, 'No url match prop, has protocol, regexp, extra colon in url');
assert.ok(parseMatchProps(`url: ${URL_INPUT_4}`).url, URL_INPUT_4, 'url match prop, has protocol, extra colon in url');

assert.ok(parseMatchProps(MIXED_INPUT).url, URL_INPUT_3, 'Mixed input, url is parsed correctly');
assert.ok(parseMatchProps(MIXED_INPUT).method, GET_METHOD, 'Mixed input, method is parsed correctly');
});
import './get-number-from-string.test';
import './match-stack-trace.test';
import './noop-promise-resolve.test';
import './parse-match-props.test';
import './to-regexp.test';
14 changes: 14 additions & 0 deletions tests/helpers/match-stack-trace.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
matchStackTrace,
} from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';

module(name);

test('Test matchStackTrace for working with getNativeRegexpTest helper', async (assert) => {
const match = matchStackTrace('stack', new Error().stack);

assert.ok(!match);
});
26 changes: 26 additions & 0 deletions tests/helpers/noop-promise-resolve.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
noopPromiseResolve,
} from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';

module(name);

test('Test noopPromiseResolve for valid response props', async (assert) => {
const TEST_URL = 'url';
const TEST_TYPE = 'opaque';
const objResponse = await noopPromiseResolve('{}');
const objBody = await objResponse.json();

const arrResponse = await noopPromiseResolve('[]');
const arrBody = await arrResponse.json();

const responseWithUrl = await noopPromiseResolve('{}', TEST_URL);
const responseWithType = await noopPromiseResolve('{}', '', TEST_TYPE);

assert.ok(responseWithUrl.url === TEST_URL);
assert.ok(typeof objBody === 'object' && !objBody.length);
assert.ok(Array.isArray(arrBody) && !arrBody.length);
assert.strictEqual(responseWithType.type, TEST_TYPE);
});
33 changes: 33 additions & 0 deletions tests/helpers/parse-match-props.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
parseMatchProps,
} from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';

module(name);

test('Test parseMatchProps for working with different url inputs', (assert) => {
const URL_INPUT_1 = 'example.com';
const URL_INPUT_2 = 'http://example.com';
const URL_INPUT_3 = '/^https?://example.org/';
const URL_INPUT_4 = '/^https?://example.org/section#user:45/comments/';

const GET_METHOD = 'GET';
const MIXED_INPUT = `url:${URL_INPUT_4} method:${GET_METHOD}`;

assert.ok(parseMatchProps(URL_INPUT_1).url, URL_INPUT_1, 'No url match prop, no protocol, not regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_1}`).url, URL_INPUT_1, 'url match prop, no protocol, not regexp');

assert.ok(parseMatchProps(URL_INPUT_2).url, URL_INPUT_2, 'No url match prop, has protocol, not regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_2}`).url, URL_INPUT_2, 'url match prop, has protocol, not regexp');

assert.ok(parseMatchProps(URL_INPUT_3).url, URL_INPUT_3, 'No url match prop, has protocol, regexp');
assert.ok(parseMatchProps(`url: ${URL_INPUT_3}`).url, URL_INPUT_3, 'url match prop, has protocol, regexp');

assert.ok(parseMatchProps(URL_INPUT_4).url, URL_INPUT_4, 'No url match prop, has protocol, regexp, extra colon in url');
assert.ok(parseMatchProps(`url: ${URL_INPUT_4}`).url, URL_INPUT_4, 'url match prop, has protocol, extra colon in url');

assert.ok(parseMatchProps(MIXED_INPUT).url, URL_INPUT_4, 'Mixed input, url is parsed correctly');
assert.ok(parseMatchProps(MIXED_INPUT).method, GET_METHOD, 'Mixed input, method is parsed correctly');
});
50 changes: 50 additions & 0 deletions tests/helpers/to-regexp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
toRegExp,
} from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';

module(name);

test('Test toRegExp for valid inputs', (assert) => {
const DEFAULT_VALUE = '.?';
const defaultRegexp = new RegExp(DEFAULT_VALUE);
let inputStr;
let expRegex;

inputStr = '/abc/';
expRegex = /abc/;
assert.deepEqual(toRegExp(inputStr), expRegex);

inputStr = '/[a-z]{1,9}/';
expRegex = /[a-z]{1,9}/;
assert.deepEqual(toRegExp(inputStr), expRegex);

inputStr = '';
assert.deepEqual(toRegExp(inputStr), defaultRegexp);
});

test('Test toRegExp for invalid inputs', (assert) => {
let inputStr;

assert.throws(() => {
inputStr = '/\\/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/[/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/*/';
toRegExp(inputStr);
});

assert.throws(() => {
inputStr = '/[0-9]++/';
toRegExp(inputStr);
});
});
4 changes: 2 additions & 2 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './scriptlets/index.test';
import './redirects/index.test';
// import './scriptlets/index.test';
// import './redirects/index.test';
import './lib-tests/index.test';
import './helpers/index.test';

0 comments on commit bd76d2c

Please sign in to comment.