Skip to content

Commit

Permalink
[core] 🧪 tests: stock level notification emitting when there is alrea…
Browse files Browse the repository at this point in the history
…dy a notificitaion for the same product
  • Loading branch information
JohnPetros committed Nov 11, 2024
1 parent 10f5e8f commit fd285fd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
28 changes: 16 additions & 12 deletions packages/core/__tests__/fakers/stock-level-notifications-faker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { fakerPT_BR as faker } from '@faker-js/faker';
import { StockLevelNotification } from '../../src/domain/entities';
import type { StockLevelNotificationDto } from '../../src/dtos';
import { fakerPT_BR as faker } from '@faker-js/faker'
import { StockLevelNotification } from '../../src/domain/entities'
import type { StockLevelNotificationDto } from '../../src/dtos'

export class StockLevelNotificationsFaker {
static fakeStockLevelNotification(baseDto?: Partial<StockLevelNotificationDto>) {
return StockLevelNotification.create(StockLevelNotificationsFaker.fakeDto(baseDto));
static fake(baseDto?: Partial<StockLevelNotificationDto>) {
return StockLevelNotification.create(StockLevelNotificationsFaker.fakeDto(baseDto))
}
static fakeDto(baseDto?: Partial<StockLevelNotificationDto>): StockLevelNotificationDto {
const fixedDate = new Date('2023-01-01T00:00:00.000Z');
static fakeDto(
baseDto?: Partial<StockLevelNotificationDto>,
): StockLevelNotificationDto {
const fixedDate = new Date('2023-01-01T00:00:00.000Z')
return {
id: faker.string.uuid(),
companyId: faker.string.uuid(),
Expand All @@ -16,18 +18,20 @@ export class StockLevelNotificationsFaker {
name: faker.commerce.productName(),
code: faker.string.uuid(),
},
createdAt: baseDto?.createdAt ?? fixedDate,
sentAt: baseDto?.sentAt ?? fixedDate,
...baseDto,
};
}
}

static fakeMany(count = 10, baseDto?: Partial<StockLevelNotificationDto>) {
return Array.from({ length: count }).map(() =>
StockLevelNotificationsFaker.fakeStockLevelNotification(baseDto),
);
StockLevelNotificationsFaker.fake(baseDto),
)
}

static fakeManyDto(count = 10, baseDto?: Partial<StockLevelNotificationDto>) {
return Array.from({ length: count }).map(() => StockLevelNotificationsFaker.fakeDto(baseDto));
return Array.from({ length: count }).map(() =>
StockLevelNotificationsFaker.fakeDto(baseDto),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ export class NotificationsRepositoryMock implements INotificationsRepository {
)
}
}

async findStockLevelNotificationByProduct(
productId: string,
): Promise<StockLevelNotification | null> {
return (
this.stockLevelNotifications.find(
(notification) => notification.product.id === productId,
) ?? null
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('DeleteNotificationUseCase', () => {
const notificationsRepository = new NotificationsRepositoryMock()
const useCase = new DeleteNotificationUseCase(notificationsRepository)

const fakeNotification = StockLevelNotificationsFaker.fakeStockLevelNotification()
const fakeNotification = StockLevelNotificationsFaker.fake()
await notificationsRepository.addStockLevelNotification(fakeNotification)

await expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
} from '../../../../__tests__/mocks/repositories'
import { SendStockLevelNotificationsUseCase } from '../send-stock-level-notification-use-case'
import { NotFoundError } from '../../../errors'
import { BatchesFaker, ProductsFaker } from '../../../../__tests__/fakers'
import {
BatchesFaker,
ProductsFaker,
StockLevelNotificationsFaker,
} from '../../../../__tests__/fakers'
import { NotificationsSocketMock } from '../../../../__tests__/mocks/sockets'

let notificationsRepository: NotificationsRepositoryMock
Expand Down Expand Up @@ -86,4 +90,33 @@ describe('Send stock level notification use case', () => {
fakeDangerLevelProduct.companyId,
)
})

it('should not emit stock level notification if there is already a notification for the same product', async () => {
const fakeDangerLevelProduct = ProductsFaker.fake({ batches: [] })

productsRepository.add(fakeDangerLevelProduct)
productsRepository.add(
ProductsFaker.fake({
minimumStock: 10,
batches: [BatchesFaker.fakeDto({ itemsCount: 50 })],
companyId: fakeDangerLevelProduct.companyId,
}),
)

notificationsRepository.addStockLevelNotification(
StockLevelNotificationsFaker.fake({
product: {
id: fakeDangerLevelProduct.id,
name: fakeDangerLevelProduct.name,
code: fakeDangerLevelProduct.code,
},
}),
)

expect(notificationsSocket.emittedStockLevelNotifications).toHaveLength(0)

await useCase.execute(fakeDangerLevelProduct.id)

expect(notificationsSocket.emittedStockLevelNotifications).toHaveLength(0)
})
})

0 comments on commit fd285fd

Please sign in to comment.