Skip to content
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

Validate observables are unsubscribed #412

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion extend-expect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare namespace jest {
toEmit<V, E>(events: import('kefir-test-utils').Event<V, E>[], cb?: () => void): R
toEmitInTime<V, E>(
events: import('kefir-test-utils').EventWithTime<V, E>[],
cb?: (tick: (s: number) => void, clock: import('lolex').Clock) => void,
cb?: (tick: (s: number) => void, clock: import('@sinonjs/fake-timers').Clock) => void,
opts?: {reverseSimultaneous?: boolean; timeLimit?: number}
): R
toFlowErrors<V, E>(source?: import('kefir').Observable<V, E>): R
Expand Down
2 changes: 1 addition & 1 deletion jest-kefir.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Observable, Stream, Property} from 'kefir'
import {Helpers as KHelpers, Event} from 'kefir-test-utils'
import {Clock} from 'lolex'
import {Clock} from '@sinonjs/fake-timers'

export interface Helpers extends KHelpers {
extensions: jest.ExpectExtendMap
Expand Down
100 changes: 42 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@types/jest": "^27.0.3",
"@types/kefir": "^3.0.0",
"@types/node": "^14.0.1",
"kefir-test-utils": "^1.1.1"
"kefir-test-utils": "^2.0.0"
},
"peerDependencies": {
"jest": ">=24.0.0",
Expand Down
24 changes: 16 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const noop = () => {}

export default function jestKefir(Kefir) {
const helpers = createTestHelpers(Kefir)
const {activate, deactivate, send, error, watch, withFakeTime, watchWithTime} = helpers
const {activate, deactivate, error, send, watch, watchWithTime, withFakeTime} = helpers

const extensions = {
toBeObservable(received) {
Expand Down Expand Up @@ -79,13 +79,21 @@ export default function jestKefir(Kefir) {
},

toEmitInTime(received, expected, cb = noop, {timeLimit = 10000, reverseSimultaneous = false} = {}) {
let log = null

withFakeTime((tick, clock) => {
log = watchWithTime(received)
cb(tick, clock)
tick(timeLimit)
}, reverseSimultaneous)
let log,
unwatch = null

try {
withFakeTime((tick, clock) => {
// prettier-ignore
({log, unwatch} = watchWithTime(received))
cb(tick, clock)
tick(timeLimit)
}, reverseSimultaneous)
} catch (e) {
throw e
} finally {
unwatch()
}

const options = {
comment: 'Emitted values',
Expand Down
13 changes: 12 additions & 1 deletion test/jest-kefir.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import '../extend-expect'

const {prop, stream, pool, activate, deactivate, value, error, end, send} = KTU
const {activate, deactivate, end, error, observables, pool, prop, send, stream, value} = KTU

describe('jest-kefir', () => {
beforeEach(() => {
observables.clear()
})

afterEach(() => {
expect(observables.active.length).toEqual(0)
})

describe('toBeObservable', () => {
it('should match with stream', () => {
expect(stream()).toBeObservable()
Expand Down Expand Up @@ -83,12 +91,14 @@ describe('jest-kefir', () => {
const a = stream()
activate(a)
expect(a).toBeActiveObservable()
deactivate(a)
})

it('should match on active stream when called as method', () => {
const a = stream()
activate(a)
expect(a).toBeActiveObservable()
deactivate(a)
})

it('should negate on inactive property', () => {
Expand All @@ -99,6 +109,7 @@ describe('jest-kefir', () => {
const a = prop()
activate(a)
expect(a).toBeActiveObservable()
deactivate(a)
})

it('should negate on activated and deactivated property', () => {
Expand Down