Types now passing in tests.

main
Jackson Morgan 7 months ago
parent ffe5d3f047
commit 068f82a9ef
  1. 7
      packages/jsonld-dataset-proxy/src/graphOf.ts
  2. 3
      packages/jsonld-dataset-proxy/src/language/languagesOf.ts
  3. 73
      packages/jsonld-dataset-proxy/test/jsonldDatasetProxy.test.ts

@ -8,21 +8,20 @@ import {
_getUnderlyingNode, _getUnderlyingNode,
_proxyContext, _proxyContext,
} from "./types"; } from "./types";
import type { LdSet } from "./setProxy/ldSet/LdSet";
/** /**
* Returns the graph for which a defined triple is a member * Returns the graph for which a defined triple is a member
* @param subject A JsonldDatasetProxy that represents the subject * @param subject A JsonldDatasetProxy that represents the subject
* @param predicate The key on the JsonldDatasetProxy * @param predicate The key on the JsonldDatasetProxy
* @param object The direct object. This can be a JsonldDatasetProxy. This field * @param object The direct object. This can be a JsonldDatasetProxy. This field
* is optional if this field does not have a "set" object. * is optional.
* @returns a list of graphs for which the triples are members * @returns a list of graphs for which the triples are members
*/ */
export function graphOf<Subject extends ObjectLike, Key extends keyof Subject>( export function graphOf<Subject extends ObjectLike, Key extends keyof Subject>(
subject: Subject, subject: Subject,
predicate: Key, predicate: Key,
object: NonNullable<Subject[Key]> extends Set<infer T> object?: NonNullable<Subject[Key]> extends LdSet<infer T> ? T : Subject[Key],
? T
: Subject[Key] | undefined,
): GraphNode[] { ): GraphNode[] {
const subjectProxy = getSubjectProxyFromObject(subject); const subjectProxy = getSubjectProxyFromObject(subject);
const proxyContext = subjectProxy[_proxyContext]; const proxyContext = subjectProxy[_proxyContext];

@ -3,6 +3,7 @@ import { getSubjectProxyFromObject } from "../subjectProxy/isSubjectProxy";
import type { ObjectLike } from "../types"; import type { ObjectLike } from "../types";
import { _getUnderlyingNode, _proxyContext } from "../types"; import { _getUnderlyingNode, _proxyContext } from "../types";
import { createLanguageMapProxy } from "./languageMapProxy"; import { createLanguageMapProxy } from "./languageMapProxy";
import type { LdSet } from "../setProxy/ldSet/LdSet";
/** /**
* ----------------------------------------------------------------------------- * -----------------------------------------------------------------------------
@ -25,7 +26,7 @@ export type LanguageSet = Set<string>;
export type LanguageOfConditionalReturn< export type LanguageOfConditionalReturn<
SubjectObject extends ObjectLike, SubjectObject extends ObjectLike,
Key extends keyof SubjectObject, Key extends keyof SubjectObject,
> = NonNullable<SubjectObject[Key]> extends Array<unknown> > = NonNullable<SubjectObject[Key]> extends LdSet<unknown>
? LanguageSetMap ? LanguageSetMap
: LanguageMap; : LanguageMap;

@ -12,7 +12,6 @@ import {
_isSubjectOriented, _isSubjectOriented,
_proxyContext, _proxyContext,
_writeGraphs, _writeGraphs,
BasicLdSet,
set, set,
} from "../src"; } from "../src";
import type { ObservationShape, PatientShape } from "./patientExampleData"; import type { ObservationShape, PatientShape } from "./patientExampleData";
@ -1401,7 +1400,7 @@ const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
describe("graphOf", () => { describe("graphOf", () => {
it("detects the graph of a single value", async () => { it("detects the graph of a single value", async () => {
const [, observation] = await getGraphLoadedDataset(); const [, observation] = await getGraphLoadedDataset();
expect(graphOf(observation, "subject")[0].value).toBe( expect(graphOf(observation, "subject").map((n) => n.value)).toContain(
"http://example.com/Observation1Doc", "http://example.com/Observation1Doc",
); );
expect( expect(
@ -1415,15 +1414,14 @@ const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
it("detects the graph of an array value", async () => { it("detects the graph of an array value", async () => {
const [, observation] = await getGraphLoadedDataset(); const [, observation] = await getGraphLoadedDataset();
const patient1 = observation.subject as PatientShape; const patient1 = observation.subject as PatientShape;
expect(graphOf(patient1, "name", 0)[0].value).toBe(
"http://example.com/Patient1Doc",
);
expect(graphOf(patient1, "roommate", 0)[0].value).toBe(
"http://example.com/Patient1Doc",
);
expect( expect(
graphOf(patient1, "roommate", patient1.roommate?.[1])[0].value, graphOf(patient1, "name", "Garrett").map((n) => n.value),
).toBe("http://example.com/Patient1Doc"); ).toContain("http://example.com/Patient1Doc");
expect(
graphOf(patient1, "roommate", {
"@id": "http://example.com/Patient2",
} as PatientShape).map((n) => n.value),
).toContain("http://example.com/Patient1Doc");
}); });
it("detects the graph of a value in multiple graphs", async () => { it("detects the graph of a value in multiple graphs", async () => {
@ -1451,13 +1449,6 @@ const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
`Key "subject" of [object Object] is not an array.`, `Key "subject" of [object Object] is not an array.`,
); );
}); });
it("throws an error if the index is out of bounds", async () => {
const [, observation] = await getGraphLoadedDataset();
expect(() =>
graphOf(observation.subject as PatientShape, "name", 10),
).toThrowError(`Index 10 does not exist.`);
});
}); });
describe("write method", () => { describe("write method", () => {
@ -1477,27 +1468,31 @@ const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
const [, patient] = await getEmptyPatientDataset(); const [, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" }; patient.type = { "@id": "Patient" };
patient.name?.push("default"); patient.name?.add("default");
const end1 = write(doc1).using(patient); const end1 = write(doc1).using(patient);
patient.name?.push("1"); patient.name?.add("1");
const end2 = write(doc2).using(patient); const end2 = write(doc2).using(patient);
patient.name?.push("2"); patient.name?.add("2");
const end3 = write(doc3).using(patient); const end3 = write(doc3).using(patient);
patient.name?.push("3"); patient.name?.add("3");
end3(); end3();
patient.name?.push("2 again"); patient.name?.add("2 again");
end2(); end2();
patient.name?.push("1 again"); patient.name?.add("1 again");
end1(); end1();
patient.name?.push("default again"); patient.name?.add("default again");
expect(graphOf(patient, "name", 0)[0].value).toBe(defaultGraph().value); expect(graphOf(patient, "name", "default")[0].value).toBe(
expect(graphOf(patient, "name", 1)[0].value).toBe(doc1.value); defaultGraph().value,
expect(graphOf(patient, "name", 2)[0].value).toBe(doc2.value); );
expect(graphOf(patient, "name", 3)[0].value).toBe(doc3.value); expect(graphOf(patient, "name", "1")[0].value).toBe(doc1.value);
expect(graphOf(patient, "name", 4)[0].value).toBe(doc2.value); expect(graphOf(patient, "name", "2")[0].value).toBe(doc2.value);
expect(graphOf(patient, "name", 5)[0].value).toBe(doc1.value); expect(graphOf(patient, "name", "3")[0].value).toBe(doc3.value);
expect(graphOf(patient, "name", 6)[0].value).toBe(defaultGraph().value); expect(graphOf(patient, "name", "4")[0].value).toBe(doc2.value);
expect(graphOf(patient, "name", "5")[0].value).toBe(doc1.value);
expect(graphOf(patient, "name", "default again")[0].value).toBe(
defaultGraph().value,
);
}); });
it("copies the proxy and changes the write graphs without modifying the original", async () => { it("copies the proxy and changes the write graphs without modifying the original", async () => {
@ -1505,11 +1500,13 @@ const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
const [, patient] = await getEmptyPatientDataset(); const [, patient] = await getEmptyPatientDataset();
patient.type = { "@id": "Patient" }; patient.type = { "@id": "Patient" };
patient.name?.push("Default"); patient.name?.add("Default");
const [patientOnDoc1] = write(doc1).usingCopy(patient); const [patientOnDoc1] = write(doc1).usingCopy(patient);
patientOnDoc1.name?.push("Doc1"); patientOnDoc1.name?.add("Doc1");
expect(graphOf(patient, "name", 0)[0].value).toBe(defaultGraph().value); expect(graphOf(patient, "name", "Default")[0].value).toBe(
expect(graphOf(patient, "name", 1)[0].value).toBe(doc1.value); defaultGraph().value,
);
expect(graphOf(patient, "name", "Doc1")[0].value).toBe(doc1.value);
}); });
it("works with array proxies", async () => { it("works with array proxies", async () => {
@ -1548,7 +1545,7 @@ const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
setLanguagePreferences("ru", "zh").using(observation, patient); setLanguagePreferences("ru", "zh").using(observation, patient);
expect(observation.langNotes).toBeUndefined(); expect(observation.langNotes).toBeUndefined();
expect(patient.langName?.length).toBe(0); expect(patient.langName?.size).toBe(0);
setLanguagePreferences("@other", "fr").using(observation, patient); setLanguagePreferences("@other", "fr").using(observation, patient);
expect(observation.langNotes).not.toBe("Notes Sympas"); expect(observation.langNotes).not.toBe("Notes Sympas");
@ -1556,7 +1553,7 @@ const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
setLanguagePreferences().using(observation, patient); setLanguagePreferences().using(observation, patient);
expect(observation.langNotes).toBe(undefined); expect(observation.langNotes).toBe(undefined);
expect(patient.langName?.length).toBe(0); expect(patient.langName?.size).toBe(0);
}); });
it("sets language strings based on the default language", async () => { it("sets language strings based on the default language", async () => {
@ -1574,7 +1571,7 @@ const testJsonldDatasetProxy = (patientContext: LdoJsonldContext) => () => {
es: "Notas Geniales", es: "Notas Geniales",
}); });
const patient = observation.subject as PatientShape; const patient = observation.subject as PatientShape;
patient.langName?.push("Luc"); patient.langName?.add("Luc");
expect(languagesOf(patient, "langName").fr?.has("Jean")).toBe(true); expect(languagesOf(patient, "langName").fr?.has("Jean")).toBe(true);
expect(languagesOf(patient, "langName").fr?.has("Luc")).toBe(true); expect(languagesOf(patient, "langName").fr?.has("Luc")).toBe(true);
expect(languagesOf(patient, "langName")["@none"]?.has("Jon")).toBe(true); expect(languagesOf(patient, "langName")["@none"]?.has("Jon")).toBe(true);

Loading…
Cancel
Save