Skip to content

Commit

Permalink
finish ts stuff, add test files, add tests for userstore
Browse files Browse the repository at this point in the history
  • Loading branch information
MaHaWo committed Aug 27, 2024
1 parent 597bd29 commit 98efe89
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
Empty file.
File renamed without changes.
Empty file.
66 changes: 66 additions & 0 deletions src/lib/stores/userStore.test.ts
Original file line number Diff line number Diff line change
@@ -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');
}
});
});
6 changes: 4 additions & 2 deletions src/lib/stores/userData.ts → src/lib/stores/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };

0 comments on commit 98efe89

Please sign in to comment.