Skip to content

[WIP] Add recipe for hot observables #956

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

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 52 additions & 0 deletions docs/recipes/hot-observables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# How to test hot observables
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name it:

Testing hot observables


Ava supports autosubscribing to cold observables, which will not work for hot observables.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ava => AVA

For hot observables, we will need to first subscribe, and then trigger the values to be sent in the stream.
We can automatically unsubscribe by using `#take()`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify here what #take() is.


## Installing rxjs v5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Installing RxJS 5

`npm install --save rxjs`

## Create a source file named interactions.js

```
function create({Rx}) {
const writeSubject = new Rx.Subject()

function write(data) {
writeSubject.next(data)
}

return {
actions: {write},
events: {write: writeSubject.toObservable()}
}
}

export default {
create
}
```

## Now test it

```
import tests from 'Ava'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ava => ava

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests => test

import Rx from 'rxjs'
import interactionsFactory from './interactions'

tests(
`Given that we subscribe to the write event,
when the user calls the action write with 'hello'
then our subscriber recieves the message 'hello'`,
function onTest(test) {
const expected = 'hello'
const interactions = interactionsFactory.create({Rx})
interactions.events.write.take(1).subscribe(function(actual) {
test.is(actual, expected)
test.done()
})
interactions.actions.write(expected)
}
)
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use semi-colons, tab indentation.