@@ -70,34 +70,46 @@ async function deleteDatabase(instance: DataConnect): Promise<void> {
7070 await executeMutation ( ref ) ;
7171 }
7272}
73- async function seedDatabase ( instance : DataConnect ) : Promise < void > {
73+ async function seedDatabase (
74+ instance : DataConnect ,
75+ testId : string
76+ ) : Promise < void > {
7477 for ( let i = 0 ; i < SEEDED_DATA . length ; i ++ ) {
75- const data = SEEDED_DATA [ i ] ;
78+ const data = { ... SEEDED_DATA [ i ] , testId } ;
7679 const ref = mutationRef ( instance , 'AddPost' , data ) ;
7780 await executeMutation ( ref ) ;
7881 }
7982}
8083
84+ interface PostVariables {
85+ testId : string ;
86+ }
8187describe ( 'DataConnect Tests' , async ( ) => {
8288 let dc : DataConnect ;
89+ const TEST_ID = crypto . randomUUID ( ) ;
8390 beforeEach ( async ( ) => {
8491 dc = initDatabase ( ) ;
85- await seedDatabase ( dc ) ;
92+ await seedDatabase ( dc , TEST_ID ) ;
8693 } ) ;
8794 afterEach ( async ( ) => {
8895 await deleteDatabase ( dc ) ;
8996 await terminate ( dc ) ;
9097 } ) ;
98+ function getPostsRef ( ) : QueryRef < PostListResponse , PostVariables > {
99+ return queryRef < PostListResponse , PostVariables > ( dc , 'ListPosts' , {
100+ testId : TEST_ID
101+ } ) ;
102+ }
91103 it ( 'Can get all posts' , async ( ) => {
92- const taskListQuery = queryRef < PostListResponse > ( dc , 'ListPosts' ) ;
104+ const taskListQuery = getPostsRef ( ) ;
93105 const taskListRes = await executeQuery ( taskListQuery ) ;
94106 expect ( taskListRes . data ) . to . deep . eq ( {
95107 posts : REAL_DATA
96108 } ) ;
97109 } ) ;
98110 it ( `instantly executes a query if one hasn't been subscribed to` , async ( ) => {
99- const taskListQuery = queryRef < PostListResponse > ( dc , 'ListPosts' ) ;
100- const promise = new Promise < QueryResult < PostListResponse , undefined > > (
111+ const taskListQuery = getPostsRef ( ) ;
112+ const promise = new Promise < QueryResult < PostListResponse , PostVariables > > (
101113 ( resolve , reject ) => {
102114 const unsubscribe = subscribe ( taskListQuery , {
103115 onNext : res => {
@@ -118,17 +130,17 @@ describe('DataConnect Tests', async () => {
118130 expect ( res . source ) . to . eq ( SOURCE_SERVER ) ;
119131 } ) ;
120132 it ( `returns the result source as cache when data already exists` , async ( ) => {
121- const taskListQuery = queryRef < PostListResponse > ( dc , 'ListPosts' ) ;
133+ const taskListQuery = getPostsRef ( ) ;
122134 const queryResult = await executeQuery ( taskListQuery ) ;
123135 const result = await waitForFirstEvent ( taskListQuery ) ;
124136 expect ( result . data ) . to . eq ( queryResult . data ) ;
125137 expect ( result . source ) . to . eq ( SOURCE_CACHE ) ;
126138 } ) ;
127139 it ( `returns the proper JSON when calling .toJSON()` , async ( ) => {
128- const taskListQuery = queryRef < PostListResponse > ( dc , 'ListPosts' ) ;
140+ const taskListQuery = getPostsRef ( ) ;
129141 await executeQuery ( taskListQuery ) ;
130142 const result = await waitForFirstEvent ( taskListQuery ) ;
131- const serializedRef : SerializedRef < PostListResponse , undefined > = {
143+ const serializedRef : SerializedRef < PostListResponse , PostVariables > = {
132144 data : {
133145 posts : REAL_DATA
134146 } ,
@@ -139,7 +151,7 @@ describe('DataConnect Tests', async () => {
139151 projectId : PROJECT_ID
140152 } ,
141153 name : taskListQuery . name ,
142- variables : undefined
154+ variables : { testId : TEST_ID }
143155 } ,
144156 source : SOURCE_CACHE
145157 } ;
0 commit comments