Skip to content

Commit

Permalink
chore: refactor notification tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PooyaRaki committed Nov 4, 2024
1 parent 88aef6f commit a28bfab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { Builder } from '@/__tests__/builder';
import { getAddress } from 'viem';
import type { UpsertSubscriptionsDto } from '@/domain/notifications/v2/entities/upsert-subscriptions.dto.entity';
import { DeviceType } from '@/domain/notifications/v2/entities/device-type.entity';
import type { Uuid } from '@/domain/notifications/v2/entities/uuid.entity';
import { NotificationType } from '@/domain/notifications/v2/entities/notification-type.entity';
import type { UUID } from 'crypto';

export function upsertSubscriptionsDtoBuilder(): IBuilder<UpsertSubscriptionsDto> {
return new Builder<UpsertSubscriptionsDto>()
.with('cloudMessagingToken', faker.string.alphanumeric({ length: 10 }))
.with('deviceType', faker.helpers.arrayElement(Object.values(DeviceType)))
.with('deviceUuid', faker.string.uuid() as Uuid)
.with('deviceUuid', faker.string.uuid() as UUID)
.with(
'safes',
Array.from(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ describe('NotificationsRepositoryV2', () => {
*
* @async
* @function createDatabaseConnection
* @returns {Promise<void>} Resolves when migrations are complete.
* @returns {Promise<PostgresDatabaseService>} Returns an instance of PostgresDatabaseService
*/
async function createDatabaseConnection(): Promise<void> {
postgresDatabaseService = new PostgresDatabaseService(
async function createDatabaseConnection(): Promise<PostgresDatabaseService> {
const databaseService = new PostgresDatabaseService(
mockLoggingService,
dataSource,
);
await postgresDatabaseService.initializeDatabaseConnection();
await databaseService.initializeDatabaseConnection();

return databaseService;
}

/**
Expand All @@ -113,11 +115,15 @@ describe('NotificationsRepositoryV2', () => {
* services (logging, database, and configuration) and executes the migration
* process.
*
* @param {postgresDatabaseService} postgresDatabaseService The postgres database service instance
*
* @async
* @function migrateDatabase
* @returns {Promise<void>} Resolves when migrations are complete.
*/
async function migrateDatabase(): Promise<void> {
async function migrateDatabase(
postgresDatabaseService: PostgresDatabaseService,
): Promise<void> {
const migrator = new DatabaseMigrator(
mockLoggingService,
postgresDatabaseService,
Expand Down Expand Up @@ -168,8 +174,8 @@ describe('NotificationsRepositoryV2', () => {

beforeAll(async () => {
await createTestDatabase();
await createDatabaseConnection();
await migrateDatabase();
postgresDatabaseService = await createDatabaseConnection();
await migrateDatabase(postgresDatabaseService);

notificationsRepositoryService = new NotificationsRepositoryV2(
mockPushNotificationsApi,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/notifications/v1/notifications.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ describe('Notifications Controller (Unit)', () => {

// @TODO Remove NotificationModuleV2 after all clients have migrated and compatibility is no longer needed.
// We call V2 as many times as we have a registration with at least one safe
const safedRegistrationsWithSafe =
const safeRegistrationsWithSafe =
registerDeviceDto.safeRegistrations.filter(
(safeRegistration) => safeRegistration.safes.length > 0,
);

expect(notificationServiceV2.upsertSubscriptions).toHaveBeenCalledTimes(
safedRegistrationsWithSafe.length,
safeRegistrationsWithSafe.length,
);

for (const [
Expand Down

0 comments on commit a28bfab

Please sign in to comment.