diff --git a/src/lib/stores/childrenStore.test.ts b/src/lib/stores/childrenStore.test.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/src/lib/stores/contentStore.test.ts b/src/lib/stores/contentStore.test.ts index e69de29b..cadeeeee 100644 --- a/src/lib/stores/contentStore.test.ts +++ b/src/lib/stores/contentStore.test.ts @@ -0,0 +1,63 @@ +import { get } from 'svelte/store'; +import { describe, expect, it } from 'vitest'; +import { + addContent, + content, + removeContent, + type ChildrenSurvey, + type ContentList, + type RegistrationForm +} from './contentStore'; + +describe('test_normal_functionality', async () => { + const mockChildSurvey: ChildrenSurvey = { + can_do_this: 'incomplete', + can_do_that: 'fully', + can_do_the_other: 'no' + }; + + const mockRegistrationForm: RegistrationForm = { + name: 'Tom', + gender: 'male' + }; + + const mockContentList: ContentList = { + childrenSurveys: { + testsurvey: mockChildSurvey + }, + registrationForms: { + testregistration: mockRegistrationForm + } + }; + + it('should add content successfully when key is not yet there', async () => { + content.set(mockContentList); + + await addContent('childrenSurveys', 'dummySurvey', mockChildSurvey); + await addContent('registrationForms', 'childRegistration', mockRegistrationForm); + + expect(get(content)['childrenSurveys']['dummySurvey']).toEqual(mockChildSurvey); + expect(get(content)['registrationForms']['childRegistration']).toEqual(mockRegistrationForm); + }); + + it('should remove content from the contentstore successfully', async () => { + content.set(mockContentList); + await removeContent('childrenSurveys', 'testsurvey'); + // await removeContent('registrationForms', 'childRegistration'); + + expect(get(content)['childrenSurveys']['testsurvey']).toBeUndefined(); + // expect(get(content)['registrationForms']['childRegistration']).toBeUndefined(); + }); + + // it('throw error when adding an element with an existing key', async () => { + // content.set(mockContentList); + + // try { + // await removeContent('childrenSurveys', 'notthere'); + // } catch (error: Error | unknown) { + // expect((error as Error).message).toBe('No such key in the contentstore'); + // } + // }); + + it('throw error when removing an element with a non-existing key', async () => {}); +}); diff --git a/src/lib/stores/contentStore.ts b/src/lib/stores/contentStore.ts index 0f7e5695..89ac174e 100644 --- a/src/lib/stores/contentStore.ts +++ b/src/lib/stores/contentStore.ts @@ -5,13 +5,13 @@ interface ChildrenSurvey { [name: string]: unknown; } -interface RegistrationForms { +interface RegistrationForm { [name: string]: unknown; } interface ContentList { childrenSurveys: ChildrenSurvey; - registrationForms: RegistrationForms; + registrationForms: RegistrationForm; } const contentlist: ContentList = { @@ -23,18 +23,42 @@ const contentlist: ContentList = { const content = writable(contentlist); // functions to add and remove stuff from the store -function addContent(type: string, key: string) { +async function addContent( + type: string, + key: string, + new_content: ChildrenSurvey | RegistrationForm +) { content.update((contentlist: ContentList) => { - contentlist[type as keyof ContentList][key] = content; + contentlist[type as keyof ContentList][key] = new_content; return contentlist; }); } -function removeContent(type: string, key: string) { +async function removeContent(type: string, key: string) { + if (!(type in contentlist)) { + throw new Error('No such register in the contentstore'); + } + console.log('removeContent', type, key, Object.keys(contentlist[type])); + + if (!(key in contentlist[type as keyof ContentList])) { + throw new Error('No such key in the contentstore'); + } content.update((contentlist) => { delete contentlist[type as keyof ContentList][key]; return contentlist; }); } -export { addContent, content, removeContent }; +async function dummyAdd(a: number, b: number) { + return a + b; +} + +export { + addContent, + content, + dummyAdd, + removeContent, + type ChildrenSurvey, + type ContentList, + type RegistrationForm +}; diff --git a/src/lib/stores/userStore.test.ts b/src/lib/stores/userStore.test.ts index a1c0fd87..d135342a 100644 --- a/src/lib/stores/userStore.test.ts +++ b/src/lib/stores/userStore.test.ts @@ -1,12 +1,7 @@ -// 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 = {