Skip to content

feat: export test plugins #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
declare type Options = {
[key: string]: unknown;
};
export declare namespace Base {
type Options = {
[key: string]: unknown;
};
}

declare type ApiExtension = {
[key: string]: unknown;
};
declare type Plugin = (instance: Base, options: Options) => ApiExtension | void;
declare type Plugin = (
instance: Base,
options: Base.Options
) => ApiExtension | void;

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

export declare class Base<TOptions extends Options = Options> {
export declare class Base<TOptions extends Base.Options = Base.Options> {
static plugins: Plugin[];
static plugin<
S extends Constructor<any> & {
Expand All @@ -40,7 +46,7 @@ export declare class Base<TOptions extends Options = Options> {
plugins: any[];
} & Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
static defaults<
TDefaults extends Options,
TDefaults extends Base.Options,
S extends Constructor<Base<TDefaults>>
>(
this: S,
Expand Down
20 changes: 6 additions & 14 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import { expectType } from "tsd";
import { Base } from "./index.js";

const fooPlugin = () => {
return {
foo: "foo",
};
};
const barPlugin = () => {
return {
bar: "bar",
};
};

const voidPlugin = () => {
// returns void
};
import { fooPlugin } from "./plugins/foo/index.js";
import { barPlugin } from "./plugins/bar/index.js";
import { voidPlugin } from "./plugins/void/index.js";

const base = new Base();

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

expectType<string>(baseWithVoidAndNonVoidPlugins.foo);
expectType<string>(baseWithVoidAndNonVoidPlugins.bar);

// @ts-expect-error unknown properties cannot be used, see #31
baseWithVoidAndNonVoidPlugins.unknown;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "javascript-plugin-architecture-with-typescript-definitions",
"version": "0.0.0-development",
"type": "module",
"description": "Plugin architecture example with full TypeScript support",
"type": "module",
"exports": {
"./": "./index.js"
".": "./index.js",
"./plugins/foo": "./plugins/foo/index.js",
"./plugins/bar": "./plugins/bar/index.js",
"./plugins/void": "./plugins/void/index.js"
},
"types": "./index.d.ts",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions plugins/bar/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Base } from "../../index.js";

export function barPlugin(
base: Base,
options: Base.Options
): {
bar: string;
};
5 changes: 5 additions & 0 deletions plugins/bar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function barPlugin() {
return {
bar: "bar",
};
}
8 changes: 8 additions & 0 deletions plugins/foo/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Base } from "../../index.js";

export function fooPlugin(
base: Base,
options: Base.Options
): {
foo: string;
};
5 changes: 5 additions & 0 deletions plugins/foo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function fooPlugin() {
return {
foo: "foo",
};
}
3 changes: 3 additions & 0 deletions plugins/void/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Base } from "../../index.js";

export function voidPlugin(base: Base, options: Base.Options): void;
3 changes: 3 additions & 0 deletions plugins/void/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function voidPlugin() {
// returns void
}
43 changes: 14 additions & 29 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,32 @@ import { test } from "uvu";
import * as assert from "uvu/assert";

import { Base } from "../index.js";

const fooPlugin = (test) => {
return {
foo: () => "foo",
};
};
const barPlugin = (test) => {
return {
bar: () => "bar",
};
};
const pluginWithEmptyObjectReturn = (test) => {
return {};
};
import { fooPlugin } from "../plugins/foo/index.js";
import { barPlugin } from "../plugins/bar/index.js";
import { voidPlugin } from "../plugins/void/index.js";

test(".plugin(fooPlugin)", () => {
const FooTest = Base.plugin(fooPlugin);
const fooTest = new FooTest();
assert.equal(fooTest.foo(), "foo");
assert.equal(fooTest.foo, "foo");
});
test(".plugin(fooPlugin, barPlugin)", () => {
const FooBarTest = Base.plugin(fooPlugin, barPlugin);
const fooBarTest = new FooBarTest();
assert.equal(fooBarTest.foo(), "foo");
assert.equal(fooBarTest.bar(), "bar");
assert.equal(fooBarTest.foo, "foo");
assert.equal(fooBarTest.bar, "bar");
});
test(".plugin(fooPlugin, barPlugin, pluginWithVoidReturn)", () => {
const FooBarTest = Base.plugin(
fooPlugin,
barPlugin,
pluginWithEmptyObjectReturn
);
test(".plugin(fooPlugin, barPlugin, voidPlugin)", () => {
const FooBarTest = Base.plugin(fooPlugin, barPlugin, voidPlugin);
const fooBarTest = new FooBarTest();
assert.equal(fooBarTest.foo(), "foo");
assert.equal(fooBarTest.bar(), "bar");
assert.equal(fooBarTest.foo, "foo");
assert.equal(fooBarTest.bar, "bar");
});
test(".plugin(fooPlugin).plugin(barPlugin)", () => {
const FooBarTest = Base.plugin(fooPlugin).plugin(barPlugin);
const fooBarTest = new FooBarTest();
assert.equal(fooBarTest.foo(), "foo");
assert.equal(fooBarTest.bar(), "bar");
assert.equal(fooBarTest.foo, "foo");
assert.equal(fooBarTest.bar, "bar");
});
test(".defaults({foo: 'bar'})", () => {
const BaseWithDefaults = Base.defaults({ foo: "bar" });
Expand All @@ -63,9 +48,9 @@ test(".plugin().defaults()", () => {
const instance1 = new BaseWithPluginAndDefaults();
const instance2 = new BaseWithDefaultsAndPlugin();

assert.equal(instance1.foo(), "foo");
assert.equal(instance1.foo, "foo");
assert.equal(instance1.options, { baz: "daz" });
assert.equal(instance2.foo(), "foo");
assert.equal(instance2.foo, "foo");
assert.equal(instance2.options, { baz: "daz" });
});

Expand Down