From e1bf58361e5e2c0388fee5cb177fc2febe7130a0 Mon Sep 17 00:00:00 2001 From: "Rong Sen Ng (motss)" Date: Sat, 6 Jan 2024 22:57:02 +0800 Subject: [PATCH] test: refactor tests of splitString Signed-off-by: Rong Sen Ng (motss) --- src/__tests__/helpers/split-string.test.ts | 69 +++++++++------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/src/__tests__/helpers/split-string.test.ts b/src/__tests__/helpers/split-string.test.ts index f2e12ee9..a2384157 100644 --- a/src/__tests__/helpers/split-string.test.ts +++ b/src/__tests__/helpers/split-string.test.ts @@ -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])); }); });