From e9510db0ba1fe7fa7b4bb404af8b95ef4fd82ac8 Mon Sep 17 00:00:00 2001 From: Arman Date: Thu, 23 Mar 2023 12:16:02 +0100 Subject: [PATCH] add tests for stirngs helpers, fix leftover mistakes from merge --- src/lib/helpers/string.ts | 20 ++++ .../attributes/edit.svelte | 2 +- .../attributes/relationship.svelte | 2 +- .../settings/displayName.svelte | 4 +- tests/unit/helpers/string.test.ts | 98 +++++++++++++++++++ 5 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 tests/unit/helpers/string.test.ts diff --git a/src/lib/helpers/string.ts b/src/lib/helpers/string.ts index d479aca05..b9f99f045 100644 --- a/src/lib/helpers/string.ts +++ b/src/lib/helpers/string.ts @@ -20,3 +20,23 @@ export function capitalize(str: string): string { export function singular(str: string): string { return str.replace(/s$/, ''); } + +/** + * Convert a dash/underscore/space separated string to camelCase. + * + * @export + * @param {string} str - The string to convert. + * @returns {string} The camelized string. + */ +export function camelize(str: string): string { + if (!str) { + return ''; + } + return str + .replace(/[-_\s]+(.)?/g, (_, char: string) => { + return char ? char.toUpperCase() : ''; + }) + .replace(/^(.)/, (firstChar) => { + return firstChar.toLowerCase(); + }); +} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/edit.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/edit.svelte index d442e36f4..2ead13a58 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/edit.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/edit.svelte @@ -61,7 +61,7 @@ } else updateButtonDisabled = true; - + {attr?.type} {#if attr?.type === 'Relationship'} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/relationship.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/relationship.svelte index ce40e817a..de438e241 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/relationship.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/attributes/relationship.svelte @@ -157,7 +157,7 @@ bind:value={data.related} required placeholder="Select a collection" - options={collectionList?.collections?.map((n) => ({ value: n.$id, label: n.name })) ?? []} /> + options={collections?.map((n) => ({ value: n.$id, label: n.name })) ?? []} /> {#if data?.related} {@const selectedCol = collections?.find((n) => n.$id === data.related)} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte index f71e40604..531f645d7 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/settings/displayName.svelte @@ -7,7 +7,7 @@ import { Button, Form, InputSelectSearch, InputText } from '$lib/elements/forms'; import { difference } from '$lib/helpers/array'; import { addNotification } from '$lib/stores/notifications'; - import { sdkForProject } from '$lib/stores/sdk'; + import { sdk } from '$lib/stores/sdk'; import { onMount } from 'svelte'; import { attributes, collection } from '../store'; @@ -57,7 +57,7 @@ (displayNames?.length && !displayNames[displayNames?.length - 1]); -
+ Display Name

diff --git a/tests/unit/helpers/string.test.ts b/tests/unit/helpers/string.test.ts new file mode 100644 index 000000000..eb145e9f5 --- /dev/null +++ b/tests/unit/helpers/string.test.ts @@ -0,0 +1,98 @@ +import { singular, camelize, capitalize } from '$lib/helpers/string'; + +/* +CAMELIZE +*/ + +test('camelize should convert hyphenated strings to camel case', () => { + const hyphenated = 'this-is-a-test'; + const expected = 'thisIsATest'; + expect(camelize(hyphenated)).toBe(expected); +}); + +test('camelize should convert underscored strings to camel case', () => { + const underscored = 'this_is_a_test'; + const expected = 'thisIsATest'; + expect(camelize(underscored)).toBe(expected); +}); + +test('camelize should convert spaced strings to camel case', () => { + const spaced = 'this is a test'; + const expected = 'thisIsATest'; + expect(camelize(spaced)).toBe(expected); +}); + +test('camelize should return empty string for falsy input', () => { + expect(camelize(null)).toBe(''); + expect(camelize(undefined)).toBe(''); + expect(camelize('')).toBe(''); +}); + +test('camelize should handle edge cases', () => { + const edgeCases = [ + { input: 'foo', expected: 'foo' }, + { input: 'foo-bar-', expected: 'fooBar' }, + { input: '-foo-bar', expected: 'fooBar' }, + { input: '--foo-bar--', expected: 'fooBar' }, + { input: '__foo__bar__', expected: 'fooBar' }, + { input: 'foo bar', expected: 'fooBar' }, + { input: 'foo\nbar', expected: 'fooBar' } + ]; + edgeCases.forEach(({ input, expected }) => { + expect(camelize(input)).toBe(expected); + }); +}); + +/* +SINGULAR +*/ + +test('singular should remove the "s" from strings', () => { + const pluralNouns = ['apples', 'bananas', 'cherries', 'elephants', 'horses', 'zebras']; + const singularNouns = ['apple', 'banana', 'cherrie', 'elephant', 'horse', 'zebra']; + pluralNouns.forEach((noun, index) => { + expect(singular(noun)).toBe(singularNouns[index]); + }); +}); + +test('singular should not remove characters from strings that do not contain "s"', () => { + const singularNouns = ['apple', 'banana', 'cherry']; + singularNouns.forEach((noun) => { + expect(singular(noun)).toBe(noun); + }); +}); + +test('singular should handle edge cases', () => { + const edgeCases = [ + { input: '', expected: '' }, + { input: 's', expected: '' }, + { input: 'ss', expected: 's' } + ]; + edgeCases.forEach(({ input, expected }) => { + expect(singular(input)).toBe(expected); + }); +}); + +/* +CAPITALIZE +*/ + +test('capitalize should capitalize the first letter of a string', () => { + const strings = ['hello world', 'this is a test', 'another example', '1234 testing']; + const expected = ['Hello world', 'This is a test', 'Another example', '1234 testing']; + strings.forEach((str, index) => { + expect(capitalize(str)).toBe(expected[index]); + }); +}); + +test('capitalize should handle empty strings', () => { + expect(capitalize('')).toBe(''); +}); + +test('capitalize should handle strings with no lowercase letters', () => { + expect(capitalize('HELLO')).toBe('HELLO'); +}); + +test('capitalize should handle strings with only one character', () => { + expect(capitalize('a')).toBe('A'); +});