|
| 1 | +/* global describe, it */ |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { reduxObservable } from 'redux-observable'; |
| 4 | +import { dispatchOnMount } from '../'; |
| 5 | +import { Component } from 'react'; |
| 6 | +import { createStore, applyMiddleware } from 'redux'; |
| 7 | +import * as Rx from 'rxjs'; |
| 8 | +import 'babel-polyfill'; |
| 9 | + |
| 10 | +const { Observable } = Rx; |
| 11 | + |
| 12 | +describe('dispatchOnMount', () => { |
| 13 | + it('should exist', () => { |
| 14 | + expect(dispatchOnMount).to.be.a('function'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should wire a thunkservable to dispatch on componentDidMount', () => { |
| 18 | + let reducedActions = []; |
| 19 | + let reducer = (state, action) => { |
| 20 | + reducedActions.push(action); |
| 21 | + return state; |
| 22 | + }; |
| 23 | + let store = createStore(reducer, applyMiddleware(reduxObservable())); |
| 24 | + |
| 25 | + @dispatchOnMount(() => Observable.of({ type: 'TEST' })) |
| 26 | + class TestComponent extends Component { |
| 27 | + } |
| 28 | + |
| 29 | + let comp = new TestComponent(); |
| 30 | + // fake connection? |
| 31 | + comp.context = { store }; |
| 32 | + comp.componentDidMount(); |
| 33 | + |
| 34 | + expect(reducedActions).to.deep.equal([{ type: '@@redux/INIT' }, { type: 'TEST' }]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should unsubscribe on componentWillUnmount', () => { |
| 38 | + let reducedActions = []; |
| 39 | + let reducer = (state, action) => { |
| 40 | + reducedActions.push(action); |
| 41 | + return state; |
| 42 | + }; |
| 43 | + let store = createStore(reducer, applyMiddleware(reduxObservable())); |
| 44 | + |
| 45 | + let source1Unsubbed = false; |
| 46 | + let source1 = new Observable((observer) => { |
| 47 | + return () => { |
| 48 | + source1Unsubbed = true; |
| 49 | + }; |
| 50 | + }); |
| 51 | + |
| 52 | + let source2Unsubbed = false; |
| 53 | + let source2 = new Observable((observer) => { |
| 54 | + return () => { |
| 55 | + source2Unsubbed = true; |
| 56 | + }; |
| 57 | + }); |
| 58 | + |
| 59 | + @dispatchOnMount(() => source1, () => source2) |
| 60 | + class TestComponent extends Component { |
| 61 | + } |
| 62 | + |
| 63 | + let comp = new TestComponent(); |
| 64 | + // fake connection? |
| 65 | + comp.context = { store }; |
| 66 | + comp.componentDidMount(); |
| 67 | + |
| 68 | + expect(source1Unsubbed).to.equal(false); |
| 69 | + expect(source2Unsubbed).to.equal(false); |
| 70 | + |
| 71 | + comp.componentWillUnmount(); |
| 72 | + |
| 73 | + expect(source1Unsubbed).to.equal(true); |
| 74 | + expect(source2Unsubbed).to.equal(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should subscribe to multiple thunkservables', () => { |
| 78 | + let reducedActions = []; |
| 79 | + let reducer = (state, action) => { |
| 80 | + reducedActions.push(action); |
| 81 | + return state; |
| 82 | + }; |
| 83 | + let store = createStore(reducer, applyMiddleware(reduxObservable())); |
| 84 | + |
| 85 | + let source1 = Observable.of({ type: 'SOURCE1' }); |
| 86 | + let source2 = Observable.of({ type: 'SOURCE2' }); |
| 87 | + |
| 88 | + @dispatchOnMount(() => source1, () => source2) |
| 89 | + class TestComponent extends Component { |
| 90 | + } |
| 91 | + |
| 92 | + let comp = new TestComponent(); |
| 93 | + // fake connection? |
| 94 | + comp.context = { store }; |
| 95 | + comp.componentDidMount(); |
| 96 | + |
| 97 | + expect(reducedActions).to.deep.equal([ |
| 98 | + { type: '@@redux/INIT' }, |
| 99 | + { type: 'SOURCE1' }, |
| 100 | + { type: 'SOURCE2' } |
| 101 | + ]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should allow normal actions to dispatch on mount', () => { |
| 105 | + let reducedActions = []; |
| 106 | + let reducer = (state, action) => { |
| 107 | + reducedActions.push(action); |
| 108 | + return state; |
| 109 | + }; |
| 110 | + let store = createStore(reducer, applyMiddleware(reduxObservable())); |
| 111 | + |
| 112 | + let source2 = Observable.of({ type: 'SOURCE2' }); |
| 113 | + |
| 114 | + @dispatchOnMount({ type: 'PLAIN_ACTION' }, () => source2) |
| 115 | + class TestComponent extends Component { |
| 116 | + } |
| 117 | + |
| 118 | + let comp = new TestComponent(); |
| 119 | + // fake connection? |
| 120 | + comp.context = { store }; |
| 121 | + comp.componentDidMount(); |
| 122 | + |
| 123 | + expect(reducedActions).to.deep.equal([ |
| 124 | + { type: '@@redux/INIT' }, |
| 125 | + { type: 'PLAIN_ACTION' }, |
| 126 | + { type: 'SOURCE2' } |
| 127 | + ]); |
| 128 | + |
| 129 | + // since plain actions don't return subscriptions, because they're not functions |
| 130 | + // let's just be really sure we don't break the unsub in the componentWillUnmount |
| 131 | + expect(() => comp.componentWillUnmount()).not.to.throw(); |
| 132 | + }); |
| 133 | +}); |
0 commit comments