-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backward-compatible support for
abort
- Loading branch information
1 parent
31bdc44
commit ee0b5e5
Showing
4 changed files
with
202 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ | |
"size-limit": [ | ||
{ | ||
"path": "dist/index.js", | ||
"limit": "200 B" | ||
"limit": "260 B" | ||
} | ||
], | ||
"jest": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,80 @@ | ||
import * as context from "./index"; | ||
import { performance } from "perf_hooks"; | ||
|
||
const testKey = Symbol("test"); | ||
|
||
describe("assign", () => { | ||
const ctx = context.background; | ||
|
||
it("should hold values", () => { | ||
const newCtx = context.withValue(ctx, "test", true); | ||
const anotherCtx = context.withValue(newCtx, testKey, 123); | ||
describe("withValue", () => { | ||
it("should hold values", () => { | ||
const newCtx = context.withValue(ctx, "test", true); | ||
const anotherCtx = context.withValue(newCtx, testKey, 123); | ||
|
||
expect(newCtx.value("test")).toEqual(true); | ||
expect(anotherCtx.value("test")).toEqual(true); | ||
expect(anotherCtx.value(testKey)).toEqual(123); | ||
expect(newCtx.value("test")).toEqual(true); | ||
expect(anotherCtx.value("test")).toEqual(true); | ||
expect(anotherCtx.value(testKey)).toEqual(123); | ||
}); | ||
|
||
it("should allow optional keys", () => { | ||
const test = (ctx: context.Context<{ prop?: boolean }>) => { | ||
return ctx.value("prop"); | ||
}; | ||
|
||
expect(test(ctx)).toEqual(undefined); | ||
expect(test(context.withValue(ctx, "prop", true))).toEqual(true); | ||
}); | ||
}); | ||
|
||
it("should allow optional keys", () => { | ||
const test = (ctx: context.Context<{ prop?: boolean }>) => { | ||
return ctx.value("prop"); | ||
}; | ||
describe("withAbort", () => { | ||
it("should allow abort", async () => { | ||
const fn = jest.fn(); | ||
const reason = new Error(); | ||
const [newCtx, abort] = context.withAbort(ctx); | ||
|
||
context.useAbort(ctx).catch(fn); | ||
context.useAbort(newCtx).catch(fn); | ||
|
||
expect(fn).toHaveBeenCalledTimes(0); | ||
|
||
abort(reason); | ||
|
||
await expect(context.useAbort(newCtx)).rejects.toBe(reason); | ||
|
||
expect(fn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should inherit abort", async () => { | ||
const reason = new Error(); | ||
const [newCtx, abort] = context.withAbort(ctx); | ||
const [anotherCtx] = context.withAbort(newCtx); | ||
|
||
abort(reason); | ||
|
||
await expect(context.useAbort(newCtx)).rejects.toBe(reason); | ||
await expect(context.useAbort(anotherCtx)).rejects.toBe(reason); | ||
}); | ||
}); | ||
|
||
describe("withTimeout", () => { | ||
it("should abort on a timeout", async () => { | ||
const start = performance.now(); | ||
const [newCtx] = context.withTimeout(ctx, 100); | ||
|
||
await expect(context.useAbort(newCtx)).rejects.toBeInstanceOf(Error); | ||
|
||
const end = performance.now(); | ||
|
||
expect(end - start).toBeGreaterThan(100); | ||
}); | ||
|
||
it("should allow manual abort", async () => { | ||
const reason = new Error(); | ||
const [newCtx, abort] = context.withTimeout(ctx, 100); | ||
|
||
abort(reason); | ||
|
||
expect(test(ctx)).toEqual(undefined); | ||
expect(test(context.withValue(ctx, "prop", true))).toEqual(true); | ||
await expect(context.useAbort(newCtx)).rejects.toBe(reason); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters