Skip to content

test: plugin function with empty object return #15

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 4 commits into from
Jan 11, 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
6 changes: 2 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
node-version: 12
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: npm ci
- run: npm run build
6 changes: 2 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ jobs:
name: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
node-version: "12.x"
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: npm ci
- run: npm run build
- run: npx semantic-release
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
node-version: 12
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: npm ci
- run: npm test
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ import { Base } from "javascript-plugin-architecture-with-typescript-definitions

function myFooPlugin(instance: Base) {
return {
foo: () => "foo"
foo: () => "foo",
};
}

function myBarPlugin(instance: Base) {
return {
bar: () => "bar"
bar: () => "bar",
};
}

const FooTest = Base.plugin(myFooPlugin);
const fooTest = new FooTest();
fooTest.foo(); // has full TypeScript intellisense

const FooBarTest = Base.plugin([myFooPlugin, myBarPlugin]);
const FooBarTest = Base.plugin(myFooPlugin, myBarPlugin);
const fooBarTest = new FooBarTest();
fooBarTest.foo(); // has full TypeScript intellisense
fooBarTest.bar(); // has full TypeScript intellisense
Expand Down
25 changes: 16 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type Constructor<T> = new (...args: any[]) => T;
* @author https://stackoverflow.com/users/2887218/jcalz
* @see https://stackoverflow.com/a/50375286/10325032
*/
type UnionToIntersection<Union> = (Union extends any
? (argument: Union) => void
: never) extends (argument: infer Intersection) => void // tslint:disable-line: no-unused
type UnionToIntersection<Union> = (
Union extends any ? (argument: Union) => void : never
) extends (argument: infer Intersection) => void // tslint:disable-line: no-unused
? Intersection
: never;

Expand All @@ -31,16 +31,23 @@ export class Base {
static plugins: TestPlugin[] = [];
static plugin<
S extends Constructor<any> & { plugins: any[] },
T extends TestPlugin | TestPlugin[]
>(this: S, plugin: T) {
T1 extends TestPlugin,
T2 extends TestPlugin[]
>(this: S, plugin1: T1, ...additionalPlugins: T2) {
const currentPlugins = this.plugins;
let newPlugins: (TestPlugin | undefined)[] = [
plugin1,
...additionalPlugins,
];

const BaseWithPlugins = class extends this {
static plugins = currentPlugins.concat(plugin);
static plugins = currentPlugins.concat(
newPlugins.filter((plugin) => !currentPlugins.includes(plugin))
);
};

type Extension = ReturnTypeOf<T>;
return BaseWithPlugins as typeof BaseWithPlugins & Constructor<Extension>;
return BaseWithPlugins as typeof BaseWithPlugins &
Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
}

static defaults<S extends Constructor<any>>(this: S, defaults: Options) {
Expand All @@ -59,7 +66,7 @@ export class Base {
// apply plugins
// https://stackoverflow.com/a/16345172
const classConstructor = this.constructor as typeof Base;
classConstructor.plugins.forEach(plugin => {
classConstructor.plugins.forEach((plugin) => {
Object.assign(this, plugin(this, options));
});
}
Expand Down
25 changes: 19 additions & 6 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,38 @@ const fooPlugin = (test: Base) => {
console.log("plugin evalutes");

return {
foo: () => "foo"
foo: () => "foo",
};
};
const barPlugin = (test: Base) => {
console.log("plugin evalutes");

return {
bar: () => "bar"
bar: () => "bar",
};
};
const pluginWithEmptyObjectReturn = (test: Base) => {
return {};
};

describe("Base", () => {
it(".plugin(fooPlugin)", () => {
const FooTest = Base.plugin(fooPlugin);
const fooTest = new FooTest();
expect(fooTest.foo()).toEqual("foo");
});
it(".plugin([fooPlugin, barPlugin])", () => {
const FooBarTest = Base.plugin([fooPlugin, barPlugin]);
it(".plugin(fooPlugin, barPlugin)", () => {
const FooBarTest = Base.plugin(fooPlugin, barPlugin);
const fooBarTest = new FooBarTest();
expect(fooBarTest.foo()).toEqual("foo");
expect(fooBarTest.bar()).toEqual("bar");
});
it(".plugin(fooPlugin, barPlugin, pluginWithVoidReturn)", () => {
const FooBarTest = Base.plugin(
fooPlugin,
barPlugin,
pluginWithEmptyObjectReturn
);
const fooBarTest = new FooBarTest();
expect(fooBarTest.foo()).toEqual("foo");
expect(fooBarTest.bar()).toEqual("bar");
Expand All @@ -43,10 +56,10 @@ describe("Base", () => {

it(".plugin().defaults()", () => {
const BaseWithPluginAndDefaults = Base.plugin(fooPlugin).defaults({
baz: "daz"
baz: "daz",
});
const BaseWithDefaultsAndPlugin = Base.defaults({
baz: "daz"
baz: "daz",
}).plugin(fooPlugin);

const instance1 = new BaseWithPluginAndDefaults();
Expand Down