Skip to content

Commit

Permalink
work on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaHaWo committed Aug 27, 2024
1 parent 98efe89 commit 0e13f41
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
Empty file.
63 changes: 63 additions & 0 deletions src/lib/stores/contentStore.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {});
});
36 changes: 30 additions & 6 deletions src/lib/stores/contentStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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');

Check failure on line 44 in src/lib/stores/contentStore.ts

View workflow job for this annotation

GitHub Actions / unit-tests

src/lib/stores/contentStore.test.ts > test_normal_functionality > should remove content from the contentstore successfully

Error: No such key in the contentstore ❯ Module.removeContent src/lib/stores/contentStore.ts:44:9 ❯ src/lib/stores/contentStore.test.ts:45:9
}
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
};
5 changes: 0 additions & 5 deletions src/lib/stores/userStore.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down

0 comments on commit 0e13f41

Please sign in to comment.