ProcessUserRequest.spec.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'reflect-metadata'
  2. import { DomainEventPublisherInterface, ExitDiscountApplyRequestedEvent } from '@standardnotes/domain-events'
  3. import { UserRequestType } from '@standardnotes/common'
  4. import { DomainEventFactoryInterface } from '../../Event/DomainEventFactoryInterface'
  5. import { UserSubscription } from '../../Subscription/UserSubscription'
  6. import { UserSubscriptionRepositoryInterface } from '../../Subscription/UserSubscriptionRepositoryInterface'
  7. import { ProcessUserRequest } from './ProcessUserRequest'
  8. describe('ProcessUserRequest', () => {
  9. let userSubscriptionRepository: UserSubscriptionRepositoryInterface
  10. let domainEventFactory: DomainEventFactoryInterface
  11. let domainEventPublisher: DomainEventPublisherInterface
  12. const createUseCase = () =>
  13. new ProcessUserRequest(userSubscriptionRepository, domainEventFactory, domainEventPublisher)
  14. beforeEach(() => {
  15. userSubscriptionRepository = {} as jest.Mocked<UserSubscriptionRepositoryInterface>
  16. userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue({
  17. cancelled: true,
  18. } as jest.Mocked<UserSubscription>)
  19. domainEventFactory = {} as jest.Mocked<DomainEventFactoryInterface>
  20. domainEventFactory.createExitDiscountApplyRequestedEvent = jest
  21. .fn()
  22. .mockReturnValue({} as jest.Mocked<ExitDiscountApplyRequestedEvent>)
  23. domainEventPublisher = {} as jest.Mocked<DomainEventPublisherInterface>
  24. domainEventPublisher.publish = jest.fn()
  25. })
  26. it('should not process unsupported requests', async () => {
  27. expect(
  28. await createUseCase().execute({
  29. userEmail: 'test@test.te',
  30. userUuid: '1-2-3',
  31. requestType: 'foobar' as UserRequestType,
  32. }),
  33. ).toEqual({
  34. success: false,
  35. })
  36. expect(domainEventPublisher.publish).not.toHaveBeenCalled()
  37. })
  38. it('should not process uncancelled subscriptions', async () => {
  39. userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue({} as jest.Mocked<UserSubscription>)
  40. expect(
  41. await createUseCase().execute({
  42. userEmail: 'test@test.te',
  43. userUuid: '1-2-3',
  44. requestType: UserRequestType.ExitDiscount,
  45. }),
  46. ).toEqual({
  47. success: false,
  48. })
  49. expect(domainEventPublisher.publish).not.toHaveBeenCalled()
  50. })
  51. it('should not process non existing subscriptions', async () => {
  52. userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue(null)
  53. expect(
  54. await createUseCase().execute({
  55. userEmail: 'test@test.te',
  56. userUuid: '1-2-3',
  57. requestType: UserRequestType.ExitDiscount,
  58. }),
  59. ).toEqual({
  60. success: false,
  61. })
  62. expect(domainEventPublisher.publish).not.toHaveBeenCalled()
  63. })
  64. it('should publish an exit discount apply requested event', async () => {
  65. expect(
  66. await createUseCase().execute({
  67. userEmail: 'test@test.te',
  68. userUuid: '1-2-3',
  69. requestType: UserRequestType.ExitDiscount,
  70. }),
  71. ).toEqual({
  72. success: true,
  73. })
  74. expect(domainEventPublisher.publish).toHaveBeenCalled()
  75. })
  76. })