Skip to content

Commit

Permalink
fix(toJSON): the json() method should be called once per response
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Apr 13, 2016
1 parent 1d49e48 commit 3c5c9a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/toJSON.js
Expand Up @@ -4,6 +4,8 @@ const Rx = require('rx')
const targ = require('argtoob')

module.exports = response => {
const json = response.flatMap(x => x.json())
return Rx.Observable.zip(json, response, targ('json', 'response'))
const json = response.flatMap(x => x.json()).publish()
json.connect()
const zip = Rx.Observable.zip(json, response, targ('json', 'response'))
return zip
}
17 changes: 15 additions & 2 deletions test/test.toJSON.js
Expand Up @@ -3,15 +3,15 @@
*/

'use strict'

import {spy} from 'sinon'
import {TestScheduler, ReactiveTest} from 'rx'
import test from 'ava'
import toJSON from '../src/toJSON'

const {onNext} = ReactiveTest
const createResponse = val => ({
status: 200,
json: () => [val + '-json']
json: spy(() => [val + '-json'])
})

test(t => {
Expand All @@ -30,3 +30,16 @@ test(t => {
onNext(230, {json: '2-json', response: mocks[2]})
])
})

test('json():callCount', t => {
const sh = new TestScheduler()
const mockResponse = createResponse(0)
const ob0 = sh.createObserver()
const ob1 = sh.createObserver()
const responses = sh.createHotObservable(onNext(210, mockResponse))
const json = toJSON(responses)
sh.scheduleAbsolute(null, 200, () => json.subscribe(ob0))
sh.scheduleAbsolute(null, 205, () => json.subscribe(ob1))
sh.start()
t.is(mockResponse.json.callCount, 1)
})

0 comments on commit 3c5c9a5

Please sign in to comment.