Commit a4687def authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[test] Added tests for libview/application

parent 91536d03bc1b
......@@ -21,13 +21,242 @@
import {
serializableResult,
RenderingResult,
NO_LANGUAGE
NO_LANGUAGE,
uriEquivalents,
newResourceFromUri,
cloneResource,
sameResources,
probeResourceWith,
Resource,
withTimeout
} from "./application";
import * as chai from "chai";
import { fail } from "assert";
const expect = chai.expect;
describe("@logilab/libview/application", () => {
it("should handle a promise that complete correctly", async () => {
let promise = new Promise<string>(
(resolve: (result: string) => void, reject: (reason: any) => void) => {
resolve("test");
}
);
let settled = false;
await withTimeout(promise, 100)
.then((value: string) => {
expect(value).equals("test");
settled = true;
})
.catch((reason: any) => {
fail("expected the promise to completed successfully");
});
expect(settled).equals(true);
});
it("should handle a promise that is rejected", async () => {
let promise = new Promise<string>(
(resolve: (result: string) => void, reject: (reason: any) => void) => {
reject("test");
}
);
let settled = false;
await withTimeout(promise, 100)
.then((value: string) => {
fail("expected the promise to be rejected");
})
.catch((reason: any) => {
expect(reason).equals("test");
settled = true;
});
expect(settled).equals(true);
});
it("should handle a promise that times out", async () => {
let promise = new Promise<string>(
(resolve: (result: string) => void, reject: (reason: any) => void) => {
// do nothing
}
);
let settled = false;
await withTimeout(promise, 100)
.then((value: string) => {
settled = true;
})
.catch((reason: any) => {
expect(reason).equals("timeout");
});
expect(settled).equals(false);
});
it("should correctly compute equivalent URIs on simple URI", () => {
let original = "http://google.com/";
let equivalents = uriEquivalents(original);
expect(equivalents.length).equals(1);
expect(equivalents[0]).equals(original);
});
it("should correctly compute equivalent URIs with accented characters", () => {
let original = "http://dbpedia.org/resource/Paul_Valéry";
let expected = [
original,
"http://dbpedia.org/resource/Paul_Val\u00E9ry",
"http://dbpedia.org/resource/Paul_Val%C3%A9ry"
];
let equivalents = uriEquivalents(original);
expect(equivalents.length).equals(expected.length);
expected.forEach((e: string) => {
expect(equivalents).contains(e);
});
});
it("should correctly compute equivalent URIs for already unicode escaped strings", () => {
let original = "http://dbpedia.org/resource/Paul_Val\u00E9ry";
let expected = [
original,
"http://dbpedia.org/resource/Paul_Valéry",
"http://dbpedia.org/resource/Paul_Val%C3%A9ry"
];
let equivalents = uriEquivalents(original);
expect(equivalents.length).equals(expected.length);
expected.forEach((e: string) => {
expect(equivalents).contains(e);
});
});
it("should correctly compute equivalent URIs for already percent encoded strings", () => {
let original = "http://dbpedia.org/resource/Paul_Val%C3%A9ry";
let expected = [
original,
"http://dbpedia.org/resource/Paul_Valéry",
"http://dbpedia.org/resource/Paul_Val\u00E9ry"
];
let equivalents = uriEquivalents(original);
expect(equivalents.length).equals(expected.length);
expected.forEach((e: string) => {
expect(equivalents).contains(e);
});
});
it("should fully clone resources", () => {
let original = "http://dbpedia.org/resource/Paul_Valéry";
let resource1 = newResourceFromUri(original);
let resource2 = cloneResource(resource1);
// modify resource 2
resource2.uri = "xxx";
resource2.uris.push("xxx");
expect(resource1.uri).equals(original);
expect(resource2.uris.length).equals(resource1.uris.length + 1);
});
it("should compare same resource with URIs in different order", () => {
let resource1 = newResourceFromUri(
"http://dbpedia.org/resource/Paul_Valéry"
);
let resource2 = newResourceFromUri(
"http://dbpedia.org/resource/Paul_Val%C3%A9ry"
);
expect(sameResources(resource1, resource2)).equals(true);
});
it("should compare different resources", () => {
let resource1 = newResourceFromUri(
"http://dbpedia.org/resource/Paul_Valéry"
);
let resource2 = newResourceFromUri(
"http://dbpedia.org/resource/Paul_Valery"
);
expect(sameResources(resource1, resource2)).equals(false);
});
it("should probe all URIs in a resource", async () => {
let resource = newResourceFromUri(
"http://dbpedia.org/resource/Paul_Valéry"
);
let probed: string[] = [];
let probe = function(uri: string): Promise<string> {
return new Promise<string>(
(resolve: (result: string) => void, reject: (reason: any) => void) => {
probed.push(uri);
resolve(uri);
}
);
};
await probeResourceWith(resource, probe)
.then((target: Resource) => {
expect(probed.length).equals(resource.uris.length);
resource.uris.forEach((uri: string) => {
expect(probed).contain(uri);
});
})
.catch((reason: any) => {
fail(reason);
});
});
it("should only return successfully probed URIs", async () => {
let resource = {
uri: "http://example.com/a",
uris: [
"http://example.com/a",
"http://example.com/b",
"http://example.com/c"
]
};
let expected = ["http://example.com/b", "http://example.com/c"];
let probe = function(uri: string): Promise<string> {
return new Promise<string>(
(resolve: (result: string) => void, reject: (reason: any) => void) => {
if (uri === "http://example.com/a") {
reject("none");
} else {
resolve(uri);
}
}
);
};
await probeResourceWith(resource, probe)
.then((target: Resource) => {
expect(target.uris.length).equals(expected.length);
target.uris.forEach((uri: string) => {
expect(expected).contain(uri);
});
expect(target.uris).contains(target.uri);
expect(target.uris).does.not.contain("http://example.com/a");
})
.catch((reason: any) => {
fail(reason);
});
});
it("should use a timeout when probing URIs", async () => {
let resource = {
uri: "http://example.com/a",
uris: ["http://example.com/a", "http://example.com/b"]
};
let expected = ["http://example.com/a"];
let probe = function(uri: string): Promise<string> {
return new Promise<string>(
(resolve: (result: string) => void, reject: (reason: any) => void) => {
if (uri === "http://example.com/a") {
resolve("http://example.com/a");
}
}
);
};
await probeResourceWith(resource, probe)
.then((target: Resource) => {
expect(target.uris.length).equals(expected.length);
target.uris.forEach((uri: string) => {
expect(expected).contain(uri);
});
expect(target.uris).contains(target.uri);
})
.catch((reason: any) => {
fail(reason);
});
});
it("should serialize a rendering result without the dom", () => {
let r1: RenderingResult = {
dom: new Object() as HTMLElement,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment