I have written a test which should check if function working properly, but it is working with instance of database column (user).
I am not sure why function not even called.
here is the test, i thought it should call addPostToUser properly because i define postOwner as User instance but it is not working
describe('addPostToUser', () => {
it('should add post to user', async () => {
const postId = 'postid';
const postOwner: User = {
id: 'userid',
name: 'User One',
email: '[email protected]',
posts: [],
} as User;
await userService.addPostToUser(postOwner.id, postId);
expect(postOwner.posts).toContain(postId);
});
});
here is the function
async addPostToUser(userId: string, postId: string): Promise<void> {
const user = await User.findOne({ where: { id: userId } });
if (user) {
await User.update(
{ posts: [...(user.posts || []), postId] },
{ where: { id: userId } },
);
}
}
here is results
● UserModule › userService › addPostToUser › should add post to user
expect(received).toContain(expected) // indexOf
Expected value: "postid"
Received array: []
101 | await userService.addPostToUser(postOwner.id, postId);
102 |
> 103 | expect(postOwner.posts).toContain(postId);
| ^
104 | });
105 | });
106 | });