From 4db8bed96dd595a91eff1962caae2a25a16fd564 Mon Sep 17 00:00:00 2001 From: Jackson Morgan Date: Mon, 5 May 2025 13:11:50 -0400 Subject: [PATCH] Completed test for useLinkQuery --- .../src/linkTraversal/exploreLinks.ts | 4 + packages/react/src/methods/useLinkQuery.ts | 4 + .../test/Solid-Integration.test.tsx | 95 +++++++++++++++++++ packages/solid-react/test/setUpServer.ts | 14 +++ .../{ => wac}/link-query/main-profile.ttl | 2 +- .../{ => wac}/link-query/other-profile.ttl | 2 +- .../{ => wac}/link-query/third-profile.ttl | 2 +- 7 files changed, 120 insertions(+), 3 deletions(-) rename packages/solid-react/test/test-server/configs/template/{ => wac}/link-query/main-profile.ttl (65%) rename packages/solid-react/test/test-server/configs/template/{ => wac}/link-query/other-profile.ttl (66%) rename packages/solid-react/test/test-server/configs/template/{ => wac}/link-query/third-profile.ttl (66%) diff --git a/packages/connected/src/linkTraversal/exploreLinks.ts b/packages/connected/src/linkTraversal/exploreLinks.ts index cb45479..f7cded8 100644 --- a/packages/connected/src/linkTraversal/exploreLinks.ts +++ b/packages/connected/src/linkTraversal/exploreLinks.ts @@ -28,10 +28,12 @@ export async function exploreLinks< options?: ExploreLinksOptions, ): Promise { // Do an initial check of the resources. + console.log("Performing read for", startingResource.uri); const readResult = options?.shouldRefreshResources ? await startingResource.read() : await startingResource.readIfUnfetched(); if (readResult.isError) return; + console.log("Completed read for", startingResource.uri); if (options?.onResourceEncountered) await options?.onResourceEncountered(startingResource); @@ -77,6 +79,7 @@ export async function exploreLinksRecursive< ); const resourceToFetch = dataset.getResource(ldObject["@id"]); if (shouldFetch) { + console.log("Performing Read for", resourceToFetch.uri); const readResult = options?.shouldRefreshResources ? await resourceToFetch.read() : await resourceToFetch.readIfUnfetched(); @@ -84,6 +87,7 @@ export async function exploreLinksRecursive< if (readResult.isError) { return; } + console.log("Completed Read for", resourceToFetch.uri); } if (!encounteredDuringThisExploration.has(resourceToFetch.uri)) { encounteredDuringThisExploration.add(resourceToFetch.uri); diff --git a/packages/react/src/methods/useLinkQuery.ts b/packages/react/src/methods/useLinkQuery.ts index a3d9dc4..1accb78 100644 --- a/packages/react/src/methods/useLinkQuery.ts +++ b/packages/react/src/methods/useLinkQuery.ts @@ -49,6 +49,10 @@ export function createUseLinkQuery( .startLinkQuery(resource, startingSubject, linkQuery); linkQueryRef.current.subscribe().then(() => setIsLoading(false)); + + return () => { + linkQueryRef.current?.unsubscribeAll(); + }; }, [shapeType, startingResource, startingSubject, linkQuery]); const fromSubject = useCallback( diff --git a/packages/solid-react/test/Solid-Integration.test.tsx b/packages/solid-react/test/Solid-Integration.test.tsx index e500871..802dcba 100644 --- a/packages/solid-react/test/Solid-Integration.test.tsx +++ b/packages/solid-react/test/Solid-Integration.test.tsx @@ -2,10 +2,14 @@ import React, { useCallback, useEffect, useState } from "react"; import type { FunctionComponent } from "react"; import { render, screen, fireEvent, act } from "@testing-library/react"; import { + MAIN_PROFILE_SUBJECT, + MAIN_PROFILE_URI, + OTHER_PROFILE_URI, SAMPLE_BINARY_URI, SAMPLE_DATA_URI, SERVER_DOMAIN, setUpServer, + THIRD_PROFILE_SUBJECT, } from "./setUpServer"; import { UnauthenticatedSolidLdoProvider } from "../src/UnauthenticatedSolidLdoProvider"; import { @@ -17,9 +21,13 @@ import { useRootContainerFor, useSubject, useSubscribeToResource, + useLinkQuery, } from "../src"; import { PostShShapeType } from "./.ldo/post.shapeTypes"; import type { PostSh } from "./.ldo/post.typings"; +import { SolidProfileShapeShapeType } from "./.ldo/solidProfile.shapeTypes"; +import { changeData, commitData } from "@ldo/connected"; +import type { SolidProfileShape } from "./.ldo/solidProfile.typings"; // Use an increased timeout, since the CSS server takes too much setup time. jest.setTimeout(40_000); @@ -650,4 +658,91 @@ describe("Integration Tests", () => { unmount(); }); }); + + /** + * =========================================================================== + * useLinkQuery + * =========================================================================== + */ + describe("useLinkQuery", () => { + const linkQuery = { + name: true, + knows: { + name: true, + }, + } as const; + + it("Fetches a resource using useLinkQuery", async () => { + const UseLinkQueryTest: FunctionComponent = () => { + const profile = useLinkQuery( + SolidProfileShapeShapeType, + MAIN_PROFILE_URI, + MAIN_PROFILE_SUBJECT, + linkQuery, + ); + const addProfile = useCallback(async () => { + const cProfile = changeData( + profile as SolidProfileShape, + dataset.getResource(MAIN_PROFILE_URI), + ); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + cProfile.knows?.add({ "@id": THIRD_PROFILE_SUBJECT }); + await commitData(cProfile); + }, [profile]); + if (!profile) return

Loading

; + return ( +
+

{profile.name}

+
    + {profile.knows?.map((nestedProfile) => ( +
  • {nestedProfile.name}
  • + ))} +
+ +
+ ); + }; + const { unmount } = render( + + + , + ); + await screen.findByText("Loading"); + + let profileNameElement = await screen.findByRole("profile-name"); + + const resource = dataset.getResource(MAIN_PROFILE_URI); + console.log(resource.status); + const resource2 = dataset.getResource(OTHER_PROFILE_URI); + console.log(resource2.status); + + expect(profileNameElement.textContent).toBe("Main User"); + + let list = await screen.findByRole("list"); + expect(list.children[0].innerHTML).toBe("Other User"); + expect(list.children.length).toBe(1); + + // Click button to add a publisher + await fireEvent.click(screen.getByText("Add Profile")); + + // Give some time for notifications to propogate + await act(async () => { + await new Promise((resolve) => setTimeout(resolve, 2000)); + }); + + profileNameElement = await screen.findByRole("profile-name"); + expect(profileNameElement.textContent).toBe("Main User"); + + list = await screen.findByRole("list"); + expect(list.children[0].innerHTML).toBe("Other User"); + expect(list.children[1].innerHTML).toBe("Third User"); + + expect(list.children.length).toBe(2); + + unmount(); + }); + }); }); diff --git a/packages/solid-react/test/setUpServer.ts b/packages/solid-react/test/setUpServer.ts index b1f73d2..390b92b 100644 --- a/packages/solid-react/test/setUpServer.ts +++ b/packages/solid-react/test/setUpServer.ts @@ -20,6 +20,20 @@ export const SAMPLE2_BINARY_URI = `${TEST_CONTAINER_URI}${SAMPLE2_BINARY_SLUG}` as SolidLeafUri; export const SAMPLE_CONTAINER_URI = `${TEST_CONTAINER_URI}sample_container/` as SolidContainerUri; +export const LINK_QUERY_CONTAINER = `${ROOT_CONTAINER}link-query/`; +export const MAIN_PROFILE_URI = + `${LINK_QUERY_CONTAINER}main-profile.ttl` as SolidContainerUri; +export const MAIN_PROFILE_SUBJECT = + `${MAIN_PROFILE_URI}#me` as SolidContainerUri; +export const OTHER_PROFILE_URI = + `${LINK_QUERY_CONTAINER}other-profile.ttl` as SolidContainerUri; +export const OTHER_PROFILE_SUBJECT = + `${OTHER_PROFILE_URI}#me` as SolidContainerUri; +export const THIRD_PROFILE_URI = + `${LINK_QUERY_CONTAINER}third-profile.ttl` as SolidContainerUri; +export const THIRD_PROFILE_SUBJECT = + `${THIRD_PROFILE_URI}#me` as SolidContainerUri; + export const EXAMPLE_POST_TTL = `@prefix schema: . <#Post1> diff --git a/packages/solid-react/test/test-server/configs/template/link-query/main-profile.ttl b/packages/solid-react/test/test-server/configs/template/wac/link-query/main-profile.ttl similarity index 65% rename from packages/solid-react/test/test-server/configs/template/link-query/main-profile.ttl rename to packages/solid-react/test/test-server/configs/template/wac/link-query/main-profile.ttl index 562e9fd..5a1891b 100644 --- a/packages/solid-react/test/test-server/configs/template/link-query/main-profile.ttl +++ b/packages/solid-react/test/test-server/configs/template/wac/link-query/main-profile.ttl @@ -4,4 +4,4 @@ :me a foaf:Person ; foaf:name "Main User" ; foaf:mbox ; - foaf:knows . \ No newline at end of file + foaf:knows . \ No newline at end of file diff --git a/packages/solid-react/test/test-server/configs/template/link-query/other-profile.ttl b/packages/solid-react/test/test-server/configs/template/wac/link-query/other-profile.ttl similarity index 66% rename from packages/solid-react/test/test-server/configs/template/link-query/other-profile.ttl rename to packages/solid-react/test/test-server/configs/template/wac/link-query/other-profile.ttl index f1eb291..bf3b3b8 100644 --- a/packages/solid-react/test/test-server/configs/template/link-query/other-profile.ttl +++ b/packages/solid-react/test/test-server/configs/template/wac/link-query/other-profile.ttl @@ -4,4 +4,4 @@ :me a foaf:Person ; foaf:name "Other User" ; foaf:mbox ; - foaf:knows . \ No newline at end of file + foaf:knows . \ No newline at end of file diff --git a/packages/solid-react/test/test-server/configs/template/link-query/third-profile.ttl b/packages/solid-react/test/test-server/configs/template/wac/link-query/third-profile.ttl similarity index 66% rename from packages/solid-react/test/test-server/configs/template/link-query/third-profile.ttl rename to packages/solid-react/test/test-server/configs/template/wac/link-query/third-profile.ttl index 81e8017..e6dc8ce 100644 --- a/packages/solid-react/test/test-server/configs/template/link-query/third-profile.ttl +++ b/packages/solid-react/test/test-server/configs/template/wac/link-query/third-profile.ttl @@ -4,4 +4,4 @@ :me a foaf:Person ; foaf:name "Third User" ; foaf:mbox ; - foaf:knows . \ No newline at end of file + foaf:knows . \ No newline at end of file