Skip to content

Instantly share code, notes, and snippets.

@Qarun-Qadir-Bissoondial
Last active April 18, 2020 22:35
Show Gist options
  • Save Qarun-Qadir-Bissoondial/5584cbbdcd19eafb057d57c08031d337 to your computer and use it in GitHub Desktop.
Save Qarun-Qadir-Bissoondial/5584cbbdcd19eafb057d57c08031d337 to your computer and use it in GitHub Desktop.
capitalizeName - Version 2 (more robust)
import { capitalizeName } from "./capitalizeName";
describe('capitalizeName', () => {
it('should capitalize the name in the object', () => {
const testObj = { name: 'Qarun', age: 25 };
expect(capitalizeName(testObj)).toEqual({name: 'QARUN', age: 25});
});
it('should not fail if the name key is missing', () => {
const missingName = { age: 25 };
expect(capitalizeName(missingName)).toEqual({name: '', age: 25});
});
it('should throw an error if the name key is not a string', () => {
const improperName = { name: 10, age: 25 };
expect(() => { capitalizeName(improperName) }).toThrowError("'name' key in object must be a string.");
});
it('should throw an error if the object is null or undefined', () => {
expect(() => { capitalizeName(undefined) }).toThrowError('object is null or undefined');
expect(() => { capitalizeName(null) }).toThrowError('object is null or undefined');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment