|
| 1 | +const Vue = require('vue/dist/vue.js') |
| 2 | +const VueRx = require('../dist/vue-rx.js') |
| 3 | +const Rx = require('rxjs') |
| 4 | + |
| 5 | +const miniRx = { |
| 6 | + Observable: Rx.Observable, |
| 7 | + Subscription: Rx.Subscription, |
| 8 | + Subject: Rx.Subject |
| 9 | +} |
| 10 | + |
| 11 | +Vue.config.productionTip = false |
| 12 | +Vue.use(VueRx, miniRx) |
| 13 | + |
| 14 | +const nextTick = Vue.nextTick |
| 15 | + |
| 16 | +function mock () { |
| 17 | + let observer |
| 18 | + const observable = Rx.Observable.create(_observer => { |
| 19 | + observer = _observer |
| 20 | + }) |
| 21 | + return { |
| 22 | + ob: observable, |
| 23 | + next: val => observer.next(val) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function trigger (target, event) { |
| 28 | + var e = document.createEvent('HTMLEvents') |
| 29 | + e.initEvent(event, true, true) |
| 30 | + target.dispatchEvent(e) |
| 31 | +} |
| 32 | + |
| 33 | +function click (target) { |
| 34 | + trigger(target, 'click') |
| 35 | +} |
| 36 | + |
| 37 | +test('expose $observables', () => { |
| 38 | + const { ob, next } = mock() |
| 39 | + |
| 40 | + const vm = new Vue({ |
| 41 | + subscriptions: { |
| 42 | + hello: ob.startWith(0) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + const results = [] |
| 47 | + vm.$observables.hello.subscribe(val => { |
| 48 | + results.push(val) |
| 49 | + }) |
| 50 | + |
| 51 | + next(1) |
| 52 | + next(2) |
| 53 | + next(3) |
| 54 | + expect(results).toEqual([0, 1, 2, 3]) |
| 55 | +}) |
| 56 | + |
| 57 | +test('bind subscriptions to render', done => { |
| 58 | + const { ob, next } = mock() |
| 59 | + |
| 60 | + const vm = new Vue({ |
| 61 | + subscriptions: { |
| 62 | + hello: ob.startWith('foo') |
| 63 | + }, |
| 64 | + render (h) { |
| 65 | + return h('div', this.hello) |
| 66 | + } |
| 67 | + }).$mount() |
| 68 | + |
| 69 | + expect(vm.$el.textContent).toBe('foo') |
| 70 | + |
| 71 | + next('bar') |
| 72 | + nextTick(() => { |
| 73 | + expect(vm.$el.textContent).toBe('bar') |
| 74 | + done() |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +test('subscriptions() has access to component state', () => { |
| 79 | + const { ob, next } = mock() |
| 80 | + |
| 81 | + const vm = new Vue({ |
| 82 | + data: { |
| 83 | + foo: 'FOO' |
| 84 | + }, |
| 85 | + props: ['bar'], |
| 86 | + propsData: { |
| 87 | + bar: 'BAR' |
| 88 | + }, |
| 89 | + subscriptions () { |
| 90 | + return { |
| 91 | + hello: ob.startWith(this.foo + this.bar) |
| 92 | + } |
| 93 | + }, |
| 94 | + render (h) { |
| 95 | + return h('div', this.hello) |
| 96 | + } |
| 97 | + }).$mount() |
| 98 | + |
| 99 | + expect(vm.$el.textContent).toBe('FOOBAR') |
| 100 | +}) |
| 101 | + |
| 102 | +test('v-stream directive (basic)', done => { |
| 103 | + const { ob, next } = mock() |
| 104 | + |
| 105 | + const vm = new Vue({ |
| 106 | + template: ` |
| 107 | + <div> |
| 108 | + <span class="count">{{ count }}</span> |
| 109 | + <button v-stream:click="click$">+</button> |
| 110 | + </div> |
| 111 | + `, |
| 112 | + domStreams: ['click$'], |
| 113 | + subscriptions () { |
| 114 | + return { |
| 115 | + count: this.click$.map(() => 1) |
| 116 | + .startWith(0) |
| 117 | + .scan((total, change) => total + change) |
| 118 | + } |
| 119 | + }, |
| 120 | + }).$mount() |
| 121 | + |
| 122 | + expect(vm.$el.querySelector('span').textContent).toBe('0') |
| 123 | + click(vm.$el.querySelector('button')) |
| 124 | + nextTick(() => { |
| 125 | + expect(vm.$el.querySelector('span').textContent).toBe('1') |
| 126 | + done() |
| 127 | + }) |
| 128 | +}) |
| 129 | + |
| 130 | +test('v-stream directive (with data)', done => { |
| 131 | + const { ob, next } = mock() |
| 132 | + |
| 133 | + const vm = new Vue({ |
| 134 | + data: { |
| 135 | + delta: -1 |
| 136 | + }, |
| 137 | + template: ` |
| 138 | + <div> |
| 139 | + <span class="count">{{ count }}</span> |
| 140 | + <button v-stream:click="{ subject: click$, data: delta }">+</button> |
| 141 | + </div> |
| 142 | + `, |
| 143 | + domStreams: ['click$'], |
| 144 | + subscriptions () { |
| 145 | + return { |
| 146 | + count: this.click$.pluck('data') |
| 147 | + .startWith(0) |
| 148 | + .scan((total, change) => total + change) |
| 149 | + } |
| 150 | + }, |
| 151 | + }).$mount() |
| 152 | + |
| 153 | + expect(vm.$el.querySelector('span').textContent).toBe('0') |
| 154 | + click(vm.$el.querySelector('button')) |
| 155 | + nextTick(() => { |
| 156 | + expect(vm.$el.querySelector('span').textContent).toBe('-1') |
| 157 | + vm.delta = 1 |
| 158 | + nextTick(() => { |
| 159 | + click(vm.$el.querySelector('button')) |
| 160 | + nextTick(() => { |
| 161 | + expect(vm.$el.querySelector('span').textContent).toBe('0') |
| 162 | + done() |
| 163 | + }) |
| 164 | + }) |
| 165 | + }) |
| 166 | +}) |
| 167 | + |
| 168 | +test('v-stream directive (multiple bindings on same node)', done => { |
| 169 | + const { ob, next } = mock() |
| 170 | + |
| 171 | + const vm = new Vue({ |
| 172 | + template: ` |
| 173 | + <div> |
| 174 | + <span class="count">{{ count }}</span> |
| 175 | + <button |
| 176 | + v-stream:click="{ subject: plus$, data: 1 }" |
| 177 | + v-stream:keyup="{ subject: plus$, data: -1 }">+</button> |
| 178 | + </div> |
| 179 | + `, |
| 180 | + domStreams: ['plus$'], |
| 181 | + subscriptions () { |
| 182 | + return { |
| 183 | + count: this.plus$.pluck('data') |
| 184 | + .startWith(0) |
| 185 | + .scan((total, change) => total + change) |
| 186 | + } |
| 187 | + }, |
| 188 | + }).$mount() |
| 189 | + |
| 190 | + expect(vm.$el.querySelector('span').textContent).toBe('0') |
| 191 | + click(vm.$el.querySelector('button')) |
| 192 | + nextTick(() => { |
| 193 | + expect(vm.$el.querySelector('span').textContent).toBe('1') |
| 194 | + trigger(vm.$el.querySelector('button'), 'keyup') |
| 195 | + nextTick(() => { |
| 196 | + expect(vm.$el.querySelector('span').textContent).toBe('0') |
| 197 | + done() |
| 198 | + }) |
| 199 | + }) |
| 200 | +}) |
| 201 | + |
| 202 | +test('$fromDOMEvent()', done => { |
| 203 | + const { ob, next } = mock() |
| 204 | + |
| 205 | + const vm = new Vue({ |
| 206 | + template: ` |
| 207 | + <div> |
| 208 | + <span class="count">{{ count }}</span> |
| 209 | + <button>+</button> |
| 210 | + </div> |
| 211 | + `, |
| 212 | + subscriptions () { |
| 213 | + const click$ = this.$fromDOMEvent('button', 'click') |
| 214 | + return { |
| 215 | + count: click$.map(() => 1) |
| 216 | + .startWith(0) |
| 217 | + .scan((total, change) => total + change) |
| 218 | + } |
| 219 | + } |
| 220 | + }).$mount() |
| 221 | + |
| 222 | + document.body.appendChild(vm.$el) |
| 223 | + expect(vm.$el.querySelector('span').textContent).toBe('0') |
| 224 | + click(vm.$el.querySelector('button')) |
| 225 | + nextTick(() => { |
| 226 | + expect(vm.$el.querySelector('span').textContent).toBe('1') |
| 227 | + done() |
| 228 | + }) |
| 229 | +}) |
| 230 | + |
| 231 | +test('$watchAsObservable()', done => { |
| 232 | + const vm = new Vue({ |
| 233 | + data: { |
| 234 | + count: 0 |
| 235 | + } |
| 236 | + }) |
| 237 | + |
| 238 | + const results = [] |
| 239 | + vm.$watchAsObservable('count').subscribe(change => { |
| 240 | + results.push(change) |
| 241 | + }) |
| 242 | + |
| 243 | + vm.count++ |
| 244 | + nextTick(() => { |
| 245 | + expect(results).toEqual([{ newValue: 1, oldValue: 0 }]) |
| 246 | + vm.count++ |
| 247 | + nextTick(() => { |
| 248 | + expect(results).toEqual([ |
| 249 | + { newValue: 1, oldValue: 0 }, |
| 250 | + { newValue: 2, oldValue: 1 } |
| 251 | + ]) |
| 252 | + done() |
| 253 | + }) |
| 254 | + }) |
| 255 | +}) |
| 256 | + |
| 257 | +test('$subscribeTo()', () => { |
| 258 | + const { ob, next } = mock() |
| 259 | + const results = [] |
| 260 | + const vm = new Vue({ |
| 261 | + created () { |
| 262 | + this.$subscribeTo(ob, count => { |
| 263 | + results.push(count) |
| 264 | + }) |
| 265 | + } |
| 266 | + }) |
| 267 | + |
| 268 | + next(1) |
| 269 | + expect(results).toEqual([1]) |
| 270 | + |
| 271 | + vm.$destroy() |
| 272 | + next(2) |
| 273 | + expect(results).toEqual([1]) // should not trigger anymore |
| 274 | +}) |
0 commit comments