|
| 1 | +const createStore = require('storeon'); |
| 2 | +const { bindStoreon } = require('.'); |
| 3 | + |
| 4 | +function connectFactory() { |
| 5 | + let counter = store => { |
| 6 | + store.on('@init', () => ({ count: 0, foo: 'baz' })); |
| 7 | + store.on('inc', ({ count }) => ({ count: count + 1 })); |
| 8 | + }; |
| 9 | + |
| 10 | + const store = createStore([counter]); |
| 11 | + |
| 12 | + return bindStoreon(store); |
| 13 | +} |
| 14 | + |
| 15 | +it('should start with init value', () => { |
| 16 | + const connect = connectFactory() |
| 17 | + |
| 18 | + const [_, count] = connect('count'); |
| 19 | + |
| 20 | + count.subscribe((value) => expect(value).toBe(0)); |
| 21 | +}); |
| 22 | + |
| 23 | +it('should be reactive', () => { |
| 24 | + const connect = connectFactory() |
| 25 | + |
| 26 | + let currentValue; |
| 27 | + const [dispatch, count] = connect('count'); |
| 28 | + |
| 29 | + count.subscribe((value) => currentValue = value); |
| 30 | + |
| 31 | + expect(currentValue).toBe(0); |
| 32 | + |
| 33 | + dispatch('inc'); |
| 34 | + |
| 35 | + expect(currentValue).toBe(1); |
| 36 | +}); |
| 37 | + |
| 38 | +it('should not emmit changes on other dispatches', () => { |
| 39 | + const connect = connectFactory() |
| 40 | + const spyCb = jest.fn(); |
| 41 | + |
| 42 | + const [dispatch, foo] = connect('foo'); |
| 43 | + |
| 44 | + foo.subscribe(spyCb); |
| 45 | + |
| 46 | + dispatch('inc'); |
| 47 | + |
| 48 | + expect(spyCb).toBeCalledWith('baz'); |
| 49 | + expect(spyCb).toBeCalledTimes(1); |
| 50 | +}); |
| 51 | + |
| 52 | +it('shoud to be unsubscribed', ()=> { |
| 53 | + const connect = connectFactory() |
| 54 | + |
| 55 | + let currentValue; |
| 56 | + const [dispatch, count] = connect('count'); |
| 57 | + |
| 58 | + const unsubscribe = count.subscribe((value) => currentValue = value); |
| 59 | + |
| 60 | + expect(currentValue).toBe(0); |
| 61 | + |
| 62 | + dispatch('inc'); |
| 63 | + |
| 64 | + expect(currentValue).toBe(1); |
| 65 | + |
| 66 | + unsubscribe(); |
| 67 | + unsubscribe(); |
| 68 | + dispatch('inc'); |
| 69 | + |
| 70 | + expect(currentValue).toBe(1); |
| 71 | +}); |
| 72 | + |
| 73 | + |
| 74 | + |
0 commit comments