-
Notifications
You must be signed in to change notification settings - Fork 3
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
5 changed files
with
115 additions
and
0 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
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,18 @@ | ||
/** | ||
* Performs the side effect function `f` for each value emitted by the signal | ||
* `s`. | ||
* | ||
* @private | ||
*/ | ||
export default function tap (f, s) { | ||
return emit => { | ||
const subscription = s.subscribe({ ...emit, | ||
next (a) { | ||
f(a) | ||
emit.next(a) | ||
} | ||
}) | ||
|
||
return () => subscription.unsubscribe() | ||
} | ||
} |
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,57 @@ | ||
import { id } from 'fkit' | ||
|
||
import tap from './tap' | ||
import mockSignal from '../internal/mockSignal' | ||
|
||
let s | ||
let next, error, complete | ||
let emit | ||
|
||
describe('tap', () => { | ||
beforeEach(() => { | ||
s = mockSignal() | ||
|
||
next = jest.fn() | ||
error = jest.fn() | ||
complete = jest.fn() | ||
|
||
emit = { next, error, complete } | ||
}) | ||
|
||
it('performs a side effect', () => { | ||
const f = jest.fn() | ||
|
||
tap(f, s)(emit) | ||
|
||
expect(next).not.toHaveBeenCalled() | ||
s.next(1) | ||
expect(f).toHaveBeenLastCalledWith(1) | ||
expect(next).toHaveBeenCalledTimes(1) | ||
expect(next).toHaveBeenLastCalledWith(1) | ||
}) | ||
|
||
it('emits an error when the given signal emits an error', () => { | ||
tap(id, s)(emit) | ||
|
||
expect(error).not.toHaveBeenCalled() | ||
s.error('foo') | ||
expect(error).toHaveBeenCalledTimes(1) | ||
expect(error).toHaveBeenCalledWith('foo') | ||
}) | ||
|
||
it('completes when the given signal is completed', () => { | ||
tap(id, s)(emit) | ||
|
||
expect(complete).not.toHaveBeenCalled() | ||
s.complete() | ||
expect(complete).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('unmounts the given signal when the unsubscribe function is called', () => { | ||
const unsubscribe = tap(id, s)(emit) | ||
|
||
expect(s.unmount).not.toHaveBeenCalled() | ||
unsubscribe() | ||
expect(s.unmount).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |