UserSubscriptionService.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'reflect-metadata'
  2. import { User } from '../User/User'
  3. import { UserSubscription } from './UserSubscription'
  4. import { UserSubscriptionRepositoryInterface } from './UserSubscriptionRepositoryInterface'
  5. import { UserSubscriptionService } from './UserSubscriptionService'
  6. import { UserSubscriptionType } from './UserSubscriptionType'
  7. describe('UserSubscriptionService', () => {
  8. let userSubscriptionRepository: UserSubscriptionRepositoryInterface
  9. let regularSubscription: UserSubscription
  10. let sharedSubscription: UserSubscription
  11. let user: User
  12. const createService = () => new UserSubscriptionService(userSubscriptionRepository)
  13. beforeEach(() => {
  14. user = {
  15. uuid: '1-2-3',
  16. } as jest.Mocked<User>
  17. regularSubscription = {
  18. uuid: '1-2-3',
  19. subscriptionType: UserSubscriptionType.Regular,
  20. user: Promise.resolve(user),
  21. } as jest.Mocked<UserSubscription>
  22. sharedSubscription = {
  23. uuid: '2-3-4',
  24. subscriptionType: UserSubscriptionType.Shared,
  25. user: Promise.resolve(user),
  26. } as jest.Mocked<UserSubscription>
  27. userSubscriptionRepository = {} as jest.Mocked<UserSubscriptionRepositoryInterface>
  28. userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue(null)
  29. userSubscriptionRepository.findOneByUuid = jest.fn().mockReturnValue(null)
  30. userSubscriptionRepository.findBySubscriptionIdAndType = jest.fn().mockReturnValue([])
  31. })
  32. describe('by uuid', () => {
  33. it('should return undefined if there is no user subscription', async () => {
  34. expect(await createService().findRegularSubscriptionForUuid('1-2-3')).toEqual({
  35. regularSubscription: null,
  36. sharedSubscription: null,
  37. })
  38. })
  39. it('should return a regular subscription if the uuid corresponds to a regular subscription', async () => {
  40. userSubscriptionRepository.findOneByUuid = jest.fn().mockReturnValue(regularSubscription)
  41. expect(await createService().findRegularSubscriptionForUuid('1-2-3')).toEqual({
  42. regularSubscription,
  43. sharedSubscription: null,
  44. })
  45. })
  46. it('should return a regular subscription if the uuid corresponds to a shared subscription', async () => {
  47. userSubscriptionRepository.findOneByUuid = jest.fn().mockReturnValue(sharedSubscription)
  48. userSubscriptionRepository.findBySubscriptionIdAndType = jest.fn().mockReturnValue([regularSubscription])
  49. expect(await createService().findRegularSubscriptionForUuid('1-2-3')).toEqual({
  50. regularSubscription,
  51. sharedSubscription,
  52. })
  53. })
  54. it('should return undefined if a regular subscription is not found corresponding to the shared subscription', async () => {
  55. userSubscriptionRepository.findOneByUuid = jest.fn().mockReturnValue(sharedSubscription)
  56. userSubscriptionRepository.findBySubscriptionIdAndType = jest.fn().mockReturnValue([])
  57. expect(await createService().findRegularSubscriptionForUuid('1-2-3')).toEqual({
  58. regularSubscription: null,
  59. sharedSubscription,
  60. })
  61. })
  62. })
  63. describe('by user uuid', () => {
  64. it('should return undefined if there is no user subscription', async () => {
  65. expect(await createService().findRegularSubscriptionForUserUuid('1-2-3')).toEqual({
  66. regularSubscription: null,
  67. sharedSubscription: null,
  68. })
  69. })
  70. it('should return a regular subscription if the uuid corresponds to a regular subscription', async () => {
  71. userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue(regularSubscription)
  72. expect(await createService().findRegularSubscriptionForUserUuid('1-2-3')).toEqual({
  73. regularSubscription,
  74. sharedSubscription: null,
  75. })
  76. })
  77. it('should return a regular subscription if the uuid corresponds to a shared subscription', async () => {
  78. userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue(sharedSubscription)
  79. userSubscriptionRepository.findBySubscriptionIdAndType = jest.fn().mockReturnValue([regularSubscription])
  80. expect(await createService().findRegularSubscriptionForUserUuid('1-2-3')).toEqual({
  81. regularSubscription,
  82. sharedSubscription,
  83. })
  84. })
  85. it('should return undefined if a regular subscription is not found corresponding to the shared subscription', async () => {
  86. userSubscriptionRepository.findOneByUserUuid = jest.fn().mockReturnValue(sharedSubscription)
  87. userSubscriptionRepository.findBySubscriptionIdAndType = jest.fn().mockReturnValue([])
  88. expect(await createService().findRegularSubscriptionForUserUuid('1-2-3')).toEqual({
  89. regularSubscription: null,
  90. sharedSubscription,
  91. })
  92. })
  93. })
  94. })