|
| 1 | +{ inspect } = require 'util' |
| 2 | + |
| 3 | +{ expect, assert } = require 'chai' |
| 4 | +{ createClass, mountComponent, createComponent, ObservableSelectorMixin } = require '..' |
| 5 | +{ makeAStore } = require './helpers/store' |
| 6 | + |
| 7 | +expectTestSequence = (tests) -> |
| 8 | + i = 0 |
| 9 | + { |
| 10 | + next: (x) -> expect(tests[i++]?(x)).to.equal(true) |
| 11 | + error: (x) -> throw x |
| 12 | + complete: -> expect(i).to.equal(tests.length) |
| 13 | + } |
| 14 | + |
| 15 | +describe 'observable selectors: ', -> |
| 16 | + store = makeAStore() |
| 17 | + |
| 18 | + RootComponent = null |
| 19 | + Subcomponent = null |
| 20 | + rootComponentInstance = null |
| 21 | + |
| 22 | + describe 'simple: ', -> |
| 23 | + it 'should create subcomponent class', -> |
| 24 | + Subcomponent = createClass { |
| 25 | + mixins: [ ObservableSelectorMixin ] |
| 26 | + displayName: 'Subcomponent' |
| 27 | + verbs: ['SET'] |
| 28 | + componentWillMount: -> |
| 29 | + console.log "Subcomponent.willMount: initial state", @state |
| 30 | + componentDidMount: -> |
| 31 | + console.log "Subcomponent.didMount" |
| 32 | + getReducer: (currentState) -> |
| 33 | + (state = {}, action) -> |
| 34 | + switch action.type |
| 35 | + when @SET then Object.assign({}, state, { payload: action.payload or {} }) |
| 36 | + else state |
| 37 | + actionDispatchers: { |
| 38 | + setValue: (val) -> { type: @SET, payload: val } |
| 39 | + } |
| 40 | + selectors: { |
| 41 | + getValue: (state) -> |
| 42 | + state.payload |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + it 'should create new store', -> |
| 47 | + store = makeAStore({ foo: { payload: 'bar' } }) |
| 48 | + |
| 49 | + it 'should mount subcomponent', -> |
| 50 | + rootComponentInstance = createComponent( { foo: Subcomponent } ) |
| 51 | + mountComponent(store, rootComponentInstance) |
| 52 | + |
| 53 | + it 'should print the whole component tree for your viewing pleasure', -> |
| 54 | + console.log(inspect(rootComponentInstance)) |
| 55 | + |
| 56 | + it 'should observe after mutation by an action', -> |
| 57 | + subscription = rootComponentInstance.foo.getValue.subscribe( |
| 58 | + expectTestSequence([ ((x) -> x is 'bar'), ((x) -> x is 'hello world')] ) |
| 59 | + ) |
| 60 | + rootComponentInstance.foo.setValue('hello world') |
| 61 | + subscription.unsubscribe() |
| 62 | + |
| 63 | + it 'should unsubscribe', -> |
| 64 | + subscription = rootComponentInstance.foo.getValue.subscribe( |
| 65 | + expectTestSequence([ ((x) -> x is 'hello world'), ((x) -> x is 'goodbye world')] ) |
| 66 | + ) |
| 67 | + rootComponentInstance.foo.setValue('goodbye world') |
| 68 | + subscription.unsubscribe() |
| 69 | + rootComponentInstance.foo.setValue('bar') |
0 commit comments