Skip to content

Instantly share code, notes, and snippets.

@debiasej
Created January 14, 2019 21:01
Show Gist options
  • Save debiasej/87b4e709858101e87444adb08472921d to your computer and use it in GitHub Desktop.
Save debiasej/87b4e709858101e87444adb08472921d to your computer and use it in GitHub Desktop.
Example of RxJS marble tests
import { cold } from 'jasmine-marbles'
import { Actions } from '@ngrx/effects'
import { of, throwError } from 'rxjs'
import * as appActions from 'app/app-redux/app/app.actions'
import * as vouchersActions from './vouchers.actions'
import { VouchersEffects } from './vouchers.effects'
const vouchersMock = require('./../../vouchers/mocks/vouchers.mock.json')
const campaignsMock = require('./../../vouchers/mocks/campaigns.mock.json')
describe('AppVouchersEffects', () => {
const fakeError = new Error('custom error')
describe('fetchCampaigns$ effect', () => {
it('should be dispatch FetchCampaignsSuccess', () => {
const serviceStub = jasmine.createSpyObj('VouchersService', { getCampaigns: of(campaignsMock) } )
const action = new vouchersActions.FetchCampaigns('customerId')
const completion = new vouchersActions.FetchCampaignsSuccess(campaignsMock.campaigns)
const source = cold('a', { a: action })
const expected = cold('b', { b: completion })
const effects = new VouchersEffects(new Actions(source), serviceStub)
expect(effects.fetchCampaigns$).toBeObservable(expected)
})
it('should be dispatch FetchCampaignsError', () => {
const serviceStub = jasmine.createSpyObj('VouchersService', { getCampaigns: throwError(fakeError) } )
const action = new vouchersActions.FetchCampaigns('customerId')
const completion = new vouchersActions.FetchCampaignsError(fakeError)
const source = cold('a', { a: action })
const expected = cold('b', { b: completion })
const effects = new VouchersEffects(new Actions(source), serviceStub)
expect(effects.fetchCampaigns$).toBeObservable(expected)
})
})
describe('fetchVouchers$ effect', () => {
it('should be dispatch FetchVouchersSuccess', () => {
const serviceStub = jasmine.createSpyObj('VouchersService', { getVouchers: of(vouchersMock) } )
const action = new vouchersActions.FetchVouchers('customerId')
const completion = new vouchersActions.FetchVouchersSuccess(vouchersMock.vouchers)
const source = cold('a', { a: action })
const expected = cold('b', { b: completion })
const effects = new VouchersEffects(new Actions(source), serviceStub)
expect(effects.fetchVouchers$).toBeObservable(expected)
})
it('should be dispatch FetchVouchersError', () => {
const serviceStub = jasmine.createSpyObj('VouchersService', { getVouchers: throwError(fakeError) } )
const action = new vouchersActions.FetchVouchers('customerId')
const completion = new vouchersActions.FetchVouchersError(fakeError)
const source = cold('a', { a: action })
const expected = cold('b', { b: completion })
const effects = new VouchersEffects(new Actions(source), serviceStub)
expect(effects.fetchVouchers$).toBeObservable(expected)
})
})
describe('exchangeVouchers$ effect', () => {
it('should be dispatch ExchangeVoucherSuccess', () => {
const serviceStub = jasmine.createSpyObj('VouchersService', { exchangeVouchers: of(vouchersMock.vouchers[0]) } )
const action = new vouchersActions.ExchangeVoucher('customerId')
const completion = new vouchersActions.ExchangeVoucherSuccess(vouchersMock.vouchers[0])
const source = cold('a', { a: action })
const expected = cold('b', { b: completion })
const effects = new VouchersEffects(new Actions(source), serviceStub)
expect(effects.exchangeVouchers$).toBeObservable(expected)
})
it('should be dispatch ExchangeVoucherError', () => {
const serviceStub = jasmine.createSpyObj('VouchersService', { exchangeVouchers: throwError(fakeError) } )
const action = new vouchersActions.ExchangeVoucher('customerId')
const source = cold('a', { a: action })
const expected = cold('(bc)', {
b: new vouchersActions.ExchangeVoucherError(fakeError),
c: new appActions.SetMessageError(`promotions.exchangeVoucherError`)
})
const effects = new VouchersEffects(new Actions(source), serviceStub)
expect(effects.exchangeVouchers$).toBeObservable(expected)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment