-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): create plugin system that allows to extend SVGuitar wi…
…th arbitrary functionality re #40
- Loading branch information
Voellmy Raphael
authored and
Voellmy Raphael
committed
Nov 28, 2020
1 parent
0d74f58
commit af28a21
Showing
4 changed files
with
85 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { SVGuitarChord } from './svguitar' | ||
import { setUpSvgDom } from '../test/testutils' | ||
|
||
const document = setUpSvgDom() | ||
|
||
describe('SVGuitarChord Plugin', () => { | ||
let container: HTMLElement | ||
|
||
beforeEach(() => { | ||
container = document.documentElement | ||
}) | ||
|
||
test('should apply a basic plugin', () => { | ||
// given | ||
const spy = jest.fn() | ||
function myFooPlugin(/* instance: SVGuitarChord */): { foo: jest.Mock } { | ||
return { | ||
foo: spy, | ||
} | ||
} | ||
|
||
// when | ||
const PluginTest = SVGuitarChord.plugin(myFooPlugin) | ||
const withPlugin = new PluginTest(container) | ||
withPlugin.foo() | ||
|
||
// then | ||
expect(spy).toHaveBeenCalledWith() | ||
}) | ||
}) |
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,26 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { SVGuitarChord } from './svguitar' | ||
|
||
export type ApiExtension = { [key: string]: any } | ||
|
||
export type Constructor<T> = new (...args: any[]) => T | ||
|
||
export type AnyFunction = (...args: any) => any | ||
|
||
/** | ||
* @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 | ||
? Intersection | ||
: never | ||
|
||
export type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> = T extends AnyFunction | ||
? ReturnType<T> | ||
: T extends AnyFunction[] | ||
? UnionToIntersection<ReturnType<T[number]>> | ||
: never | ||
|
||
export type SVGuitarPlugin = (instance: SVGuitarChord) => ApiExtension | undefined |
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