-
Notifications
You must be signed in to change notification settings - Fork 9
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
Showing
9 changed files
with
335 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,203 @@ | ||
import { fake } from 'sinon' | ||
import { test } from 'uvu' | ||
import assert from 'uvu/assert' | ||
|
||
import { Exome } from '../exome' | ||
import { middleware, runMiddleware } from '../middleware' | ||
|
||
import { onAction } from './on-action' | ||
|
||
test.before.each(() => { | ||
middleware.splice(0, 100) | ||
}) | ||
|
||
test('exports `onAction`', () => { | ||
assert.ok(onAction) | ||
}) | ||
|
||
test('that `onAction` is function', () => { | ||
assert.instance(onAction, Function) | ||
}) | ||
|
||
test('adds before middleware without errors', () => { | ||
class Person extends Exome { | ||
constructor( | ||
public name?: string | ||
) { | ||
super() | ||
} | ||
|
||
public rename(name: string) { | ||
this.name = name | ||
} | ||
} | ||
|
||
const person = new Person('John') | ||
const handler = fake() | ||
|
||
onAction(Person, 'rename', handler, 'before') | ||
|
||
const after = runMiddleware(person, 'rename', [1]) | ||
|
||
assert.equal(handler.callCount, 1) | ||
assert.equal(handler.args[0], [person, 'rename', [1]]) | ||
|
||
after() | ||
|
||
assert.equal(handler.callCount, 1) | ||
}) | ||
|
||
test('adds after middleware without errors', () => { | ||
class Person extends Exome { | ||
constructor( | ||
public name?: string | ||
) { | ||
super() | ||
} | ||
|
||
public rename(name: string) { | ||
this.name = name | ||
} | ||
} | ||
|
||
const person = new Person('John') | ||
const handler = fake() | ||
|
||
onAction(Person, 'rename', handler, 'after') | ||
|
||
const after = runMiddleware(person, 'rename', [1]) | ||
|
||
assert.equal(handler.callCount, 0) | ||
|
||
after() | ||
|
||
assert.equal(handler.callCount, 1) | ||
assert.equal(handler.args[0], [person, 'rename', [1]]) | ||
}) | ||
|
||
test('calls NEW action correctly', async() => { | ||
class Person extends Exome { | ||
constructor( | ||
public name?: string | ||
) { | ||
super() | ||
} | ||
|
||
public rename(name: string) { | ||
this.name = name | ||
} | ||
} | ||
|
||
const handler = fake() | ||
onAction(Person, 'NEW', handler) | ||
|
||
assert.equal(handler.callCount, 0) | ||
|
||
// eslint-disable-next-line no-new | ||
new Person('John') | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 10)) | ||
|
||
assert.equal(handler.callCount, 1) | ||
assert.instance(handler.args[0][0], Person) | ||
assert.equal(handler.args[0][1], 'NEW') | ||
assert.equal(handler.args[0][2].length, 0) | ||
}) | ||
|
||
test('calls any action correctly', async() => { | ||
class Person extends Exome { | ||
constructor( | ||
public name?: string | ||
) { | ||
super() | ||
} | ||
|
||
public rename(name: string) { | ||
this.name = name | ||
} | ||
} | ||
|
||
const handler = fake() | ||
onAction(Person, null, handler) | ||
|
||
assert.equal(handler.callCount, 0) | ||
|
||
const person = new Person('John') | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 10)) | ||
|
||
assert.equal(handler.callCount, 1) | ||
assert.instance(handler.args[0][0], Person) | ||
assert.equal(handler.args[0][1], 'NEW') | ||
assert.equal(handler.args[0][2].length, 0) | ||
|
||
person.rename('Jane') | ||
|
||
assert.equal(handler.callCount, 2) | ||
assert.equal(handler.args[1], [person, 'rename', ['Jane']]) | ||
}) | ||
|
||
test('calls custom action correctly', () => { | ||
class Person extends Exome { | ||
constructor( | ||
public name?: string | ||
) { | ||
super() | ||
} | ||
|
||
public rename(name: string) { | ||
this.name = name | ||
} | ||
} | ||
|
||
const handler = fake() | ||
onAction(Person, 'rename', handler) | ||
|
||
assert.equal(handler.callCount, 0) | ||
|
||
const person = new Person('John') | ||
|
||
assert.equal(handler.callCount, 0) | ||
|
||
person.rename('Jane') | ||
|
||
assert.equal(handler.callCount, 1) | ||
assert.equal(handler.args[0], [person, 'rename', ['Jane']]) | ||
}) | ||
|
||
test('unsubscribes correctly', () => { | ||
class Person extends Exome { | ||
constructor( | ||
public name?: string | ||
) { | ||
super() | ||
} | ||
|
||
public rename(name: string) { | ||
this.name = name | ||
} | ||
} | ||
|
||
const handler = fake() | ||
const unsubscribe = onAction(Person, 'rename', handler) | ||
|
||
assert.equal(handler.callCount, 0) | ||
|
||
const person = new Person('John') | ||
|
||
assert.equal(handler.callCount, 0) | ||
|
||
person.rename('Jane') | ||
|
||
assert.equal(handler.callCount, 1) | ||
|
||
person.rename('Janine') | ||
|
||
assert.equal(handler.callCount, 2) | ||
|
||
unsubscribe() | ||
|
||
assert.equal(handler.callCount, 2) | ||
}) | ||
|
||
test.run() |
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 @@ | ||
import { Exome } from '../exome' | ||
import { addMiddleware } from '../middleware' | ||
|
||
type Unsubscribe = () => void | ||
|
||
export function onAction<T extends Exome>( | ||
Parent: new (...args: any[]) => T, | ||
action: null | 'NEW' | keyof T, | ||
callback: (instance: T, action: 'NEW' | keyof T, payload: any[]) => void, | ||
type: 'before' | 'after' = 'after' | ||
): Unsubscribe { | ||
return addMiddleware((instance, targetAction, payload) => { | ||
if (!(instance instanceof Parent && (targetAction === action || action === null))) { | ||
return | ||
} | ||
|
||
if (targetAction === 'NEW' || type === 'before') { | ||
callback(instance, targetAction as any, payload) | ||
return | ||
} | ||
|
||
return () => { | ||
callback(instance, targetAction as any, payload) | ||
} | ||
}) | ||
} |