diff --git a/src/lib/stores/childrenStore.test.ts b/src/lib/stores/childrenStore.test.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/lib/stores/childrenData.ts b/src/lib/stores/childrenStore.ts similarity index 100% rename from src/lib/stores/childrenData.ts rename to src/lib/stores/childrenStore.ts diff --git a/src/lib/stores/contentStore.test.ts b/src/lib/stores/contentStore.test.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/lib/stores/userStore.test.ts b/src/lib/stores/userStore.test.ts new file mode 100644 index 00000000..a1c0fd87 --- /dev/null +++ b/src/lib/stores/userStore.test.ts @@ -0,0 +1,66 @@ +// userStore.test.ts + +import { get } from 'svelte/store'; +import { describe, expect, it } from 'vitest'; +import { addUser, removeUser, type UserData, users } from './userStore'; + +// Mock user data + +// Test suite for userStore +describe('test_normal_function', () => { + const mockUserToken = 'mockUserToken'; + const mockUserData: UserData = { + role: 'mockRole', + email: 'mockEmail', + name: 'mockName', + token: '', + password: 'mockPassword', + data: { + yearofbirth: 1990, + gender: 'male', + profession: 'developer', + education: 'bachelor', + workinghours: 'full-time', + householdincome: 'above-average' + } + }; + + it('should add a new user successfully', async () => { + users.set({}); + + await addUser(mockUserToken, mockUserData); + + expect(get(users)[mockUserToken]).toEqual({ + ...mockUserData, + token: mockUserToken + }); + }); + + it('should not add a user with an existing token', async () => { + users.set({ mockUserToken: mockUserData }); + + try { + await addUser(mockUserToken, mockUserData); + } catch (error: Error | unknown) { + expect((error as Error).message).toBe('User with this token already exists'); + } + }); + + it('should remove a user successfully', async () => { + users.set({ mockUserToken: mockUserData }); + + await removeUser(mockUserToken); + + expect(get(users)[mockUserToken]).toBeUndefined(); + }); + + it('should not remove a user with a non-existing token', async () => { + users.set({ mockUserToken: mockUserData }); + + try { + await removeUser('notdefined'); + } catch (error: Error | unknown) { + expect((error as Error).message).toBe('User with this token does not exist'); + } + }); +}); diff --git a/src/lib/stores/userData.ts b/src/lib/stores/userStore.ts similarity index 84% rename from src/lib/stores/userData.ts rename to src/lib/stores/userStore.ts index fdec63a1..5d5878ea 100644 --- a/src/lib/stores/userData.ts +++ b/src/lib/stores/userStore.ts @@ -40,7 +40,7 @@ const users = writable(userlist); async function addUser(userToken: string, userData: UserData) { users.update((userlist) => { if (userToken in userlist) { - // raise some error + throw new Error('User with this token already exists'); } else { userData['token'] = userToken; userlist[userToken] = userData; @@ -53,9 +53,11 @@ async function removeUser(userToken: string) { users.update((userlist) => { if (userToken in userlist) { delete userlist[userToken]; + } else { + throw new Error('User with this token does not exist'); } return userlist; }); } -export { addUser, removeUser, users }; +export { addUser, removeUser, users, type UserData, type UserList };