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

Update readme based on karet.util #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
78 changes: 50 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
# jest-kefir
# <a id="jest-kefir"></a> [≡](#contents) [jest-kefir](#jest-kefir) &middot; [![npm](https://img.shields.io/npm/dm/jest-kefir.svg)](https://www.npmjs.com/package/jest-kefir)

Jest plugin for asserting on Kefir Observables.
Jest plugin for asserting on [Kefir](https://kefirjs.github.io/kefir/) Observables.

[![npm version](https://badge.fury.io/js/jest-kefir.svg)](http://badge.fury.io/js/jest-kefir)
[![Build Status](https://github.com/kefirjs/jest-kefir/actions/workflows/continuous_integration.yml/badge.svg)](https://github.com/kefirjs/jest-kefir/actions?query=workflow%3ACI)

---

## How to Use
## <a id="contents"></a> [≡](#contents) [Contents](#contents)

* [How to use](#how-to-use)
* [Default export](#default-export)
* [`Factory: (Kefir) => PluginHelpers`](#factory)
* [Plugin helpers](#plugin-helpers)
* [`extensions: {[key: string]: Matcher}`](#extensions)
* [`activate: (obs: Kefir.Observable) => void`](#activate)
* [`deactivate: (obs: Kefir.Observable) => void`](#deactivate)
* [`send: (obs: Kefir.Observable, values: Array<Event<T>>) => obs`](#send)
* [`value: (value, options: ?{ current }) => Event<Value>`](#value)
* [`error: (error, options: ?{ current }) => Event<Error>`](#error)
* [`end: (options: ?{ current }) => Event<End>`](#end)
* [`stream: () => Kefir.Stream`](#stream)
* [`prop: () => Kefir.Property`](#prop)
* [`pool: () => Kefir.Pool`](#pool)
* [Assertions](#assertions)
* [`toBeObservable`](#tobeobservable)
* [`toBeProperty`](#tobeproperty)
* [`toBeStream`](#tobestream)
* [`toBePool`](#tobepool)
* [`toBeActiveObservable`](#tobeactiveobservable)
* [`toEmit`](#toemit)
* [`toEmitInTime`](#toemitintime)

## <a id="how-to-use"></a> [≡](#contents) How to use

Install with npm:

Expand Down Expand Up @@ -38,93 +62,91 @@ expect.extend(extensions)

All of the exported functions enable you to interact with Kefir Observables without needing to directly connect them to real or mock sources.

---

# API
### <a id="default-export"></a> [≡](#contents) [Default export](#default-export)

## `Factory: (Kefir) => PluginHelpers`
#### <a id="factory"></a> [≡](#contents) [`Factory: (Kefir) => PluginHelpers`](#factory)

The default export is a factory function that takes the application's Kefir instance returns an object of plugin helpers. Those helpers are documented below.

## `PluginHelpers`
### <a id="plugin-helpers"></a> [≡](#contents) [Plugin helpers](#plugin-helpers)

### `extensions: {[key: string]: Matcher}`
#### <a id="extensions"></a> [≡](#contents) [`extensions: {[key: string]: Matcher}`](#extensions)

The `extensions` object contains custom matchers for use with Jest. This object should be passed to Jest's `expect.extend`.

### `activate: (obs: Kefir.Observable) => void`
#### <a id="activate"></a> [≡](#contents) [`activate: (obs: Kefir.Observable) => void`](#activate)

`activate` is a simple helper function to turn a stream on.

### `deactivate: (obs: Kefir.Observable) => void`
#### <a id="deactivate"></a> [≡](#contents) [`deactivate: (obs: Kefir.Observable) => void`](#deactivate)

`deactivate` is a simple helper function to turn a stream off. It can turn off streams that were activated with `activate`. Streams turned on through other means (direct call to `on{Value|Error|End|Any}`, use of `observe`, etc.) need to be deactivated through their complementary mechanisms.

### `send: (obs: Kefir.Observable, values: Array<Event<T>>) => obs`
#### <a id="send"></a> [≡](#contents) [`send: (obs: Kefir.Observable, values: Array<Event<T>>) => obs`](#send)

`send` is a helper function for emitting values into a given observable. Note that the second parameter is an array of values to emit from the observable. The `Event` is generated by the `value`, `error`, and `end` functions. For all three of these functions, the optional `options` object is not needed.

### `value: (value, options: ?{ current }) => Event<Value>`
#### <a id="value"></a> [≡](#contents) [`value: (value, options: ?{ current }) => Event<Value>`](#value)

### `error: (error, options: ?{ current }) => Event<Error>`
#### <a id="error"></a> [≡](#contents) [`error: (error, options: ?{ current }) => Event<Error>`](#error)

### `end: (options: ?{ current }) => Event<End>`
#### <a id="end"></a> [≡](#contents) [`end: (options: ?{ current }) => Event<End>`](#end)

`value` and `error` take a value or error and an optional `options` object and return an `Event` object that can be passed to `send`, `emit`, or `emitInTime`. `end` does not take this value, as the `end` event in Kefir does not send a value with it.

When passing to `send`, the `options` object is ignored. `options` is used by `emit` and `emitInTime` (both described below) to determine whether the event should be treated as a `Kefir.Property`'s current event, error, or end.

### `stream: () => Kefir.Stream`
#### <a id="stream"></a> [≡](#contents) [`stream: () => Kefir.Stream`](#stream)

### `prop: () => Kefir.Property`
#### <a id="prop"></a> [≡](#contents) [`prop: () => Kefir.Property`](#prop)

### `pool: () => Kefir.Pool`
#### <a id="pool"></a> [≡](#contents) [`pool: () => Kefir.Pool`](#pool)

`stream`, `prop`, and `pool` are helper functions to create empty streams, properties, and pools. These can be used as mock sources to send values into. They have no other behavior.

## Assertions
### <a id="assertions"></a> [≡](#contents) [Assertions](#assertions)

### `toBeObservable`
#### <a id="tobeobservable"></a> [≡](#contents) [`toBeObservable`](#tobeobservable)

Asserts whether the expected value is a `Kefir.Observable`.

```js
expect(obs).toBeObservable()
```

### `toBeProperty`
#### <a id="tobeproperty"></a> [≡](#contents) [`toBeProperty`](#tobeproperty)

Asserts whether the expected value is a `Kefir.Property`.

```js
expect(obs).toBeProperty()
```

### `toBeStream`
#### <a id="tobestream"></a> [≡](#contents) [`toBeStream`](#tobestream)

Asserts whether the expected value is a `Kefir.Stream`.

```js
expect(obs).toBeStream()
```

### `toBePool`
#### <a id="tobepool"></a> [≡](#contents) [`toBePool`](#tobepool)

Asserts whether the expected value is a `Kefir.Pool`.

```js
expect(obs).toBePool()
```

### `toBeActiveObservable`
#### <a id="tobeactiveobservable"></a> [≡](#contents) [`toBeActiveObservable`](#tobeactiveobservable)

Asserts whether the expected value is an observable that is active.

```js
expect(obs).toBeActiveObservable()
```

### `toEmit`
#### <a id="toemit"></a> [≡](#contents) [`toEmit`](#toemit)

Asserts whether the provided observable emits the expected values synchronously. `toEmit` takes an array of values to match against and expects them to deep equal the values in the correct order.

Expand All @@ -148,7 +170,7 @@ expect(obs).to.emit([value(2, {current: true}), end()], () => {

These rules also apply to `toEmitInTime`.

### `toEmitInTime`
#### <a id="toemitintime"></a> [≡](#contents) [`toEmitInTime`](#toemitintime)

Asserts whether the provided emits the values correctly over time. Uses `lolex` behind the scenes to take over JavaScripts timers, allowing you to assert against the times the values are emitted. The expected value should be an array of tuples, where the first value is the time and the second is the value emitted.

Expand Down