123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import { ApolloServer } from 'apollo-server-express'
- import { Mongoose } from 'mongoose'
- import { Aggregates } from '../src/types'
- import { createMutationFn, createQueryFn, MutationFn, QueryFn } from './helpers'
- import { buildAggregates, connectMongoose, createServer } from '../src/server'
- import {
- GET_ALL_CATEGORIES_FEATURED_VIDEOS,
- GET_CATEGORY_FEATURED_VIDEOS,
- GET_VIDEO_HERO,
- GetAllCategoriesFeaturedVideos,
- GetCategoryFeaturedVideos,
- GetCategoryFeaturedVideosArgs,
- GetVideoHero,
- SET_CATEGORY_FEATURED_VIDEOS,
- SET_VIDEO_HERO,
- SetCategoryFeaturedVideos,
- SetCategoryFeaturedVideosArgs,
- SetVideoHero,
- SetVideoHeroArgs,
- GetAllCategoriesFeaturedVideosArgs,
- } from './queries/featuredContent'
- import {
- DEFAULT_FEATURED_CONTENT_DOC,
- FeaturedContentModel,
- FeaturedVideo,
- VideoHero,
- } from '../src/models/FeaturedContent'
- describe('Featured content resolver', () => {
- let server: ApolloServer
- let mongoose: Mongoose
- let aggregates: Aggregates
- let query: QueryFn
- let mutate: MutationFn
- beforeEach(async () => {
- mongoose = await connectMongoose(process.env.MONGO_URL!)
- aggregates = await buildAggregates()
- server = await createServer(mongoose, aggregates, process.env.ORION_QUERY_NODE_URL!)
- await server.start()
- query = createQueryFn(server)
- mutate = createMutationFn(server)
- })
- afterEach(async () => {
- await server.stop()
- await FeaturedContentModel.deleteMany({})
- await mongoose.disconnect()
- })
- const getVideoHero = async () => {
- const result = await query<GetVideoHero>({
- query: GET_VIDEO_HERO,
- })
- expect(result.errors).toBeUndefined()
- return result.data?.videoHero
- }
- const getCategoryFeaturedVideos = async (categoryId: string) => {
- const result = await query<GetCategoryFeaturedVideos, GetCategoryFeaturedVideosArgs>({
- query: GET_CATEGORY_FEATURED_VIDEOS,
- variables: { categoryId },
- })
- expect(result.errors).toBeUndefined()
- return result.data?.categoryFeaturedVideos
- }
- const getAllCategoriesFeaturedVideos = async (videosLimit: number) => {
- const result = await query<GetAllCategoriesFeaturedVideos, GetAllCategoriesFeaturedVideosArgs>({
- query: GET_ALL_CATEGORIES_FEATURED_VIDEOS,
- variables: { videosLimit },
- })
- expect(result.errors).toBeUndefined()
- return result.data?.allCategoriesFeaturedVideos
- }
- it("should return default video hero if it wasn't set", async () => {
- const videoHero = await getVideoHero()
- expect(videoHero).toEqual(DEFAULT_FEATURED_CONTENT_DOC.videoHero)
- })
- it('should return empty array of featured videos for unknown category id', async () => {
- const featuredVideos = await getCategoryFeaturedVideos('1')
- expect(featuredVideos).toHaveLength(0)
- })
- it('should return empty array for list of all categories with featured videos', async () => {
- const allCategoriesFeaturedVideos = await getAllCategoriesFeaturedVideos(3)
- expect(allCategoriesFeaturedVideos).toHaveLength(0)
- })
- it('should set video hero', async () => {
- const newVideoHero: VideoHero = {
- videoId: '1111',
- heroTitle: 'Hello darkness my old friend',
- heroVideoCutUrl: 'example_url',
- heroPosterUrl: 'example_url_2',
- }
- await mutate<SetVideoHero, SetVideoHeroArgs>({
- mutation: SET_VIDEO_HERO,
- variables: { newVideoHero },
- })
- const videoHero = await getVideoHero()
- expect(videoHero).toEqual(newVideoHero)
- })
- it('should set featured videos for a given category', async () => {
- const newFeaturedVideos: FeaturedVideo[] = [
- { videoId: '1', videoCutUrl: 'test_url' },
- { videoId: '2', videoCutUrl: 'another_url' },
- ]
- await mutate<SetCategoryFeaturedVideos, SetCategoryFeaturedVideosArgs>({
- mutation: SET_CATEGORY_FEATURED_VIDEOS,
- variables: { categoryId: '3', videos: newFeaturedVideos },
- })
- const featuredVideos = await getCategoryFeaturedVideos('3')
- expect(featuredVideos).toEqual(newFeaturedVideos)
- })
- it('should return all categories that have featured videos set', async () => {
- const category1FeaturedVideos: FeaturedVideo[] = [
- { videoId: '1', videoCutUrl: 'test_url' },
- { videoId: '2', videoCutUrl: 'another_url' },
- ]
- const category2FeaturedVideos: FeaturedVideo[] = [
- { videoId: '3', videoCutUrl: 'url_test' },
- { videoId: '4', videoCutUrl: 'url_another' },
- ]
- await mutate<SetCategoryFeaturedVideos, SetCategoryFeaturedVideosArgs>({
- mutation: SET_CATEGORY_FEATURED_VIDEOS,
- variables: { categoryId: '1', videos: category1FeaturedVideos },
- })
- await mutate<SetCategoryFeaturedVideos, SetCategoryFeaturedVideosArgs>({
- mutation: SET_CATEGORY_FEATURED_VIDEOS,
- variables: { categoryId: '2', videos: category2FeaturedVideos },
- })
- const allCategoriesFeaturedVideos = await getAllCategoriesFeaturedVideos(3)
- expect(allCategoriesFeaturedVideos).toEqual([
- {
- categoryId: '1',
- categoryFeaturedVideos: category1FeaturedVideos,
- },
- {
- categoryId: '2',
- categoryFeaturedVideos: category2FeaturedVideos,
- },
- ])
- })
- })
|