Move shlink-web-component tests to their own folder

This commit is contained in:
Alejandro Celaya
2023-08-02 09:01:44 +02:00
parent c48facc863
commit c794ff8b58
124 changed files with 455 additions and 371 deletions

View File

@@ -0,0 +1,65 @@
import {
nonEmptyValueOrNull,
parseBooleanToString,
parseOptionalBooleanToString,
rangeOf,
} from '../../../src/utils/helpers';
describe('utils', () => {
describe('rangeOf', () => {
const func = (i: number) => `result_${i}`;
const size = 5;
it('builds a range of specified size invike provided function', () => {
expect(rangeOf(size, func)).toEqual([
'result_1',
'result_2',
'result_3',
'result_4',
'result_5',
]);
});
it('builds a range starting at provided pos', () => {
const startAt = 3;
expect(rangeOf(size, func, startAt)).toEqual([
'result_3',
'result_4',
'result_5',
]);
});
});
describe('nonEmptyValueOrNull', () => {
it.each([
['', null],
['Hello', 'Hello'],
[[], null],
[[1, 2, 3], [1, 2, 3]],
[{}, null],
[{ foo: 'bar' }, { foo: 'bar' }],
])('returns expected value based on input', (value, expected) => {
expect(nonEmptyValueOrNull(value)).toEqual(expected);
});
});
describe('parseBooleanToString', () => {
it.each([
[true, 'true'],
[false, 'false'],
])('parses value as expected', (value, expectedResult) => {
expect(parseBooleanToString(value)).toEqual(expectedResult);
});
});
describe('parseOptionalBooleanToString', () => {
it.each([
[undefined, undefined],
[true, 'true'],
[false, 'false'],
])('parses value as expected', (value, expectedResult) => {
expect(parseOptionalBooleanToString(value)).toEqual(expectedResult);
});
});
});

View File

@@ -0,0 +1,20 @@
import { roundTen } from '../../../src/utils/helpers/numbers';
describe('numbers', () => {
describe('roundTen', () => {
it('rounds provided number to the next multiple of ten', () => {
const expectationsPairs = [
[10, 10],
[12, 20],
[158, 160],
[5, 10],
[-42, -40],
];
expect.assertions(expectationsPairs.length);
expectationsPairs.forEach(([number, expected]) => {
expect(roundTen(number)).toEqual(expected);
});
});
});
});

View File

@@ -0,0 +1,36 @@
import type { QrCodeFormat, QrErrorCorrection } from '../../../src/utils/helpers/qrCodes';
import { buildQrCodeUrl } from '../../../src/utils/helpers/qrCodes';
describe('qrCodes', () => {
describe('buildQrCodeUrl', () => {
it.each([
[
'bar.io',
{ size: 870, format: 'svg' as QrCodeFormat, margin: 0, errorCorrection: 'L' as QrErrorCorrection },
'bar.io/qr-code?size=870&format=svg&errorCorrection=L',
],
[
'bar.io',
{ size: 200, format: 'svg' as QrCodeFormat, margin: 0, errorCorrection: 'L' as QrErrorCorrection },
'bar.io/qr-code?size=200&format=svg&errorCorrection=L',
],
[
'shlink.io',
{ size: 456, format: 'png' as QrCodeFormat, margin: 10, errorCorrection: 'L' as QrErrorCorrection },
'shlink.io/qr-code?size=456&format=png&errorCorrection=L&margin=10',
],
[
'shlink.io',
{ size: 456, format: 'png' as QrCodeFormat, margin: 0, errorCorrection: 'H' as QrErrorCorrection },
'shlink.io/qr-code?size=456&format=png&errorCorrection=H',
],
[
'shlink.io',
{ size: 999, format: 'png' as QrCodeFormat, margin: 20, errorCorrection: 'Q' as QrErrorCorrection },
'shlink.io/qr-code?size=999&format=png&errorCorrection=Q&margin=20',
],
])('builds expected URL based in params', (shortUrl, options, expectedUrl) => {
expect(buildQrCodeUrl(shortUrl, options)).toEqual(expectedUrl);
});
});
});

View File

@@ -0,0 +1,25 @@
import { parseQuery, stringifyQuery } from '../../../src/utils/helpers/query';
describe('query', () => {
describe('parseQuery', () => {
it.each([
['', {}],
['foo=bar', { foo: 'bar' }],
['?foo=bar', { foo: 'bar' }],
['?foo=bar&baz=123', { foo: 'bar', baz: '123' }],
])('parses query string as expected', (queryString, expectedResult) => {
expect(parseQuery(queryString)).toEqual(expectedResult);
});
});
describe('stringifyQuery', () => {
it.each([
[{}, ''],
[{ foo: 'bar' }, 'foo=bar'],
[{ foo: 'bar', baz: '123' }, 'foo=bar&baz=123'],
[{ bar: 'foo', list: ['one', 'two'] }, encodeURI('bar=foo&list[]=one&list[]=two')],
])('stringifies query as expected', (queryObj, expectedResult) => {
expect(stringifyQuery(queryObj)).toEqual(expectedResult);
});
});
});