forked from ydb-platform/ydb-nodejs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexey Zorkaltsev
committed
Apr 8, 2024
1 parent
b7e397d
commit 8bdc0a6
Showing
13 changed files
with
804 additions
and
43 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
File renamed without changes.
File renamed without changes.
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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {Context, setContextIdGenerator} from "../../context/Context"; | ||
|
||
describe('Context.do', () => { | ||
beforeEach(() => { | ||
setContextIdGenerator(); // reset default | ||
}); | ||
|
||
it('general', () => { | ||
const ctx = Context.createNew().ctx; | ||
|
||
function test(n: number) { | ||
expect(Context.get()).toBeDefined(); | ||
expect(n).toBe(12); | ||
return n; | ||
} | ||
|
||
const res = ctx.do(() => test(12)); | ||
|
||
expect(res).toBe(12); | ||
}); | ||
|
||
it('async', async () => { | ||
const ctx = Context.createNew().ctx; | ||
|
||
async function test(n: number) { | ||
expect(Context.get()).toBeDefined(); | ||
await new Promise((resolve) =>{ | ||
setTimeout(resolve, 0); | ||
}); | ||
expect(() => Context.get()).toThrow(); // because it's after await | ||
expect(n).toBe(12); | ||
return n; | ||
} | ||
|
||
const res = await ctx.do(() => test(12)); | ||
|
||
expect(res).toBe(12); | ||
}); | ||
|
||
it('no current context', async () => { | ||
expect(() => Context.get()) | ||
.toThrow(`"ctx" was either not passed through Context.do() or was already taken through Context.get()`); | ||
}); | ||
|
||
it('unprocessed current context', async () => { | ||
const ctx = Context.createNew().ctx; | ||
|
||
ctx.do(() => { | ||
expect(() => ctx.do(() => {})) | ||
.toThrow(`There is a "ctx" that was passed through "ctx.do()" and was not processed till next "ctx.do()": 0001`) | ||
}) | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import {Context} from "../../context/Context"; | ||
import {EnsureContext} from "../../context/ensureContext"; | ||
|
||
describe('ensureContext', () => { | ||
it('positional args', () => { | ||
class Test { | ||
// @ts-ignore | ||
noArgs(): void; | ||
noArgs(ctx: Context): void; | ||
@EnsureContext(true) | ||
noArgs(ctx: Context): void { | ||
expect(ctx instanceof Context).toBeTruthy(); | ||
} | ||
|
||
// @ts-ignore | ||
posArgs(n: number, s: string): void; | ||
posArgs(ctx: Context, n: number, s: string): void; | ||
@EnsureContext(true) | ||
posArgs(ctx: Context, n: number, s: string) { | ||
expect(ctx instanceof Context).toBeTruthy(); | ||
expect(n).toBe(12); | ||
expect(s).toBe('test'); | ||
} | ||
|
||
// @ts-ignore | ||
static staticNoArgs(): void; | ||
static staticNoArgs(ctx: Context): void; | ||
|
||
@EnsureContext(true) | ||
static staticNoArgs(ctx: Context) { | ||
expect(ctx instanceof Context).toBeTruthy(); | ||
} | ||
} | ||
|
||
const test = new Test(); | ||
|
||
test.noArgs(); | ||
test.noArgs(Context.createNew().ctx); | ||
|
||
test.posArgs(12, 'test'); | ||
test.posArgs(Context.createNew().ctx, 12, 'test'); | ||
|
||
Test.staticNoArgs(); | ||
}); | ||
|
||
it('named args', () => { | ||
class Test { | ||
// noArgs(): void; | ||
// noArgs(opts: { | ||
// ctx?: Context, | ||
// }): void; | ||
@EnsureContext() | ||
noArgs(opts?: { | ||
ctx?: Context, | ||
}): void { | ||
const ctx = opts!.ctx!; | ||
expect(ctx instanceof Context).toBeTruthy(); | ||
} | ||
|
||
@EnsureContext(false) // should throw error cause fire arg is not obj | ||
mismatchTypeOfArgs(n: number, s: string) { | ||
expect(n).toBe(12); | ||
expect(s).toBe('test'); | ||
} | ||
} | ||
|
||
const test = new Test(); | ||
|
||
test.noArgs(); | ||
test.noArgs({}); | ||
test.noArgs({ | ||
ctx: Context.createNew().ctx, | ||
}); | ||
|
||
expect(() => test.mismatchTypeOfArgs(12, 'test')).rejects | ||
.toThrow('An object with options or undefined is expected as the first argument'); | ||
}); | ||
}); |
Oops, something went wrong.