Skip to content

Commit

Permalink
test: Update specs for string methods (#589)
Browse files Browse the repository at this point in the history
* style: Remove unnecessary `async` keyword

* test: Update specs for string methods
  • Loading branch information
mass2527 committed Sep 24, 2024
1 parent f38392c commit 7192cd7
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/compat/function/debounce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('debounce', () => {
expect(func).toHaveBeenCalledTimes(2);
});

it('should have no effect if we call cancel when the function is not executed', async () => {
it('should have no effect if we call cancel when the function is not executed', () => {
const func = vi.fn();
const debounceMs = 50;
const debouncedFunc = debounce(func, debounceMs);
Expand Down
14 changes: 7 additions & 7 deletions src/compat/string/endsWith.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ import { describe, it, expect } from 'vitest';
import { endsWith } from './endsWith';

describe('endsWith', () => {
it('should return true if the string ends with the target string', async () => {
it('should return true if the string ends with the target string', () => {
expect(endsWith('fooBar', 'Bar')).toEqual(true);
});

it('should return false if the string does not end with the target string', async () => {
it('should return false if the string does not end with the target string', () => {
expect(endsWith('fooBar', 'abc')).toEqual(false);
});

it('should return false if the string does not end with the target string, but does contain it', async () => {
it('should return false if the string does not end with the target string, but does contain it', () => {
expect(endsWith('fooBar', 'foo')).toEqual(false);
});

it('should return true if the target string is an empty string', async () => {
it('should return true if the target string is an empty string', () => {
expect(endsWith('fooBar', '')).toEqual(true);
});

it('should return true if the string and target string are empty strings', async () => {
it('should return true if the string and target string are empty strings', () => {
expect(endsWith('', '')).toEqual(true);
});

it('should return false if the string past the provided position does not end with the target string', async () => {
it('should return false if the string past the provided position does not end with the target string', () => {
expect(endsWith('fooBar', 'foo', 5)).toEqual(false);
});

it('should return true if the string before the provided position ends with the target string', async () => {
it('should return true if the string before the provided position ends with the target string', () => {
expect(endsWith('fooBar123', 'foo', 3)).toEqual(true);
});

Expand Down
14 changes: 7 additions & 7 deletions src/compat/string/startsWith.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ import { describe, it, expect } from 'vitest';
import { startsWith } from './startsWith';

describe('startsWith', () => {
it('should return true if the string starts with the target string', async () => {
it('should return true if the string starts with the target string', () => {
expect(startsWith('fooBar', 'foo')).toEqual(true);
});

it('should return false if the string does not start with the target string', async () => {
it('should return false if the string does not start with the target string', () => {
expect(startsWith('fooBar', 'abc')).toEqual(false);
});

it('should return false if the string does not start with the target string, but does contain it', async () => {
it('should return false if the string does not start with the target string, but does contain it', () => {
expect(startsWith('fooBar', 'Bar')).toEqual(false);
});

it('should return true if the target string is an empty string', async () => {
it('should return true if the target string is an empty string', () => {
expect(startsWith('fooBar', '')).toEqual(true);
});

it('should return true if the string and target string are empty strings', async () => {
it('should return true if the string and target string are empty strings', () => {
expect(startsWith('', '')).toEqual(true);
});

it('should return false if the string past the provided position does not start with the target string', async () => {
it('should return false if the string past the provided position does not start with the target string', () => {
expect(startsWith('fooBar', 'Bar', 5)).toEqual(false);
});

it('should return true if the string past the provided position does start with the target string', async () => {
it('should return true if the string past the provided position does start with the target string', () => {
expect(startsWith('fooBar', 'Bar', 3)).toEqual(true);
});

Expand Down
8 changes: 4 additions & 4 deletions src/function/after.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { describe, expect, it, vi } from 'vitest';
import { after } from './after';

describe('after', () => {
it('should throw error if n is less than zero.', async () => {
it('should throw error if n is less than zero.', () => {
const mockFn = vi.fn();
const n = -1;
expect(() => after(n, mockFn)).toThrowErrorMatchingInlineSnapshot('[Error: n must be a non-negative integer.]');
expect(() => after(NaN, mockFn)).toThrowErrorMatchingInlineSnapshot('[Error: n must be a non-negative integer.]');
});

it('should create a function that invokes `func` only after being called `n` calls.`', async () => {
it('should create a function that invokes `func` only after being called `n` calls.`', () => {
const mockFn = vi.fn();
const n = 3;

Expand All @@ -26,7 +26,7 @@ describe('after', () => {
expect(mockFn).toHaveBeenCalledTimes(2);
});

it('should not invoke func immediately when n is zero.', async () => {
it('should not invoke func immediately when n is zero.', () => {
const mockFn = vi.fn();
const afterFn = after(0, mockFn);
expect(mockFn).toHaveBeenCalledTimes(0);
Expand All @@ -35,7 +35,7 @@ describe('after', () => {
expect(mockFn).toHaveBeenCalledTimes(1);
});

it('should handle arguments correctly.', async () => {
it('should handle arguments correctly.', () => {
const mockFn = vi.fn();
mockFn.mockReturnValue(3);

Expand Down
8 changes: 4 additions & 4 deletions src/function/before.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { describe, expect, it, vi } from 'vitest';
import { before } from './before';

describe('before', () => {
it('should throw error if n is less than zero.', async () => {
it('should throw error if n is less than zero.', () => {
const mockFn = vi.fn();
expect(() => before(-1, mockFn)).toThrowErrorMatchingInlineSnapshot('[Error: n must be a non-negative integer.]');
});

it('should create a function that invokes `func` only until the `n-1`-th calls.', async () => {
it('should create a function that invokes `func` only until the `n-1`-th calls.', () => {
const mockFn = vi.fn();
mockFn.mockReturnValue(1);
const n = 3;
Expand All @@ -21,7 +21,7 @@ describe('before', () => {
expect(beforeFn()).toBeUndefined();
});

it('should not invoke func immediately when n is a positive integer', async () => {
it('should not invoke func immediately when n is a positive integer', () => {
const mockFn = vi.fn();
mockFn.mockReturnValue(1);
const n = 3;
Expand All @@ -32,7 +32,7 @@ describe('before', () => {
expect(mockFn).toHaveBeenCalledTimes(1);
});

it('should handle arguments correctly', async () => {
it('should handle arguments correctly', () => {
const mockFn = vi.fn();
mockFn.mockReturnValue(3);
const n = 3;
Expand Down
2 changes: 1 addition & 1 deletion src/function/debounce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('debounce', () => {
expect(func).toHaveBeenCalledTimes(2);
});

it('should have no effect if we call cancel when the function is not executed', async () => {
it('should have no effect if we call cancel when the function is not executed', () => {
const func = vi.fn();
const debounceMs = 50;
const debouncedFunc = debounce(func, debounceMs);
Expand Down
4 changes: 2 additions & 2 deletions src/function/throttle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { throttle } from './throttle';
import { delay } from '../promise';

describe('throttle', () => {
it('should throttle function calls', async () => {
it('should throttle function calls', () => {
const func = vi.fn();
const throttledFunc = throttle(func, 100);

Expand Down Expand Up @@ -34,7 +34,7 @@ describe('throttle', () => {
expect(func).toHaveBeenCalledTimes(2);
});

it('should call the function with correct arguments', async () => {
it('should call the function with correct arguments', () => {
const func = vi.fn();
const throttleMs = 50;
const throttledFunc = throttle(func, throttleMs);
Expand Down
20 changes: 10 additions & 10 deletions src/string/_internal/getWords.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,61 @@ import { describe, expect, it } from 'vitest';
import { getWords } from './getWords';

describe('caseSplitPattern', () => {
it('should match camelCase', async () => {
it('should match camelCase', () => {
const str = 'camelCase';
const matches = getWords(str);
expect(matches).toEqual(['camel', 'Case']);
});

it('should match snake_case', async () => {
it('should match snake_case', () => {
const str = 'snake_case';
const matches = getWords(str);
expect(matches).toEqual(['snake', 'case']);
});

it('should match kebab-case', async () => {
it('should match kebab-case', () => {
const str = 'kebab-case';
const matches = getWords(str);
expect(matches).toEqual(['kebab', 'case']);
});

it('should handle mixed formats', async () => {
it('should handle mixed formats', () => {
const str = 'camelCase_snake_case-kebabCase';
const matches = getWords(str);
expect(matches).toEqual(['camel', 'Case', 'snake', 'case', 'kebab', 'Case']);
});

it('should match acronyms', async () => {
it('should match acronyms', () => {
const str = 'HTTPRequest';
const matches = getWords(str);
expect(matches).toEqual(['HTTP', 'Request']);
});

it('should match special characters', async () => {
it('should match special characters', () => {
const str = 'special_characters@123';
const matches = getWords(str);
expect(matches).toEqual(['special', 'characters', '123']);
});

it('should handle leading and trailing whitespace', async () => {
it('should handle leading and trailing whitespace', () => {
const str = ' leading_and_trailing_whitespace ';
const matches = getWords(str);
expect(matches).toEqual(['leading', 'and', 'trailing', 'whitespace']);
});

it('should handle underscores', async () => {
it('should handle underscores', () => {
const str = 'underscore_case_example';
const matches = getWords(str);
expect(matches).toEqual(['underscore', 'case', 'example']);
});

it('should handle single character words', async () => {
it('should handle single character words', () => {
const str = 'aB';
const matches = getWords(str);
expect(matches).toEqual(['a', 'B']);
});

it('should work with hyphens ', () => {
it('should work with hyphens', () => {
expect(getWords('--FOO-BAR--')).toEqual(['FOO', 'BAR']);
});

Expand Down
22 changes: 9 additions & 13 deletions src/string/camelCase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,35 @@ import { describe, expect, it } from 'vitest';
import { camelCase } from './camelCase';

describe('camelCase', () => {
it('should change camel case to camel case', async () => {
it('should change camel case to camel case', () => {
expect(camelCase('camelCase')).toEqual('camelCase');
});

it('should change space to camel case', async () => {
it('should change space to camel case', () => {
expect(camelCase('some whitespace')).toEqual('someWhitespace');
});

it('should change hyphen to camel case', async () => {
it('should change hyphen to camel case', () => {
expect(camelCase('hyphen-text')).toEqual('hyphenText');
});

it('should change Acronyms to small letter', async () => {
it('should change Acronyms to small letter', () => {
expect(camelCase('HTTPRequest')).toEqual('httpRequest');
});

it('should handle leading and trailing whitespace', async () => {
expect(camelCase(' leading and trailing whitespace')).toEqual('leadingAndTrailingWhitespace');
it('should handle leading and trailing whitespace', () => {
expect(camelCase(' leading and trailing whitespace ')).toEqual('leadingAndTrailingWhitespace');
});

it('should handle special characters correctly', async () => {
it('should handle special characters correctly', () => {
expect(camelCase('special@characters!')).toEqual('specialCharacters');
});

it('should handle strings that are already in camel_case', async () => {
expect(camelCase('camel_case')).toEqual('camelCase');
});

it('should work with an empty string', async () => {
it('should work with an empty string', () => {
expect(camelCase('')).toEqual('');
});

it('should work with screaming camel case', async () => {
it('should work with screaming camel case', () => {
expect(camelCase('FOO_BAR')).toEqual('fooBar');
});
});
14 changes: 7 additions & 7 deletions src/string/capitalize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ import { describe, it, expect } from 'vitest';
import { capitalize } from './capitalize';

describe('capitalize', () => {
it('should converts the first character of string to upper case', async () => {
it('should converts the first character of string to upper case', () => {
expect(capitalize('fred')).toEqual('Fred');
});

it('should converts the first character of string to upper case and the remaining to lower case.', async () => {
it('should converts the first character of string to upper case and the remaining to lower case.', () => {
expect(capitalize('FRED')).toEqual('Fred');
});

it('should handle special characters correctly', async () => {
it('should handle special characters correctly', () => {
expect(capitalize('special@characters!')).toEqual('Special@characters!');
});

it('should handle hyphen correctly', async () => {
it('should handle hyphen correctly', () => {
expect(capitalize('hyphen-text')).toEqual('Hyphen-text');
});

it('should handle leading whitespace', async () => {
it('should handle leading whitespace', () => {
expect(capitalize(' fred')).toEqual(' fred');
});

it('should handle strings that are already in capitalize', async () => {
it('should handle strings that are already in capitalize', () => {
expect(capitalize('Fred')).toEqual('Fred');
});

it('should work with an empty string', async () => {
it('should work with an empty string', () => {
expect(capitalize('')).toEqual('');
});
});
Loading

0 comments on commit 7192cd7

Please sign in to comment.