All tests except useSubject

main
jaxoncreed 2 years ago
parent 787c50e330
commit 99c02bdae2
  1. 1
      packages/solid-react/src/UnauthenticatedSolidLdoProvider.tsx
  2. 13
      packages/solid-react/src/useLdoMethods.ts
  3. 11
      packages/solid-react/src/useRootContainer.ts
  4. 31
      packages/solid-react/test/.ldo/post.context.ts
  5. 155
      packages/solid-react/test/.ldo/post.schema.ts
  6. 19
      packages/solid-react/test/.ldo/post.shapeTypes.ts
  7. 45
      packages/solid-react/test/.ldo/post.typings.ts
  8. 101
      packages/solid-react/test/Integration.test.tsx
  9. 2
      packages/solid-react/test/setUpServer.ts
  10. 9
      packages/solid-react/test/useSubject.integration.test.tsx

@ -1,3 +1,4 @@
/* istanbul ignore file */
import React, { useCallback, useMemo } from "react"; import React, { useCallback, useMemo } from "react";
import type { FunctionComponent, PropsWithChildren } from "react"; import type { FunctionComponent, PropsWithChildren } from "react";
import type { LoginOptions, SessionInfo } from "./SolidAuthContext"; import type { LoginOptions, SessionInfo } from "./SolidAuthContext";

@ -9,13 +9,18 @@ export interface UseLdoMethods {
getSubject<Type extends LdoBase>( getSubject<Type extends LdoBase>(
shapeType: ShapeType<Type>, shapeType: ShapeType<Type>,
subject: string | SubjectNode, subject: string | SubjectNode,
): Type | Error; ): Type;
createData<Type extends LdoBase>( createData<Type extends LdoBase>(
shapeType: ShapeType<Type>, shapeType: ShapeType<Type>,
subject: string | SubjectNode, subject: string | SubjectNode,
...resources: Resource[] resource: Resource,
...additionalResources: Resource[]
): Type;
changeData<Type extends LdoBase>(
input: Type,
resource: Resource,
...additionalResources: Resource[]
): Type; ): Type;
changeData<Type extends LdoBase>(input: Type, ...resources: Resource[]): Type;
commitData(input: LdoBase): ReturnType<SolidLdoDataset["commitChangesToPod"]>; commitData(input: LdoBase): ReturnType<SolidLdoDataset["commitChangesToPod"]>;
} }
@ -35,7 +40,7 @@ export function createUseLdoMethods(dataset: SolidLdoDataset): UseLdoMethods {
getSubject<Type extends LdoBase>( getSubject<Type extends LdoBase>(
shapeType: ShapeType<Type>, shapeType: ShapeType<Type>,
subject: string | SubjectNode, subject: string | SubjectNode,
): Type | Error { ): Type {
return dataset.usingType(shapeType).fromSubject(subject); return dataset.usingType(shapeType).fromSubject(subject);
}, },
/** /**

@ -1,8 +1,13 @@
import type { Container, ContainerUri } from "@ldo/solid"; import type { Container, ContainerUri } from "@ldo/solid";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import type { UseResourceOptions } from "./useResource";
import { useResource } from "./useResource"; import { useResource } from "./useResource";
import { useLdo } from "./SolidLdoProvider"; import { useLdo } from "./SolidLdoProvider";
export function useRootContainerFor(uri?: string): Container | undefined {
export function useRootContainerFor(
uri?: string,
options?: UseResourceOptions,
): Container | undefined {
const { getResource } = useLdo(); const { getResource } = useLdo();
const [rootContainerUri, setRootContainerUri] = useState< const [rootContainerUri, setRootContainerUri] = useState<
@ -17,8 +22,10 @@ export function useRootContainerFor(uri?: string): Container | undefined {
setRootContainerUri(result.uri); setRootContainerUri(result.uri);
} }
}); });
} else {
setRootContainerUri(undefined);
} }
}, [uri]); }, [uri]);
return useResource(rootContainerUri); return useResource(rootContainerUri, options);
} }

@ -0,0 +1,31 @@
import { ContextDefinition } from "jsonld";
/**
* =============================================================================
* postContext: JSONLD Context for post
* =============================================================================
*/
export const postContext: ContextDefinition = {
type: {
"@id": "@type",
},
SocialMediaPosting: "http://schema.org/SocialMediaPosting",
CreativeWork: "http://schema.org/CreativeWork",
Thing: "http://schema.org/Thing",
articleBody: {
"@id": "http://schema.org/articleBody",
"@type": "http://www.w3.org/2001/XMLSchema#string",
},
uploadDate: {
"@id": "http://schema.org/uploadDate",
"@type": "http://www.w3.org/2001/XMLSchema#date",
},
image: {
"@id": "http://schema.org/image",
"@type": "@id",
},
publisher: {
"@id": "http://schema.org/publisher",
"@type": "@id",
},
};

@ -0,0 +1,155 @@
import { Schema } from "shexj";
/**
* =============================================================================
* postSchema: ShexJ Schema for post
* =============================================================================
*/
export const postSchema: Schema = {
type: "Schema",
shapes: [
{
id: "https://example.com/PostSh",
type: "ShapeDecl",
shapeExpr: {
type: "Shape",
expression: {
type: "EachOf",
expressions: [
{
type: "TripleConstraint",
predicate: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
valueExpr: {
type: "NodeConstraint",
values: [
"http://schema.org/SocialMediaPosting",
"http://schema.org/CreativeWork",
"http://schema.org/Thing",
],
},
},
{
type: "TripleConstraint",
predicate: "http://schema.org/articleBody",
valueExpr: {
type: "NodeConstraint",
datatype: "http://www.w3.org/2001/XMLSchema#string",
},
min: 0,
max: 1,
annotations: [
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#label",
object: {
value: "articleBody",
},
},
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#comment",
object: {
value: "The actual body of the article. ",
},
},
],
},
{
type: "TripleConstraint",
predicate: "http://schema.org/uploadDate",
valueExpr: {
type: "NodeConstraint",
datatype: "http://www.w3.org/2001/XMLSchema#date",
},
annotations: [
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#label",
object: {
value: "uploadDate",
},
},
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#comment",
object: {
value:
"Date when this media object was uploaded to this site.",
},
},
],
},
{
type: "TripleConstraint",
predicate: "http://schema.org/image",
valueExpr: {
type: "NodeConstraint",
nodeKind: "iri",
},
min: 0,
max: 1,
annotations: [
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#label",
object: {
value: "image",
},
},
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#comment",
object: {
value:
"A media object that encodes this CreativeWork. This property is a synonym for encoding.",
},
},
],
},
{
type: "TripleConstraint",
predicate: "http://schema.org/publisher",
valueExpr: {
type: "NodeConstraint",
nodeKind: "iri",
},
annotations: [
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#label",
object: {
value: "publisher",
},
},
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#comment",
object: {
value: "The publisher of the creative work.",
},
},
],
},
],
},
annotations: [
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#label",
object: {
value: "SocialMediaPost",
},
},
{
type: "Annotation",
predicate: "http://www.w3.org/2000/01/rdf-schema#comment",
object: {
value:
"A post to a social media platform, including blog posts, tweets, Facebook posts, etc.",
},
},
],
},
},
],
};

@ -0,0 +1,19 @@
import { ShapeType } from "@ldo/ldo";
import { postSchema } from "./post.schema";
import { postContext } from "./post.context";
import { PostSh } from "./post.typings";
/**
* =============================================================================
* LDO ShapeTypes post
* =============================================================================
*/
/**
* PostSh ShapeType
*/
export const PostShShapeType: ShapeType<PostSh> = {
schema: postSchema,
shape: "https://example.com/PostSh",
context: postContext,
};

@ -0,0 +1,45 @@
import { ContextDefinition } from "jsonld";
/**
* =============================================================================
* Typescript Typings for post
* =============================================================================
*/
/**
* PostSh Type
*/
export interface PostSh {
"@id"?: string;
"@context"?: ContextDefinition;
type:
| {
"@id": "SocialMediaPosting";
}
| {
"@id": "CreativeWork";
}
| {
"@id": "Thing";
};
/**
* The actual body of the article.
*/
articleBody?: string;
/**
* Date when this media object was uploaded to this site.
*/
uploadDate: string;
/**
* A media object that encodes this CreativeWork. This property is a synonym for encoding.
*/
image?: {
"@id": string;
};
/**
* The publisher of the creative work.
*/
publisher: {
"@id": string;
};
}

@ -1,9 +1,18 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import type { FunctionComponent } from "react"; import type { FunctionComponent } from "react";
import { render, screen, fireEvent } from "@testing-library/react"; import { render, screen, fireEvent } from "@testing-library/react";
import { SAMPLE_BINARY_URI, SAMPLE_DATA_URI, setUpServer } from "./setUpServer"; import {
SAMPLE_BINARY_URI,
SAMPLE_DATA_URI,
SERVER_DOMAIN,
setUpServer,
} from "./setUpServer";
import { UnauthenticatedSolidLdoProvider } from "../src/UnauthenticatedSolidLdoProvider"; import { UnauthenticatedSolidLdoProvider } from "../src/UnauthenticatedSolidLdoProvider";
import { useResource } from "../src/useResource"; import { useResource } from "../src/useResource";
import { useRootContainerFor } from "../src/useRootContainer";
import { useLdo } from "../src/SolidLdoProvider";
import { PostShShapeType } from "./.ldo/post.shapeTypes";
import type { PostSh } from "./.ldo/post.typings";
// Use an increased timeout, since the CSS server takes too much setup time. // Use an increased timeout, since the CSS server takes too much setup time.
jest.setTimeout(40_000); jest.setTimeout(40_000);
@ -118,4 +127,94 @@ describe("Integration Tests", () => {
expect(resourceStatus2.innerHTML).toBe("binaryReadSuccess"); expect(resourceStatus2.innerHTML).toBe("binaryReadSuccess");
}); });
}); });
describe("useRootContainer", () => {
it("gets the root container for a sub-resource", async () => {
const RootContainerTest: FunctionComponent = () => {
const rootContainer = useRootContainerFor(SAMPLE_DATA_URI, {
suppressInitialRead: true,
});
return rootContainer ? (
<p role="root">{rootContainer?.uri}</p>
) : undefined;
};
render(
<UnauthenticatedSolidLdoProvider>
<RootContainerTest />
</UnauthenticatedSolidLdoProvider>,
);
const container = await screen.findByRole("root");
expect(container.innerHTML).toBe(SERVER_DOMAIN);
});
it("returns undefined when a URI is not provided", async () => {
const RootContainerTest: FunctionComponent = () => {
const rootContainer = useRootContainerFor(undefined, {
suppressInitialRead: true,
});
return rootContainer ? (
<p role="root">{rootContainer?.uri}</p>
) : (
<p role="undefined">Undefined</p>
);
};
render(
<UnauthenticatedSolidLdoProvider>
<RootContainerTest />
</UnauthenticatedSolidLdoProvider>,
);
const container = await screen.findByRole("undefined");
expect(container.innerHTML).toBe("Undefined");
});
});
describe("useLdoMethod", () => {
it("uses get subject to get a linked data object", async () => {
const GetSubjectTest: FunctionComponent = () => {
const [subject, setSubject] = useState<PostSh | undefined>();
const { getSubject } = useLdo();
useEffect(() => {
const someSubject = getSubject(
PostShShapeType,
"https://example.com/subject",
);
setSubject(someSubject);
}, []);
return subject ? <p role="subject">{subject["@id"]}</p> : undefined;
};
render(
<UnauthenticatedSolidLdoProvider>
<GetSubjectTest />
</UnauthenticatedSolidLdoProvider>,
);
const container = await screen.findByRole("subject");
expect(container.innerHTML).toBe("https://example.com/subject");
});
it("uses createData to create a new data object", async () => {
const GetSubjectTest: FunctionComponent = () => {
const [subject, setSubject] = useState<PostSh | undefined>();
const { createData, getResource } = useLdo();
useEffect(() => {
const someSubject = createData(
PostShShapeType,
"https://example.com/subject",
getResource("https://example.com/"),
);
someSubject.articleBody = "Cool Article";
setSubject(someSubject);
}, []);
return subject ? (
<p role="subject">{subject.articleBody}</p>
) : undefined;
};
render(
<UnauthenticatedSolidLdoProvider>
<GetSubjectTest />
</UnauthenticatedSolidLdoProvider>,
);
const container = await screen.findByRole("subject");
expect(container.innerHTML).toBe("Cool Article");
});
});
}); });

@ -73,7 +73,7 @@ export function setUpServer(): SetUpServerReturn {
beforeEach(async () => { beforeEach(async () => {
s.fetchMock = jest.fn(s.authFetch); s.fetchMock = jest.fn(s.authFetch);
// Create a new document called sample.ttl // Create a new document called sample.ttl
const result = await s.authFetch(ROOT_CONTAINER, { await s.authFetch(ROOT_CONTAINER, {
method: "POST", method: "POST",
headers: { headers: {
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"', link: '<http://www.w3.org/ns/ldp#Container>; rel="type"',

@ -0,0 +1,9 @@
import { setUpServer } from "./setUpServer";
describe("useSubject", () => {
setUpServer();
it("trivial", () => {
expect(true).toBe(true);
});
});
Loading…
Cancel
Save