One problem I see is that your function will fail when empty/null/undefined value is passed and you need a guard for that. And that is why you need a test for.
As an example, I'd test this function as follows
// Should be in another file
function capitalizeFirstLetter(string: string) {
if (!string) return '';
return string.charAt(0).toUpperCase() + string.slice(1);
}
describe('capitalizeFirstLetter', () => {
it('should return "Hello"', () => {
expect(capitalizeFirstLetter('hello')).toBe('Hello');
});
it('should return "Hi"', () => {
expect(capitalizeFirstLetter('Hi')).toBe('Hi');
});
it('should not fail when undefined/null or empty value is passed', () => {
expect(capitalizeFirstLetter(undefined)).toBe('');
expect(capitalizeFirstLetter(null)).toBe('');
expect(capitalizeFirstLetter('')).toBe('');
});
});
Or you can use describe.each with the list of payloads.
undefinedvalue. Can you show how you test it?capitalizeFirstLetter()? Is that actually a realistic case to deal with (and if so what should the behaviour be)? And given that this is a vanilla JS function, what relevance does React/Testing Library have? Give a minimal reproducible example of your test.