77// the Business Source License, use of this software will be governed
88// by the Apache License, Version 2.0.
99
10- import { screen , within } from "@testing-library/react" ;
10+ import { screen , waitFor , within } from "@testing-library/react" ;
1111import userEvent from "@testing-library/user-event" ;
12+ import { delay , http } from "msw" ;
1213import React from "react" ;
1314import { describe , expect , it } from "vitest" ;
1415
1516import { Cluster } from "~/api/materialize/cluster/clusterList" ;
17+ import { DatabaseObject } from "~/api/materialize/objects" ;
1618import { SchemaWithOptionalDatabase } from "~/api/materialize/schemaList" ;
1719import { SubscribeError } from "~/api/materialize/SubscribeManager" ;
20+ import server from "~/api/mocks/server" ;
1821import { allClusters } from "~/store/allClusters" ;
22+ import { allObjects } from "~/store/allObjects" ;
1923import { allSchemas } from "~/store/allSchemas" ;
2024import { renderComponent } from "~/test/utils" ;
2125
@@ -74,15 +78,51 @@ const CLUSTER: Cluster = {
7478 replicationFactor : 1 ,
7579} as unknown as Cluster ;
7680
81+ const PROMPT =
82+ "Select a cluster or schema to see the freshness of its objects." ;
83+
84+ // One object on CLUSTER, so a selected cluster has something to query.
85+ const OBJECT = {
86+ id : "u10" ,
87+ name : "orders_mv" ,
88+ objectType : "materialized-view" ,
89+ schemaId : "u3" ,
90+ schemaName : "public" ,
91+ databaseId : "u1" ,
92+ databaseName : "materialize" ,
93+ sourceType : null ,
94+ isWebhookTable : "false" ,
95+ clusterId : "u1" ,
96+ clusterName : "quickstart" ,
97+ } as unknown as DatabaseObject ;
98+
99+ // Records each SQL request and never answers.
100+ const recordRequests = ( ) => {
101+ const requests : string [ ] = [ ] ;
102+ server . use (
103+ http . post ( "*/api/sql" , async ( { request } ) => {
104+ requests . push ( new URL ( request . url ) . searchParams . get ( "query_key" ) ?? "" ) ;
105+ await delay ( "infinite" ) ;
106+ } ) ,
107+ ) ;
108+ return requests ;
109+ } ;
110+
77111describe ( "FreshnessPage cluster selection" , ( ) => {
78112 const renderAt = async ( search : string ) =>
79113 renderComponent ( < FreshnessPage /> , {
80- initializeState : ( store ) =>
114+ initializeState : ( store ) => {
81115 store . set ( allClusters , {
82116 data : [ CLUSTER ] ,
83117 error : undefined ,
84118 snapshotComplete : true ,
85- } ) ,
119+ } ) ;
120+ store . set ( allObjects , {
121+ data : [ OBJECT ] ,
122+ error : undefined ,
123+ snapshotComplete : true ,
124+ } ) ;
125+ } ,
86126 initialRouterEntries : [ search ] ,
87127 } ) ;
88128
@@ -95,11 +135,25 @@ describe("FreshnessPage cluster selection", () => {
95135 expect ( screen . queryByText ( "quickstart" ) ) . not . toBeInTheDocument ( ) ;
96136 } ) ;
97137
98- it ( "still falls back when no cluster is named" , async ( ) => {
138+ it ( "waits for a selection rather than loading the first cluster" , async ( ) => {
139+ // The sidebar's link names no cluster. Loading the first one
140+ // alphabetically would spend queries on a cluster nobody asked about.
141+ const requests = recordRequests ( ) ;
99142 await renderAt ( "/" ) ;
100143
144+ expect ( await screen . findByText ( PROMPT ) ) . toBeVisible ( ) ;
145+ expect ( screen . getByText ( "Select a cluster" ) ) . toBeVisible ( ) ;
146+ expect ( screen . queryByText ( "quickstart" ) ) . not . toBeInTheDocument ( ) ;
147+ expect ( requests ) . toEqual ( [ ] ) ;
148+ } ) ;
149+
150+ it ( "loads a cluster a link names" , async ( ) => {
151+ const requests = recordRequests ( ) ;
152+ await renderAt ( "/?cluster=u1" ) ;
153+
101154 expect ( await screen . findByText ( "quickstart" ) ) . toBeVisible ( ) ;
102- expect ( screen . queryByText ( / w a s n o t f o u n d / ) ) . not . toBeInTheDocument ( ) ;
155+ await waitFor ( ( ) => expect ( requests . length ) . toBeGreaterThan ( 0 ) ) ;
156+ expect ( screen . queryByText ( PROMPT ) ) . not . toBeInTheDocument ( ) ;
103157 } ) ;
104158} ) ;
105159
@@ -126,9 +180,9 @@ describe("FreshnessPage schema scope", () => {
126180 initialRouterEntries : [ search ] ,
127181 } ) ;
128182
129- it ( "names each schema with its database and falls back to the first " , async ( ) => {
183+ it ( "names a linked schema with its database" , async ( ) => {
130184 // Both are `public`, so the bare names could not be told apart.
131- await renderAt ( "/?scope=schema" ) ;
185+ await renderAt ( "/?scope=schema&schema=u9 " ) ;
132186
133187 expect ( await screen . findByText ( "analytics.public" ) ) . toBeVisible ( ) ;
134188 expect ( screen . queryByText ( "quickstart" ) ) . not . toBeInTheDocument ( ) ;
@@ -156,7 +210,7 @@ describe("FreshnessPage schema scope", () => {
156210 expect ( await screen . findByText ( / T h a t s c h e m a w a s n o t f o u n d / ) ) . toBeVisible ( ) ;
157211 } ) ;
158212
159- it ( "shows the first option of the new scope when the scope changes" , async ( ) => {
213+ it ( "waits for a selection when the scope changes" , async ( ) => {
160214 await renderAt ( "/?cluster=u1" ) ;
161215 expect ( await screen . findByText ( "quickstart" ) ) . toBeVisible ( ) ;
162216
@@ -165,7 +219,9 @@ describe("FreshnessPage schema scope", () => {
165219 "schema" ,
166220 ) ;
167221
168- expect ( await screen . findByText ( "analytics.public" ) ) . toBeVisible ( ) ;
222+ expect ( await screen . findByText ( PROMPT ) ) . toBeVisible ( ) ;
223+ expect ( screen . getByText ( "Select a schema" ) ) . toBeVisible ( ) ;
224+ expect ( screen . queryByText ( "analytics.public" ) ) . not . toBeInTheDocument ( ) ;
169225 expect ( screen . queryByText ( "quickstart" ) ) . not . toBeInTheDocument ( ) ;
170226 } ) ;
171227} ) ;
0 commit comments