Skip to content

Commit c35cf81

Browse files
committed
test: add unit test to reset login streak if lastSeen is not yesterday
1 parent be6fc42 commit c35cf81

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

server/src/user/user.service.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ describe('UserService', () => {
222222
const user = { _id: 'test-id' } as UserDocument;
223223
const yesterday = new Date();
224224
yesterday.setDate(yesterday.getDate() - 1);
225+
yesterday.setHours(0, 0, 0, 0);
225226

226227
const userData = {
227228
...user,
@@ -259,6 +260,28 @@ describe('UserService', () => {
259260
expect(result.loginStreak).toBe(1);
260261
expect(userData.save).not.toHaveBeenCalled();
261262
});
263+
264+
it('should reset loginStreak if lastSeen is not yesterday', async () => {
265+
const user = { _id: 'test-id' } as UserDocument;
266+
const twoDaysAgo = new Date();
267+
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);
268+
twoDaysAgo.setHours(0, 0, 0, 0);
269+
270+
const userData = {
271+
...user,
272+
lastSeen: twoDaysAgo,
273+
loginStreak: 5,
274+
save: jest.fn().mockResolvedValue(true),
275+
} as unknown as UserDocument;
276+
277+
jest.spyOn(service, 'findByID').mockResolvedValue(userData);
278+
279+
const result = await service.getSelfUserData(user);
280+
281+
expect(result.lastSeen).toBeInstanceOf(Date);
282+
expect(result.loginStreak).toBe(1);
283+
expect(userData.save).toHaveBeenCalled();
284+
});
262285
});
263286

264287
describe('usernameExists', () => {

0 commit comments

Comments
 (0)