From 3e3b940d555216dce13fa666ccf5f7c9b7415244 Mon Sep 17 00:00:00 2001 From: Stanislav A Date: Thu, 20 Oct 2022 12:46:47 +0300 Subject: [PATCH] improve tests --- tests/helpers/parse-match-props.test.js | 90 ++++++++++++++++++++----- 1 file changed, 73 insertions(+), 17 deletions(-) diff --git a/tests/helpers/parse-match-props.test.js b/tests/helpers/parse-match-props.test.js index a4b15f90..98816f33 100644 --- a/tests/helpers/parse-match-props.test.js +++ b/tests/helpers/parse-match-props.test.js @@ -7,27 +7,83 @@ 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 METHOD_PROP = 'method'; +const URL_PROP = 'url'; - const GET_METHOD = 'GET'; - const MIXED_INPUT = `url:${URL_INPUT_4} method:${GET_METHOD}`; +const URL1 = 'example.com'; +const URL2 = 'http://example.com'; +const URL3 = '/^https?://example.org/'; +const URL4 = '/^https?://example.org/section#user:45/comments/'; - 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'); +test('Test different url props, simple input', (assert) => { + assert.ok(parseMatchProps(URL1).url, URL1, 'No url match prop, no protocol, not regexp'); + assert.ok(parseMatchProps(`url: ${URL1}`).url, URL1, '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(URL2).url, URL2, 'No url match prop, has protocol, not regexp'); + assert.ok(parseMatchProps(`url: ${URL2}`).url, URL2, '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(URL3).url, URL3, 'No url match prop, has protocol, regexp'); + assert.ok(parseMatchProps(`url: ${URL3}`).url, URL3, '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(URL4).url, URL4, 'No url match prop, has protocol, regexp, extra colon in url'); + assert.ok(parseMatchProps(`url: ${URL4}`).url, URL4, 'url match prop, has protocol, extra colon in url'); +}); + +test('Test different url props, mixed input', (assert) => { + const INPUT1 = `${URL1} ${METHOD_PROP}:${GET_METHOD}`; + const expected1 = { + url: URL1, + [METHOD_PROP]: GET_METHOD, + }; + assert.deepEqual(parseMatchProps(INPUT1), expected1, 'No url match prop, no protocol, not regexp'); + + const INPUT1_PREFIXED = `${URL_PROP}:${URL1} ${METHOD_PROP}:${GET_METHOD}`; + const expectedPrefixed1 = { + url: URL1, + [METHOD_PROP]: GET_METHOD, + }; + assert.deepEqual(parseMatchProps(INPUT1_PREFIXED), expectedPrefixed1, 'Has url match prop, no protocol, not regexp'); + + const INPUT2 = `${URL2} ${METHOD_PROP}:${GET_METHOD}`; + const expected2 = { + url: URL2, + [METHOD_PROP]: GET_METHOD, + }; + assert.deepEqual(parseMatchProps(INPUT2), expected2, 'No url match prop, has protocol, not regexp'); + + const INPUT2_PREFIXED = `${URL_PROP}:${URL2} ${METHOD_PROP}:${GET_METHOD}`; + const expectedPrefixed2 = { + url: URL2, + [METHOD_PROP]: GET_METHOD, + }; + assert.deepEqual(parseMatchProps(INPUT2_PREFIXED), expectedPrefixed2, 'Has url match prop, has protocol, not regexp'); + + const INPUT3 = `${URL3} ${METHOD_PROP}:${GET_METHOD}`; + const expected3 = { + url: URL3, + [METHOD_PROP]: GET_METHOD, + }; + assert.deepEqual(parseMatchProps(INPUT3), expected3, 'No url match prop, has protocol, regexp'); + + const INPUT3_PREFIXED = `${URL_PROP}:${URL3} ${METHOD_PROP}:${GET_METHOD}`; + const expectedPrefixed3 = { + url: URL3, + [METHOD_PROP]: GET_METHOD, + }; + assert.deepEqual(parseMatchProps(INPUT3_PREFIXED), expectedPrefixed3, 'Has url match prop, has protocol, regexp'); + + const INPUT4 = `${URL4} ${METHOD_PROP}:${GET_METHOD}`; + const expected4 = { + url: URL4, + [METHOD_PROP]: GET_METHOD, + }; + assert.deepEqual(parseMatchProps(INPUT4), expected4, 'No url match prop, has protocol, regexp, 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'); + const INPUT4_PREFIXED = `${URL_PROP}:${URL4} ${METHOD_PROP}:${GET_METHOD}`; + const expectedPrefixed4 = { + url: URL4, + [METHOD_PROP]: GET_METHOD, + }; + assert.deepEqual(parseMatchProps(INPUT4_PREFIXED), expectedPrefixed4, 'Has url match prop, has protocol, regexp, extra colon in url'); });