featuredContent.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { ApolloServer } from 'apollo-server-express'
  2. import { Mongoose } from 'mongoose'
  3. import { Aggregates } from '../src/types'
  4. import { createMutationFn, createQueryFn, MutationFn, QueryFn } from './helpers'
  5. import { buildAggregates, connectMongoose, createServer } from '../src/server'
  6. import {
  7. GET_ALL_CATEGORIES_FEATURED_VIDEOS,
  8. GET_CATEGORY_FEATURED_VIDEOS,
  9. GET_VIDEO_HERO,
  10. GetAllCategoriesFeaturedVideos,
  11. GetCategoryFeaturedVideos,
  12. GetCategoryFeaturedVideosArgs,
  13. GetVideoHero,
  14. SET_CATEGORY_FEATURED_VIDEOS,
  15. SET_VIDEO_HERO,
  16. SetCategoryFeaturedVideos,
  17. SetCategoryFeaturedVideosArgs,
  18. SetVideoHero,
  19. SetVideoHeroArgs,
  20. } from './queries/featuredContent'
  21. import {
  22. DEFAULT_FEATURED_CONTENT_DOC,
  23. FeaturedContentModel,
  24. FeaturedVideo,
  25. VideoHero,
  26. } from '../src/models/FeaturedContent'
  27. describe('Featured content resolver', () => {
  28. let server: ApolloServer
  29. let mongoose: Mongoose
  30. let aggregates: Aggregates
  31. let query: QueryFn
  32. let mutate: MutationFn
  33. beforeEach(async () => {
  34. mongoose = await connectMongoose(process.env.MONGO_URL!)
  35. aggregates = await buildAggregates()
  36. server = await createServer(mongoose, aggregates)
  37. await server.start()
  38. query = createQueryFn(server)
  39. mutate = createMutationFn(server)
  40. })
  41. afterEach(async () => {
  42. await server.stop()
  43. await FeaturedContentModel.deleteMany({})
  44. await mongoose.disconnect()
  45. })
  46. const getVideoHero = async () => {
  47. const result = await query<GetVideoHero>({
  48. query: GET_VIDEO_HERO,
  49. })
  50. expect(result.errors).toBeUndefined()
  51. return result.data?.videoHero
  52. }
  53. const getCategoryFeaturedVideos = async (categoryId: string) => {
  54. const result = await query<GetCategoryFeaturedVideos, GetCategoryFeaturedVideosArgs>({
  55. query: GET_CATEGORY_FEATURED_VIDEOS,
  56. variables: { categoryId },
  57. })
  58. expect(result.errors).toBeUndefined()
  59. return result.data?.categoryFeaturedVideos
  60. }
  61. const getAllCategoriesFeaturedVideos = async () => {
  62. const result = await query<GetAllCategoriesFeaturedVideos>({
  63. query: GET_ALL_CATEGORIES_FEATURED_VIDEOS,
  64. })
  65. expect(result.errors).toBeUndefined()
  66. return result.data?.allCategoriesFeaturedVideos
  67. }
  68. it("should return default video hero if it wasn't set", async () => {
  69. const videoHero = await getVideoHero()
  70. expect(videoHero).toEqual(DEFAULT_FEATURED_CONTENT_DOC.videoHero)
  71. })
  72. it('should return empty array of featured videos for unknown category id', async () => {
  73. const featuredVideos = await getCategoryFeaturedVideos('1')
  74. expect(featuredVideos).toHaveLength(0)
  75. })
  76. it('should return empty array for list of all categories with featured videos', async () => {
  77. const allCategoriesFeaturedVideos = await getAllCategoriesFeaturedVideos()
  78. expect(allCategoriesFeaturedVideos).toHaveLength(0)
  79. })
  80. it('should set video hero', async () => {
  81. const newVideoHero: VideoHero = {
  82. videoId: '1111',
  83. heroTitle: 'Hello darkness my old friend',
  84. heroVideoCutUrl: 'example_url',
  85. heroPosterUrl: 'example_url_2',
  86. }
  87. await mutate<SetVideoHero, SetVideoHeroArgs>({
  88. mutation: SET_VIDEO_HERO,
  89. variables: { newVideoHero },
  90. })
  91. const videoHero = await getVideoHero()
  92. expect(videoHero).toEqual(newVideoHero)
  93. })
  94. it('should set featured videos for a given category', async () => {
  95. const newFeaturedVideos: FeaturedVideo[] = [
  96. { videoId: '1', videoCutUrl: 'test_url' },
  97. { videoId: '2', videoCutUrl: 'another_url' },
  98. ]
  99. await mutate<SetCategoryFeaturedVideos, SetCategoryFeaturedVideosArgs>({
  100. mutation: SET_CATEGORY_FEATURED_VIDEOS,
  101. variables: { categoryId: '3', videos: newFeaturedVideos },
  102. })
  103. const featuredVideos = await getCategoryFeaturedVideos('3')
  104. expect(featuredVideos).toEqual(newFeaturedVideos)
  105. })
  106. it('should return all categories that have featured videos set', async () => {
  107. const category1FeaturedVideos: FeaturedVideo[] = [
  108. { videoId: '1', videoCutUrl: 'test_url' },
  109. { videoId: '2', videoCutUrl: 'another_url' },
  110. ]
  111. const category2FeaturedVideos: FeaturedVideo[] = [
  112. { videoId: '3', videoCutUrl: 'url_test' },
  113. { videoId: '4', videoCutUrl: 'url_another' },
  114. ]
  115. await mutate<SetCategoryFeaturedVideos, SetCategoryFeaturedVideosArgs>({
  116. mutation: SET_CATEGORY_FEATURED_VIDEOS,
  117. variables: { categoryId: '1', videos: category1FeaturedVideos },
  118. })
  119. await mutate<SetCategoryFeaturedVideos, SetCategoryFeaturedVideosArgs>({
  120. mutation: SET_CATEGORY_FEATURED_VIDEOS,
  121. variables: { categoryId: '2', videos: category2FeaturedVideos },
  122. })
  123. const allCategoriesFeaturedVideos = await getAllCategoriesFeaturedVideos()
  124. expect(allCategoriesFeaturedVideos).toEqual([
  125. {
  126. categoryId: '1',
  127. videos: category1FeaturedVideos,
  128. },
  129. {
  130. categoryId: '2',
  131. videos: category2FeaturedVideos,
  132. },
  133. ])
  134. })
  135. })