Skip to content

Commit

Permalink
test: refactor tests of splitString
Browse files Browse the repository at this point in the history
Signed-off-by: Rong Sen Ng (motss) <wes.ngrongsen@gmail.com>
  • Loading branch information
motss committed Jan 6, 2024
1 parent a5cdfec commit e1bf583
Showing 1 changed file with 29 additions and 40 deletions.
69 changes: 29 additions & 40 deletions src/__tests__/helpers/split-string.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,43 @@
import { expect } from '@open-wc/testing';
import { describe, expect, it } from 'vitest';

import { splitString } from '../../helpers/split-string';
import { messageFormatter } from '../test-utils/message-formatter';

describe(splitString.name, () => {
const str = 'hello, world, everyone';
const expected = str.split(/,\s*/);

type CaseSplitString = [
source: string,
expected: string[]
];
const casesSplitString: CaseSplitString[] = [
['', []],
[str, expected],
];
casesSplitString.forEach((a) => {
it(
messageFormatter('splits string (%s)', a),
() => {
const [testSource, expected] = a;
const result = splitString(testSource);

expect(result).deep.equal(expected);
}
);
it.each<{
$_value: string[];
source: string;
}>([
{
$_value: [],
source: '',
},
{
$_value: expected,
source: str,
},
])('splits string ($source)', ({
$_value,
source,
}) => {
const result = splitString(source);

expect(result).toEqual($_value);
});

type CaseSplitStringWithOptionalCallbackAndSeparator = [
separator?: RegExp
];
const casesSplitStringWithOptionalCallback: CaseSplitStringWithOptionalCallbackAndSeparator[] = [
it.each<[separator?: RegExp]>([
[],
[/,\s/],
];
casesSplitStringWithOptionalCallback.forEach((a) => {
it(
messageFormatter('splits string with optional callback and optional separator (%s)', a),
() => {
const [testSeparator] = a;

const result = splitString<[string]>(
str,
(n) => [n],
testSeparator
);

expect(result).deep.equal(expected.map(n => [n]));
}
);
])('splits string with optional callback and optional separator (%s)', (separator) => {
const result = splitString<[string]>(
str,
(n) => [n],
separator
);

expect(result).toEqual(expected.map(n => [n]));
});

});

0 comments on commit e1bf583

Please sign in to comment.