From d3b6cebe70e2015004be45d8f6be8afd808a33b5 Mon Sep 17 00:00:00 2001 From: aelassas Date: Sun, 18 Feb 2024 08:23:00 +0100 Subject: [PATCH] Update notification.test.ts --- api/tests/TestHelper.ts | 2 +- api/tests/notification.test.ts | 65 ++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/api/tests/TestHelper.ts b/api/tests/TestHelper.ts index 506d11f1..6d06099a 100644 --- a/api/tests/TestHelper.ts +++ b/api/tests/TestHelper.ts @@ -14,7 +14,7 @@ const USER_EMAIL = `user.${uuid()}@test.movinin.io` export const PASSWORD = 'Un1tTest5' export const LANGUAGE = 'en' export const PAGE = 1 -export const SIZE = 1 +export const SIZE = 30 let ADMIN_USER_ID: string let USER_ID: string diff --git a/api/tests/notification.test.ts b/api/tests/notification.test.ts index af5d3c2b..833afe17 100644 --- a/api/tests/notification.test.ts +++ b/api/tests/notification.test.ts @@ -1,10 +1,15 @@ import 'dotenv/config' +import request from 'supertest' import * as DatabaseHelper from '../src/common/DatabaseHelper' import * as TestHelper from './TestHelper' import Notification from '../src/models/Notification' import NotificationCounter from '../src/models/NotificationCounter' +import app from '../src/app' +import * as env from '../src/config/env.config' let ADMIN_USER_ID: string +let NOTIFICATION1_ID: string +let NOTIFICATION2_ID: string // // Connecting and initializing the database before running the test suite @@ -17,8 +22,10 @@ beforeAll(async () => { // create admin user notifications and notification counter let notification = new Notification({ user: ADMIN_USER_ID, message: 'Message 1' }) await notification.save() + NOTIFICATION1_ID = notification.id notification = new Notification({ user: ADMIN_USER_ID, message: 'Message 2' }) await notification.save() + NOTIFICATION2_ID = notification.id const notificationCounter = new NotificationCounter({ user: ADMIN_USER_ID, count: 2 }) await notificationCounter.save() } @@ -45,7 +52,12 @@ describe('GET /api/notification-counter/:userId', () => { it('should get notification counter', async () => { const token = await TestHelper.signinAsAdmin() - // TODO + const res = await request(app) + .get(`/api/notification-counter/${ADMIN_USER_ID}`) + .set(env.X_ACCESS_TOKEN, token) + + expect(res.statusCode).toBe(200) + expect(res.body.count).toBe(2) await TestHelper.signout(token) }) @@ -55,7 +67,12 @@ describe('GET /api/notifications/:userId/:page/:size', () => { it('should get notifications', async () => { const token = await TestHelper.signinAsAdmin() - // TODO + const res = await request(app) + .get(`/api/notifications/${ADMIN_USER_ID}/${TestHelper.PAGE}/${TestHelper.SIZE}`) + .set(env.X_ACCESS_TOKEN, token) + + expect(res.statusCode).toBe(200) + expect(res.body[0].resultData.length).toBe(2) await TestHelper.signout(token) }) @@ -65,7 +82,17 @@ describe('POST /api/mark-notifications-as-read/:userId', () => { it('should mark notifications as read', async () => { const token = await TestHelper.signinAsAdmin() - // TODO + const payload = { ids: [NOTIFICATION1_ID, NOTIFICATION2_ID] } + + const res = await request(app) + .post(`/api/mark-notifications-as-read/${ADMIN_USER_ID}`) + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + + expect(res.statusCode).toBe(200) + + const counter = await NotificationCounter.findOne({ user: ADMIN_USER_ID }) + expect(counter?.count).toBe(0) await TestHelper.signout(token) }) @@ -75,17 +102,43 @@ describe('POST /api/mark-notifications-as-unread/:userId', () => { it('should mark notifications as unread', async () => { const token = await TestHelper.signinAsAdmin() - // TODO + const payload = { ids: [NOTIFICATION1_ID, NOTIFICATION2_ID] } + + const res = await request(app) + .post(`/api/mark-notifications-as-unread/${ADMIN_USER_ID}`) + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + + expect(res.statusCode).toBe(200) + + const counter = await NotificationCounter.findOne({ user: ADMIN_USER_ID }) + expect(counter?.count).toBe(2) await TestHelper.signout(token) }) }) -describe('DELETE /api/delete-notifications/:userId', () => { +describe('POST /api/delete-notifications/:userId', () => { it('should delete notifications', async () => { const token = await TestHelper.signinAsAdmin() - // TODO + let notifications = await Notification.find({ user: ADMIN_USER_ID }) + expect(notifications.length).toBe(2) + + const payload = { ids: [NOTIFICATION1_ID, NOTIFICATION2_ID] } + + const res = await request(app) + .post(`/api/delete-notifications/${ADMIN_USER_ID}`) + .set(env.X_ACCESS_TOKEN, token) + .send(payload) + + expect(res.statusCode).toBe(200) + + notifications = await Notification.find({ user: ADMIN_USER_ID }) + expect(notifications.length).toBe(0) + + const counter = await NotificationCounter.findOne({ user: ADMIN_USER_ID }) + expect(counter?.count).toBe(0) await TestHelper.signout(token) })