Skip to content

Commit 4c8cec9

Browse files
authored
feat: export test plugins (#54)
1 parent 1836763 commit 4c8cec9

File tree

10 files changed

+69
-51
lines changed

10 files changed

+69
-51
lines changed

Diff for: index.d.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
declare type Options = {
2-
[key: string]: unknown;
3-
};
1+
export declare namespace Base {
2+
type Options = {
3+
[key: string]: unknown;
4+
};
5+
}
6+
47
declare type ApiExtension = {
58
[key: string]: unknown;
69
};
7-
declare type Plugin = (instance: Base, options: Options) => ApiExtension | void;
10+
declare type Plugin = (
11+
instance: Base,
12+
options: Base.Options
13+
) => ApiExtension | void;
814

915
declare type Constructor<T> = new (...args: any[]) => T;
1016
/**
@@ -24,7 +30,7 @@ declare type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> =
2430
? UnionToIntersection<Exclude<ReturnType<T[number]>, void>>
2531
: never;
2632

27-
export declare class Base<TOptions extends Options = Options> {
33+
export declare class Base<TOptions extends Base.Options = Base.Options> {
2834
static plugins: Plugin[];
2935
static plugin<
3036
S extends Constructor<any> & {
@@ -40,7 +46,7 @@ export declare class Base<TOptions extends Options = Options> {
4046
plugins: any[];
4147
} & Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
4248
static defaults<
43-
TDefaults extends Options,
49+
TDefaults extends Base.Options,
4450
S extends Constructor<Base<TDefaults>>
4551
>(
4652
this: S,

Diff for: index.test-d.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
import { expectType } from "tsd";
22
import { Base } from "./index.js";
33

4-
const fooPlugin = () => {
5-
return {
6-
foo: "foo",
7-
};
8-
};
9-
const barPlugin = () => {
10-
return {
11-
bar: "bar",
12-
};
13-
};
14-
15-
const voidPlugin = () => {
16-
// returns void
17-
};
4+
import { fooPlugin } from "./plugins/foo/index.js";
5+
import { barPlugin } from "./plugins/bar/index.js";
6+
import { voidPlugin } from "./plugins/void/index.js";
187

198
const base = new Base();
209

@@ -56,3 +45,6 @@ const baseWithVoidAndNonVoidPlugins = new BaseWithVoidAndNonVoidPlugins();
5645

5746
expectType<string>(baseWithVoidAndNonVoidPlugins.foo);
5847
expectType<string>(baseWithVoidAndNonVoidPlugins.bar);
48+
49+
// @ts-expect-error unknown properties cannot be used, see #31
50+
baseWithVoidAndNonVoidPlugins.unknown;

Diff for: package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
22
"name": "javascript-plugin-architecture-with-typescript-definitions",
33
"version": "0.0.0-development",
4-
"type": "module",
54
"description": "Plugin architecture example with full TypeScript support",
5+
"type": "module",
66
"exports": {
7-
"./": "./index.js"
7+
".": "./index.js",
8+
"./plugins/foo": "./plugins/foo/index.js",
9+
"./plugins/bar": "./plugins/bar/index.js",
10+
"./plugins/void": "./plugins/void/index.js"
811
},
912
"types": "./index.d.ts",
1013
"scripts": {

Diff for: plugins/bar/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Base } from "../../index.js";
2+
3+
export function barPlugin(
4+
base: Base,
5+
options: Base.Options
6+
): {
7+
bar: string;
8+
};

Diff for: plugins/bar/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function barPlugin() {
2+
return {
3+
bar: "bar",
4+
};
5+
}

Diff for: plugins/foo/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Base } from "../../index.js";
2+
3+
export function fooPlugin(
4+
base: Base,
5+
options: Base.Options
6+
): {
7+
foo: string;
8+
};

Diff for: plugins/foo/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function fooPlugin() {
2+
return {
3+
foo: "foo",
4+
};
5+
}

Diff for: plugins/void/index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Base } from "../../index.js";
2+
3+
export function voidPlugin(base: Base, options: Base.Options): void;

Diff for: plugins/void/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function voidPlugin() {
2+
// returns void
3+
}

Diff for: test/base.test.js

+14-29
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,32 @@ import { test } from "uvu";
22
import * as assert from "uvu/assert";
33

44
import { Base } from "../index.js";
5-
6-
const fooPlugin = (test) => {
7-
return {
8-
foo: () => "foo",
9-
};
10-
};
11-
const barPlugin = (test) => {
12-
return {
13-
bar: () => "bar",
14-
};
15-
};
16-
const pluginWithEmptyObjectReturn = (test) => {
17-
return {};
18-
};
5+
import { fooPlugin } from "../plugins/foo/index.js";
6+
import { barPlugin } from "../plugins/bar/index.js";
7+
import { voidPlugin } from "../plugins/void/index.js";
198

209
test(".plugin(fooPlugin)", () => {
2110
const FooTest = Base.plugin(fooPlugin);
2211
const fooTest = new FooTest();
23-
assert.equal(fooTest.foo(), "foo");
12+
assert.equal(fooTest.foo, "foo");
2413
});
2514
test(".plugin(fooPlugin, barPlugin)", () => {
2615
const FooBarTest = Base.plugin(fooPlugin, barPlugin);
2716
const fooBarTest = new FooBarTest();
28-
assert.equal(fooBarTest.foo(), "foo");
29-
assert.equal(fooBarTest.bar(), "bar");
17+
assert.equal(fooBarTest.foo, "foo");
18+
assert.equal(fooBarTest.bar, "bar");
3019
});
31-
test(".plugin(fooPlugin, barPlugin, pluginWithVoidReturn)", () => {
32-
const FooBarTest = Base.plugin(
33-
fooPlugin,
34-
barPlugin,
35-
pluginWithEmptyObjectReturn
36-
);
20+
test(".plugin(fooPlugin, barPlugin, voidPlugin)", () => {
21+
const FooBarTest = Base.plugin(fooPlugin, barPlugin, voidPlugin);
3722
const fooBarTest = new FooBarTest();
38-
assert.equal(fooBarTest.foo(), "foo");
39-
assert.equal(fooBarTest.bar(), "bar");
23+
assert.equal(fooBarTest.foo, "foo");
24+
assert.equal(fooBarTest.bar, "bar");
4025
});
4126
test(".plugin(fooPlugin).plugin(barPlugin)", () => {
4227
const FooBarTest = Base.plugin(fooPlugin).plugin(barPlugin);
4328
const fooBarTest = new FooBarTest();
44-
assert.equal(fooBarTest.foo(), "foo");
45-
assert.equal(fooBarTest.bar(), "bar");
29+
assert.equal(fooBarTest.foo, "foo");
30+
assert.equal(fooBarTest.bar, "bar");
4631
});
4732
test(".defaults({foo: 'bar'})", () => {
4833
const BaseWithDefaults = Base.defaults({ foo: "bar" });
@@ -63,9 +48,9 @@ test(".plugin().defaults()", () => {
6348
const instance1 = new BaseWithPluginAndDefaults();
6449
const instance2 = new BaseWithDefaultsAndPlugin();
6550

66-
assert.equal(instance1.foo(), "foo");
51+
assert.equal(instance1.foo, "foo");
6752
assert.equal(instance1.options, { baz: "daz" });
68-
assert.equal(instance2.foo(), "foo");
53+
assert.equal(instance2.foo, "foo");
6954
assert.equal(instance2.options, { baz: "daz" });
7055
});
7156

0 commit comments

Comments
 (0)