From d8c92b9ba6b52c6ac8581b4a298c7a42fcdaec21 Mon Sep 17 00:00:00 2001 From: Jordan Shatford Date: Thu, 21 Mar 2024 15:13:32 +1100 Subject: [PATCH] chore: use it.each for various other tests --- src/utils/__tests__/escapeName.spec.ts | 44 +++++++++++------------- src/utils/__tests__/getPattern.spec.ts | 22 ++++++------ src/utils/__tests__/type.spec.ts | 46 ++++++++++++++------------ src/utils/__tests__/unique.spec.ts | 2 +- 4 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/utils/__tests__/escapeName.spec.ts b/src/utils/__tests__/escapeName.spec.ts index aef56b729..7605cffba 100644 --- a/src/utils/__tests__/escapeName.spec.ts +++ b/src/utils/__tests__/escapeName.spec.ts @@ -2,36 +2,32 @@ import { describe, expect, it } from 'vitest'; import { escapeName, unescapeName } from '../escapeName'; -const data = [ - ['', "''"], - ['fooBar', 'fooBar'], - ['Foo Bar', `'Foo Bar'`], - ['foo bar', `'foo bar'`], - ['foo-bar', `'foo-bar'`], - ['foo.bar', `'foo.bar'`], - ['foo_bar', 'foo_bar'], - ['123foo.bar', `'123foo.bar'`], - ['@foo.bar', `'@foo.bar'`], - ['$foo.bar', `'$foo.bar'`], - ['_foo.bar', `'_foo.bar'`], - ['123foobar', `'123foobar'`], - ['@foobar', `'@foobar'`], - ['$foobar', '$foobar'], - ['_foobar', '_foobar'], +const toCheck: { unescaped: string; escaped: string }[] = [ + { unescaped: '', escaped: "''" }, + { unescaped: 'fooBar', escaped: 'fooBar' }, + { unescaped: 'Foo Bar', escaped: `'Foo Bar'` }, + { unescaped: 'foo bar', escaped: `'foo bar'` }, + { unescaped: 'foo-bar', escaped: `'foo-bar'` }, + { unescaped: 'foo.bar', escaped: `'foo.bar'` }, + { unescaped: 'foo_bar', escaped: 'foo_bar' }, + { unescaped: '123foo.bar', escaped: `'123foo.bar'` }, + { unescaped: '@foo.bar', escaped: `'@foo.bar'` }, + { unescaped: '$foo.bar', escaped: `'$foo.bar'` }, + { unescaped: '_foo.bar', escaped: `'_foo.bar'` }, + { unescaped: '123foobar', escaped: `'123foobar'` }, + { unescaped: '@foobar', escaped: `'@foobar'` }, + { unescaped: '$foobar', escaped: '$foobar' }, + { unescaped: '_foobar', escaped: '_foobar' }, ]; describe('escapeName', () => { - it('should escape', () => { - data.forEach(([unescaped, escaped]) => { - expect(escapeName(unescaped)).toBe(escaped); - }); + it.each(toCheck)('should escape $unescaped to $escaped', ({ unescaped, escaped }) => { + expect(escapeName(unescaped)).toBe(escaped); }); }); describe('unescapeName', () => { - it('should unescape', () => { - data.forEach(([unescaped, escaped]) => { - expect(unescapeName(escaped)).toBe(unescaped); - }); + it.each(toCheck)('should unescape $escaped to $unescaped', ({ unescaped, escaped }) => { + expect(unescapeName(escaped)).toBe(unescaped); }); }); diff --git a/src/utils/__tests__/getPattern.spec.ts b/src/utils/__tests__/getPattern.spec.ts index 1266210b8..8efe94b92 100644 --- a/src/utils/__tests__/getPattern.spec.ts +++ b/src/utils/__tests__/getPattern.spec.ts @@ -3,15 +3,17 @@ import { describe, expect, it } from 'vitest'; import { getPattern } from '../getPattern'; describe('getPattern', () => { - it('should produce correct result', () => { - expect(getPattern()).toEqual(undefined); - expect(getPattern('')).toEqual(''); - expect(getPattern('^[a-zA-Z]')).toEqual('^[a-zA-Z]'); - expect(getPattern('^\\w+$')).toEqual('^\\\\w+$'); - expect(getPattern('^\\d{3}-\\d{2}-\\d{4}$')).toEqual('^\\\\d{3}-\\\\d{2}-\\\\d{4}$'); - expect(getPattern('\\')).toEqual('\\\\'); - expect(getPattern('\\/')).toEqual('\\\\/'); - expect(getPattern('\\/\\/')).toEqual('\\\\/\\\\/'); - expect(getPattern("'")).toEqual("\\'"); + it.each([ + { pattern: undefined, expected: undefined }, + { pattern: '', expected: '' }, + { pattern: '^[a-zA-Z]', expected: '^[a-zA-Z]' }, + { pattern: '^\\w+$', expected: '^\\\\w+$' }, + { pattern: '^\\d{3}-\\d{2}-\\d{4}$', expected: '^\\\\d{3}-\\\\d{2}-\\\\d{4}$' }, + { pattern: '\\', expected: '\\\\' }, + { pattern: '\\/', expected: '\\\\/' }, + { pattern: '\\/\\/', expected: '\\\\/\\\\/' }, + { pattern: "'", expected: "\\'" }, + ])('getPattern($pattern) -> $expected', ({ pattern, expected }) => { + expect(getPattern(pattern)).toEqual(expected); }); }); diff --git a/src/utils/__tests__/type.spec.ts b/src/utils/__tests__/type.spec.ts index 346130a40..b1d763ad9 100644 --- a/src/utils/__tests__/type.spec.ts +++ b/src/utils/__tests__/type.spec.ts @@ -3,28 +3,30 @@ import { describe, expect, it } from 'vitest'; import { getMappedType, getType } from '../type'; describe('getMappedType', () => { - it('should map types to the basics', () => { - expect(getMappedType('')).toEqual(undefined); - expect(getMappedType('any')).toEqual('unknown'); - expect(getMappedType('array')).toEqual('unknown[]'); - expect(getMappedType('boolean')).toEqual('boolean'); - expect(getMappedType('byte')).toEqual('number'); - expect(getMappedType('char')).toEqual('string'); - expect(getMappedType('date-time')).toEqual('string'); - expect(getMappedType('date')).toEqual('string'); - expect(getMappedType('double')).toEqual('number'); - expect(getMappedType('file')).toEqual('binary'); - expect(getMappedType('float')).toEqual('number'); - expect(getMappedType('int')).toEqual('number'); - expect(getMappedType('integer')).toEqual('number'); - expect(getMappedType('long')).toEqual('number'); - expect(getMappedType('null')).toEqual('null'); - expect(getMappedType('number')).toEqual('number'); - expect(getMappedType('object')).toEqual('unknown'); - expect(getMappedType('password')).toEqual('string'); - expect(getMappedType('short')).toEqual('number'); - expect(getMappedType('string')).toEqual('string'); - expect(getMappedType('void')).toEqual('void'); + it.each([ + { type: '', expected: undefined }, + { type: 'any', expected: 'unknown' }, + { type: 'array', expected: 'unknown[]' }, + { type: 'boolean', expected: 'boolean' }, + { type: 'byte', expected: 'number' }, + { type: 'char', expected: 'string' }, + { type: 'date-time', expected: 'string' }, + { type: 'date', expected: 'string' }, + { type: 'double', expected: 'number' }, + { type: 'file', expected: 'binary' }, + { type: 'float', expected: 'number' }, + { type: 'int', expected: 'number' }, + { type: 'integer', expected: 'number' }, + { type: 'long', expected: 'number' }, + { type: 'null', expected: 'null' }, + { type: 'number', expected: 'number' }, + { type: 'object', expected: 'unknown' }, + { type: 'password', expected: 'string' }, + { type: 'short', expected: 'number' }, + { type: 'string', expected: 'string' }, + { type: 'void', expected: 'void' }, + ])('should map type $type to $expected', ({ type, expected }) => { + expect(getMappedType(type)).toEqual(expected); }); }); diff --git a/src/utils/__tests__/unique.spec.ts b/src/utils/__tests__/unique.spec.ts index 0af1b7e7a..08cf80e9e 100644 --- a/src/utils/__tests__/unique.spec.ts +++ b/src/utils/__tests__/unique.spec.ts @@ -9,7 +9,7 @@ describe('unique', () => { { value: 'a', index: 2, arr: ['a', 'b', 'c'], result: false }, { value: 'a', index: 1, arr: ['z', 'a', 'b'], result: true }, { value: 'a', index: 2, arr: ['y', 'z', 'a'], result: true }, - ])('unique($value, $index, $arr) === $result', ({ value, index, arr, result }) => { + ])('unique($value, $index, $arr) -> $result', ({ value, index, arr, result }) => { expect(unique(value, index, arr)).toEqual(result); });