test: Add missing test cases

This commit is contained in:
raon0211 2024-09-14 18:00:33 +09:00
parent 4b9c4985b6
commit a02c616e56
5 changed files with 29 additions and 16 deletions

View File

@ -1,44 +1,44 @@
import { describe, expect, it } from 'vitest';
import { trimEnd } from './trimEnd';
import { trim } from './trim';
import { whitespace } from '../_internal/whitespace';
describe('trimEnd', () => {
const func = trimEnd;
describe('trim', () => {
const func = trim;
it(`\`trimEnd\` should remove trailing whitespace`, () => {
it(`\`trim\` should remove trailing whitespace`, () => {
const string = `${whitespace}a b c${whitespace}`;
const expected = `${whitespace}a b c`;
const expected = `a b c`;
expect(func(string)).toBe(expected);
});
it(`\`trimEnd\` should coerce \`string\` to a string`, () => {
it(`\`trim\` should coerce \`string\` to a string`, () => {
const object = {
toString: () => `${whitespace}a b c${whitespace}`,
};
const expected = `${whitespace}a b c`;
const expected = `a b c`;
// @ts-ignore
expect(func(object)).toBe(expected);
});
it(`\`trimEnd\` should remove trailing \`chars\``, () => {
it(`\`trim\` should remove leading and trailing \`chars\``, () => {
const string = '-_-a-b-c-_-';
const expected = `${'-_-'}a-b-c`;
const expected = `a-b-c`;
expect(func(string, '_-')).toBe(expected);
});
it(`\`trimEnd\` should coerce \`chars\` to a string`, () => {
it(`\`trim\` should coerce \`chars\` to a string`, () => {
const object = { toString: () => '_-' };
const string = '-_-a-b-c-_-';
const expected = `${'-_-'}a-b-c`;
const expected = `a-b-c`;
// @ts-ignore
expect(func(string, object)).toBe(expected);
});
it(`\`trimEnd\` should return an empty string for empty values and \`chars\``, () => {
it(`\`trim\` should return an empty string for empty values and \`chars\``, () => {
[null, '_-'].forEach(chars => {
// eslint-disable-next-line
// @ts-ignore
@ -52,17 +52,17 @@ describe('trimEnd', () => {
});
});
it(`\`trimEnd\` should work with \`undefined\` or empty string values for \`chars\``, () => {
it(`\`trim\` should work with \`undefined\` or empty string values for \`chars\``, () => {
const string = `${whitespace}a b c${whitespace}`;
const expected = `${whitespace}a b c`;
const expected = `a b c`;
expect(func(string, undefined)).toBe(expected);
expect(func(string, '')).toBe(string);
});
it(`\`trimEnd\` should work as an iteratee for methods like \`_.map\``, () => {
it(`\`trim\` should work as an iteratee for methods like \`_.map\``, () => {
const string = Object(`${whitespace}a b c${whitespace}`);
const trimmed = `${whitespace}a b c`;
const trimmed = `a b c`;
// eslint-disable-next-line
// @ts-ignore
const actual = [string, string, string].map(func);

View File

@ -27,6 +27,7 @@ describe('trimEnd', () => {
const expected = `${'-_-'}a-b-c`;
expect(func(string, '_-')).toBe(expected);
expect(func(string, ['-', '_'])).toBe(expected);
});
it(`\`trimEnd\` should coerce \`chars\` to a string`, () => {

View File

@ -41,4 +41,8 @@ describe('trim', () => {
it('should remove numbers from a string', async () => {
expect(trim('123abc456', ['1', '2', '3', '4', '5', '6'])).toEqual('abc');
});
it('should trim the string without giving the second parameter, which defaults to whitespace', async () => {
expect(trim(' hello world ')).toEqual('hello world');
});
});

View File

@ -41,4 +41,8 @@ describe('trimEnd', () => {
it('should handle cases where multiple trailing characters need removal', async () => {
expect(trimEnd('abc123abc123abc', 'c')).toEqual('abc123abc123ab');
});
it('should trim the string without giving the second parameter, which defaults to whitespace', async () => {
expect(trimEnd(' hello world ')).toEqual(' hello world');
});
});

View File

@ -41,4 +41,8 @@ describe('trimStart', () => {
it('should handle cases where multiple leading characters need removal', async () => {
expect(trimStart('aaaabbbcccc', 'a')).toEqual('bbbcccc');
});
it('should trim the string without giving the second parameter, which defaults to whitespace', async () => {
expect(trimStart(' hello world ')).toEqual('hello world ');
});
});