Skip to content

Commit

Permalink
Add notification.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Feb 17, 2024
1 parent 6c2abac commit 33870e8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/tests/TestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const LANGUAGE = 'en'
export const PAGE = 1
export const SIZE = 1

let ADMIN_USER_ID: string
let USER_ID: string

export async function initializeDatabase() {
const salt = await bcrypt.genSalt(10)
const passwordHash = await bcrypt.hash(PASSWORD, salt)
Expand All @@ -31,11 +34,21 @@ export async function initializeDatabase() {
let user = new User(body)
await user.save()
expect(user.id).toBeDefined()
ADMIN_USER_ID = user.id

// user
user = new User({ ...body, fullName: 'user', email: USER_EMAIL, type: movininTypes.UserType.User })
await user.save()
expect(user.id).toBeDefined()
USER_ID = user.id
}

export function getAdminUserId() {
return ADMIN_USER_ID
}

export function getUserId() {
return USER_ID
}

export async function clearDatabase() {
Expand Down
92 changes: 92 additions & 0 deletions api/tests/notification.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'dotenv/config'
import * as DatabaseHelper from '../src/common/DatabaseHelper'
import * as TestHelper from './TestHelper'
import Notification from '../src/models/Notification'
import NotificationCounter from '../src/models/NotificationCounter'

let ADMIN_USER_ID: string

//
// Connecting and initializing the database before running the test suite
//
beforeAll(async () => {
if (await DatabaseHelper.Connect(false)) {
await TestHelper.initializeDatabase()
ADMIN_USER_ID = TestHelper.getAdminUserId()

// create admin user notifications and notification counter
let notification = new Notification({ user: ADMIN_USER_ID, message: 'Message 1' })
await notification.save()
notification = new Notification({ user: ADMIN_USER_ID, message: 'Message 2' })
await notification.save()
const notificationCounter = new NotificationCounter({ user: ADMIN_USER_ID, count: 2 })
await notificationCounter.save()
}
})

//
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
await TestHelper.clearDatabase()

// clear admin user notifications and notification counter
await Notification.deleteMany({ user: ADMIN_USER_ID })
await NotificationCounter.deleteOne({ user: ADMIN_USER_ID })

await DatabaseHelper.Close(false)
})

//
// Unit tests
//

describe('GET /api/notification-counter/:userId', () => {
it('should get notification counter', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO

await TestHelper.signout(token)
})
})

describe('GET /api/notifications/:userId/:page/:size', () => {
it('should get notifications', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO

await TestHelper.signout(token)
})
})

describe('POST /api/mark-notifications-as-read/:userId', () => {
it('should mark notifications as read', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO

await TestHelper.signout(token)
})
})

describe('POST /api/mark-notifications-as-unread/:userId', () => {
it('should mark notifications as unread', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO

await TestHelper.signout(token)
})
})

describe('DELETE /api/delete-notifications/:userId', () => {
it('should delete notifications', async () => {
const token = await TestHelper.signinAsAdmin()

// TODO

await TestHelper.signout(token)
})
})

0 comments on commit 33870e8

Please sign in to comment.