featuredContent.test.ts 5.0 KB

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