From 9d938af005eaa192a12d88c03870df95463dbfbd Mon Sep 17 00:00:00 2001 From: Anandaroop Roy Date: Tue, 27 Aug 2024 17:21:01 -0400 Subject: [PATCH] feat: add a News home view section (#5946) * chore: clean up, alphabetize etc * feat: add a news section * fix: improper nesting in test --- .../v2/homeView/__tests__/HomeView.test.ts | 10 +- .../__tests__/HomeViewSection.test.ts | 135 ++++++++++++++---- src/schema/v2/homeView/articlesResolvers.ts | 27 ++++ src/schema/v2/homeView/getSectionsForUser.ts | 3 + src/schema/v2/homeView/sections.ts | 17 ++- 5 files changed, 161 insertions(+), 31 deletions(-) diff --git a/src/schema/v2/homeView/__tests__/HomeView.test.ts b/src/schema/v2/homeView/__tests__/HomeView.test.ts index db308704e2..e77ee7da40 100644 --- a/src/schema/v2/homeView/__tests__/HomeView.test.ts +++ b/src/schema/v2/homeView/__tests__/HomeView.test.ts @@ -31,12 +31,20 @@ describe("homeView", () => { }), } - it("returns requested data for each section", async () => { + it("returns every section", async () => { const { homeView } = await runQuery(query, context) expect(homeView.sectionsConnection).toMatchInlineSnapshot(` Object { "edges": Array [ + Object { + "node": Object { + "__typename": "ArticlesRailHomeViewSection", + "component": Object { + "title": "News", + }, + }, + }, Object { "node": Object { "__typename": "ActivityRailHomeViewSection", diff --git a/src/schema/v2/homeView/__tests__/HomeViewSection.test.ts b/src/schema/v2/homeView/__tests__/HomeViewSection.test.ts index aa86fa914b..4d80537778 100644 --- a/src/schema/v2/homeView/__tests__/HomeViewSection.test.ts +++ b/src/schema/v2/homeView/__tests__/HomeViewSection.test.ts @@ -477,42 +477,42 @@ describe("HomeViewSection", () => { } `) }) + }) - describe("ViewingRoomsRailHomeViewSection", () => { - it("returns correct data", async () => { - const query = gql` - { - homeView { - section(id: "home-view-section-viewing-rooms") { - __typename + describe("ViewingRoomsRailHomeViewSection", () => { + it("returns correct data", async () => { + const query = gql` + { + homeView { + section(id: "home-view-section-viewing-rooms") { + __typename - ... on ViewingRoomsRailHomeViewSection { - component { - title - } + ... on ViewingRoomsRailHomeViewSection { + component { + title } } } } - ` - - const context = { - authenticatedLoaders: { - meLoader: jest.fn().mockReturnValue({ type: "User" }), - }, } + ` - const data = await runQuery(query, context) + const context = { + authenticatedLoaders: { + meLoader: jest.fn().mockReturnValue({ type: "User" }), + }, + } - expect(data.homeView.section).toMatchInlineSnapshot(` - Object { - "__typename": "ViewingRoomsRailHomeViewSection", - "component": Object { - "title": "Viewing Rooms", - }, - } - `) - }) + const data = await runQuery(query, context) + + expect(data.homeView.section).toMatchInlineSnapshot(` + Object { + "__typename": "ViewingRoomsRailHomeViewSection", + "component": Object { + "title": "Viewing Rooms", + }, + } + `) }) }) @@ -732,4 +732,85 @@ describe("HomeViewSection", () => { `) }) }) + + describe("News", () => { + it("returns correct data", async () => { + const query = gql` + { + homeView { + section(id: "home-view-section-news") { + __typename + + ... on ArticlesRailHomeViewSection { + component { + title + href + type + } + + articlesConnection(first: 3) { + edges { + node { + title + href + } + } + } + } + } + } + } + ` + + const articles = [ + { + title: "Bored apes stolen", + slug: "stolen-apes", + }, + { + title: "More apes stolen", + slug: "more-apes", + }, + ] + + const context = { + articlesLoader: jest.fn().mockReturnValue({ + count: articles.length, + results: articles, + }), + authenticatedLoaders: { + meLoader: jest.fn().mockReturnValue({ type: "User" }), + }, + } + + const { homeView } = await runQuery(query, context) + + expect(homeView.section).toMatchInlineSnapshot(` + Object { + "__typename": "ArticlesRailHomeViewSection", + "articlesConnection": Object { + "edges": Array [ + Object { + "node": Object { + "href": "/article/stolen-apes", + "title": "Bored apes stolen", + }, + }, + Object { + "node": Object { + "href": "/article/more-apes", + "title": "More apes stolen", + }, + }, + ], + }, + "component": Object { + "href": "/news", + "title": "News", + "type": "ArticlesCard", + }, + } + `) + }) + }) }) diff --git a/src/schema/v2/homeView/articlesResolvers.ts b/src/schema/v2/homeView/articlesResolvers.ts index b69fafed51..4acb150a76 100644 --- a/src/schema/v2/homeView/articlesResolvers.ts +++ b/src/schema/v2/homeView/articlesResolvers.ts @@ -1,6 +1,8 @@ import type { GraphQLFieldResolver } from "graphql" import type { ResolverContext } from "types/graphql" import ArticlesConnection from "../articlesConnection" +import ArticleSorts from "../sorts/article_sorts" +import { ArticleLayoutEnum } from "../article/models" /* * Resolvers for home view articels sections @@ -10,3 +12,28 @@ export const LatestArticlesResolvers: GraphQLFieldResolver< any, ResolverContext > = ArticlesConnection.resolve! + +export const NewsResolver: GraphQLFieldResolver = async ( + parent, + args, + context, + info +) => { + const finalArgs = { + // formerly specified client-side + published: true, + sort: ArticleSorts.type.getValue("PUBLISHED_AT_DESC")?.value, + layout: ArticleLayoutEnum.getValue("NEWS")?.value, + + ...args, + } + + const result = await ArticlesConnection.resolve!( + parent, + finalArgs, + context, + info + ) + + return result +} diff --git a/src/schema/v2/homeView/getSectionsForUser.ts b/src/schema/v2/homeView/getSectionsForUser.ts index 1f6d037486..50431c0c9e 100644 --- a/src/schema/v2/homeView/getSectionsForUser.ts +++ b/src/schema/v2/homeView/getSectionsForUser.ts @@ -17,6 +17,7 @@ import { TrendingArtists, ViewingRooms, LatestAuctionResults, + News, } from "./sections" export async function getSectionsForUser( @@ -51,9 +52,11 @@ export async function getSectionsForUser( NewWorksFromGalleriesYouFollow, RecommendedArtists, ViewingRooms, + News, ] } else { sections = [ + News, LatestActivity, CuratorsPicksEmerging, SimilarToRecentlyViewedArtworks, diff --git a/src/schema/v2/homeView/sections.ts b/src/schema/v2/homeView/sections.ts index fa04bfec0c..6d91f9e95d 100644 --- a/src/schema/v2/homeView/sections.ts +++ b/src/schema/v2/homeView/sections.ts @@ -18,7 +18,7 @@ import { FeaturedFairsResolver } from "./featuredFairsResolver" type MaybeResolved = | T | ((context: ResolverContext, args: any) => Promise) -import { LatestArticlesResolvers } from "./articlesResolvers" +import { LatestArticlesResolvers, NewsResolver } from "./articlesResolvers" import { MarketingCollectionsResolver } from "./marketingCollectionsResolver" import { LatestActivityResolver } from "./activityResolvers" import { LatestAuctionResultsResolver } from "./auctionResultsResolvers" @@ -117,8 +117,6 @@ export const NewWorksFromGalleriesYouFollow: HomeViewSection = { resolver: NewWorksFromGalleriesYouFollowResolver, } -// Artists Rails - export const TrendingArtists: HomeViewSection = { id: "home-view-section-trending-artists", type: "ArtistsRailHomeViewSection", @@ -212,6 +210,17 @@ export const LatestAuctionResults: HomeViewSection = { resolver: LatestAuctionResultsResolver, } +export const News: HomeViewSection = { + id: "home-view-section-news", + type: "ArticlesRailHomeViewSection", + component: { + title: "News", + href: "/news", + type: "ArticlesCard", + }, + resolver: NewsResolver, +} + const sections: HomeViewSection[] = [ AuctionLotsForYou, CuratorsPicksEmerging, @@ -222,8 +231,10 @@ const sections: HomeViewSection[] = [ LatestAuctionResults, MarketingCollections, MarketingCollections, + MarketingCollections, NewWorksForYou, NewWorksFromGalleriesYouFollow, + News, RecentlyViewedArtworks, RecommendedArtists, ShowsForYou,