|
| 1 | +/* eslint-disable no-shadow */ |
1 | 2 | /* eslint-disable import/no-extraneous-dependencies */ |
2 | 3 | /* eslint-disable @typescript-eslint/no-unused-vars */ |
3 | 4 | /* eslint-disable @typescript-eslint/ban-ts-ignore */ |
@@ -26,6 +27,8 @@ import { |
26 | 27 | optional, |
27 | 28 | inject, |
28 | 29 | autobind, |
| 30 | + subscribe, |
| 31 | + watch, |
29 | 32 | } from '../..'; |
30 | 33 |
|
31 | 34 | let container: Element; |
@@ -168,6 +171,9 @@ describe('base API', () => { |
168 | 171 | const sum1ComputedFn = jest.fn(); |
169 | 172 | const getPropsFn = jest.fn(); |
170 | 173 |
|
| 174 | + const subscribeFn = jest.fn(); |
| 175 | + const watchFn = jest.fn(); |
| 176 | + |
171 | 177 | interface HomeView1Props { |
172 | 178 | version?: string; |
173 | 179 | text?: string; |
@@ -236,6 +242,12 @@ describe('base API', () => { |
236 | 242 | name: 'homeView', |
237 | 243 | }) |
238 | 244 | class HomeView extends HomeView1 { |
| 245 | + constructor() { |
| 246 | + super(); |
| 247 | + subscribe(this, subscribeFn); |
| 248 | + watch(this, () => this.state.list[0].count, watchFn); |
| 249 | + } |
| 250 | + |
239 | 251 | @action |
240 | 252 | increase(num: number) { |
241 | 253 | super.increase(num); |
@@ -393,6 +405,32 @@ describe('base API', () => { |
393 | 405 | container = document.createElement('div'); |
394 | 406 | document.body.appendChild(container); |
395 | 407 |
|
| 408 | + const subscribeFn1 = jest.fn(); |
| 409 | + const watchFn1 = jest.fn(); |
| 410 | + |
| 411 | + subscribe(app.instance.homeView, subscribeFn1); |
| 412 | + watch( |
| 413 | + app.instance.homeView, |
| 414 | + () => app.instance.homeView.state.list[0].count, |
| 415 | + watchFn1 |
| 416 | + ); |
| 417 | + |
| 418 | + expect(subscribeFn).toBeCalledTimes(4); |
| 419 | + expect(watchFn).toBeCalledTimes(3); |
| 420 | + expect(subscribeFn1).toBeCalledTimes(0); |
| 421 | + expect(watchFn1).toBeCalledTimes(0); |
| 422 | + app.instance.homeView.increase(1); |
| 423 | + expect(subscribeFn).toBeCalledTimes(5); |
| 424 | + expect(watchFn).toBeCalledTimes(4); |
| 425 | + expect(subscribeFn1).toBeCalledTimes(1); |
| 426 | + expect(watchFn1).toBeCalledTimes(1); |
| 427 | + app.destroy(); |
| 428 | + app.instance.homeView.increase(1); |
| 429 | + expect(subscribeFn).toBeCalledTimes(5); |
| 430 | + expect(watchFn).toBeCalledTimes(4); |
| 431 | + expect(subscribeFn1).toBeCalledTimes(1); |
| 432 | + expect(watchFn1).toBeCalledTimes(1); |
| 433 | + |
396 | 434 | const app1 = createApp({ |
397 | 435 | main: HomeView, |
398 | 436 | render, |
|
0 commit comments