|
| 1 | +var React = require('react') |
| 2 | +var Nuclear = require('../src/main') |
| 3 | +var toImmutable = require('../src/main').toImmutable |
| 4 | + |
| 5 | +var testStore = new Nuclear.Store({ |
| 6 | + getInitialState() { |
| 7 | + return toImmutable({ |
| 8 | + count: 0, |
| 9 | + |
| 10 | + map: { |
| 11 | + key1: 'value1', |
| 12 | + multi: 2, |
| 13 | + } |
| 14 | + }) |
| 15 | + }, |
| 16 | + |
| 17 | + initialize() { |
| 18 | + this.on('increment', state => { |
| 19 | + var nextCount = state.get('count') |
| 20 | + return state.set('count', ++nextCount) |
| 21 | + }) |
| 22 | + |
| 23 | + this.on('set', (state, payload) => { |
| 24 | + return state.setIn(['map', payload.key], toImmutable(payload.value)) |
| 25 | + }) |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +describe('reactor.ReactMixin', () => { |
| 30 | + var mountNode |
| 31 | + var reactor |
| 32 | + var countGetter = ['test', 'count'] |
| 33 | + var key1Getter = ['test', 'map', 'key1'] |
| 34 | + var key2Getter = ['test', 'map', 'key2'] |
| 35 | + var multipliedGetter = [ |
| 36 | + countGetter, |
| 37 | + ['test', 'map', 'multi'], |
| 38 | + (count, multi) => count * multi |
| 39 | + ] |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + reactor = new Nuclear.Reactor({ |
| 43 | + debug: true, |
| 44 | + }) |
| 45 | + reactor.registerStores({ |
| 46 | + test: testStore |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + |
| 51 | + describe("when rendering a component with the flux.ReactMixin", () => { |
| 52 | + var component |
| 53 | + beforeEach(() => { |
| 54 | + mountNode = document.createElement('div') |
| 55 | + document.body.appendChild(mountNode) |
| 56 | + //var componentWillMountSpy = jasmine.createSpy() |
| 57 | + var Component = React.createClass({ |
| 58 | + mixins: [reactor.ReactMixin], |
| 59 | + |
| 60 | + getDataBindings() { |
| 61 | + return { |
| 62 | + count: countGetter, |
| 63 | + multiplied: multipliedGetter, |
| 64 | + key1: key1Getter, |
| 65 | + key2: key2Getter, |
| 66 | + } |
| 67 | + }, |
| 68 | + |
| 69 | + render() { |
| 70 | + return React.DOM.div(null, '') |
| 71 | + }, |
| 72 | + }) |
| 73 | + |
| 74 | + component = React.render(React.createElement(Component, null), mountNode) |
| 75 | + }) |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + React.unmountComponentAtNode(mountNode) |
| 79 | + document.body.removeChild(mountNode) |
| 80 | + }) |
| 81 | + |
| 82 | + it("should set the component initialState from `getDataBindings()`", () => { |
| 83 | + expect(component.state.count).toBe(0) |
| 84 | + expect(component.state.multiplied).toBe(0) |
| 85 | + expect(component.state.key1).toBe('value1') |
| 86 | + expect(component.state.key2).toBe(undefined) |
| 87 | + }) |
| 88 | + |
| 89 | + it("should update the state automatically when the underyling getters change", () => { |
| 90 | + reactor.dispatch('increment') |
| 91 | + |
| 92 | + expect(component.state.count).toBe(1) |
| 93 | + expect(component.state.multiplied).toBe(2) |
| 94 | + expect(component.state.key1).toBe('value1') |
| 95 | + expect(component.state.key2).toBe(undefined) |
| 96 | + |
| 97 | + reactor.dispatch('set', { |
| 98 | + key: 'key2', |
| 99 | + value: 'value2', |
| 100 | + }) |
| 101 | + |
| 102 | + expect(component.state.count).toBe(1) |
| 103 | + expect(component.state.multiplied).toBe(2) |
| 104 | + expect(component.state.key1).toBe('value1') |
| 105 | + expect(component.state.key2).toBe('value2') |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + describe("when rendering a component with a getInitialState() method", () => { |
| 110 | + var component |
| 111 | + beforeEach(() => { |
| 112 | + mountNode = document.createElement('div') |
| 113 | + document.body.appendChild(mountNode) |
| 114 | + //var componentWillMountSpy = jasmine.createSpy() |
| 115 | + var Component = React.createClass({ |
| 116 | + mixins: [reactor.ReactMixin], |
| 117 | + |
| 118 | + getInitialState() { |
| 119 | + return { |
| 120 | + foo: 'bar', |
| 121 | + } |
| 122 | + }, |
| 123 | + |
| 124 | + getDataBindings() { |
| 125 | + return { |
| 126 | + count: countGetter, |
| 127 | + multiplied: multipliedGetter, |
| 128 | + key1: key1Getter, |
| 129 | + key2: key2Getter, |
| 130 | + } |
| 131 | + }, |
| 132 | + |
| 133 | + render() { |
| 134 | + return React.DOM.div(null, '') |
| 135 | + }, |
| 136 | + }) |
| 137 | + |
| 138 | + component = React.render(React.createElement(Component, null), mountNode) |
| 139 | + }) |
| 140 | + |
| 141 | + afterEach(() => { |
| 142 | + React.unmountComponentAtNode(mountNode) |
| 143 | + document.body.removeChild(mountNode) |
| 144 | + }) |
| 145 | + |
| 146 | + it("should set the component initialState from `getDataBindings()` and getInitialState", () => { |
| 147 | + expect(component.state.foo).toBe('bar') |
| 148 | + expect(component.state.count).toBe(0) |
| 149 | + expect(component.state.multiplied).toBe(0) |
| 150 | + expect(component.state.key1).toBe('value1') |
| 151 | + expect(component.state.key2).toBe(undefined) |
| 152 | + }) |
| 153 | + }) |
| 154 | + |
| 155 | + describe("after unmounting the component", () => { |
| 156 | + var component |
| 157 | + beforeEach(() => { |
| 158 | + mountNode = document.createElement('div') |
| 159 | + document.body.appendChild(mountNode) |
| 160 | + //var componentWillMountSpy = jasmine.createSpy() |
| 161 | + var Component = React.createClass({ |
| 162 | + mixins: [reactor.ReactMixin], |
| 163 | + |
| 164 | + getInitialState() { |
| 165 | + return { |
| 166 | + foo: 'bar', |
| 167 | + } |
| 168 | + }, |
| 169 | + |
| 170 | + getDataBindings() { |
| 171 | + return { |
| 172 | + count: countGetter, |
| 173 | + multiplied: multipliedGetter, |
| 174 | + key1: key1Getter, |
| 175 | + key2: key2Getter, |
| 176 | + } |
| 177 | + }, |
| 178 | + |
| 179 | + render() { |
| 180 | + return React.DOM.div(null, '') |
| 181 | + }, |
| 182 | + }) |
| 183 | + |
| 184 | + component = React.render(React.createElement(Component, null), mountNode) |
| 185 | + }) |
| 186 | + |
| 187 | + afterEach(() => { |
| 188 | + document.body.removeChild(mountNode) |
| 189 | + }) |
| 190 | + |
| 191 | + it("should unobserve all getters", () => { |
| 192 | + React.unmountComponentAtNode(mountNode) |
| 193 | + expect(reactor.__changeObserver.__observers.length).toBe(0) |
| 194 | + }) |
| 195 | + }) |
| 196 | +}) |
0 commit comments