Skip to content

Commit

Permalink
Update notification.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Feb 18, 2024
1 parent beebdbb commit d3b6ceb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/tests/TestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 59 additions & 6 deletions api/tests/notification.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
Expand All @@ -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)
})
Expand All @@ -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)
})
Expand All @@ -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)
})
Expand All @@ -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)
})
Expand Down

0 comments on commit d3b6ceb

Please sign in to comment.