|
| 1 | +import Dep from 'core/observer/dep' |
| 2 | + |
| 3 | +describe('Dep', () => { |
| 4 | + let dep |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + dep = new Dep() |
| 8 | + }) |
| 9 | + |
| 10 | + describe('instance', () => { |
| 11 | + it('should be created with correct properties', () => { |
| 12 | + expect(dep.subs.length).toBe(0) |
| 13 | + expect(new Dep().id).toBe(dep.id + 1) |
| 14 | + }) |
| 15 | + }) |
| 16 | + |
| 17 | + describe('addSub()', () => { |
| 18 | + it('should add sub', () => { |
| 19 | + dep.addSub(null) |
| 20 | + expect(dep.subs.length).toBe(1) |
| 21 | + expect(dep.subs[0]).toBe(null) |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + describe('removeSub()', () => { |
| 26 | + it('should remove sub', () => { |
| 27 | + dep.subs.push(null) |
| 28 | + dep.removeSub(null) |
| 29 | + expect(dep.subs.length).toBe(0) |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + describe('depend()', () => { |
| 34 | + let _target |
| 35 | + |
| 36 | + beforeAll(() => { |
| 37 | + _target = Dep.target |
| 38 | + }) |
| 39 | + |
| 40 | + afterAll(() => { |
| 41 | + Dep.target = _target |
| 42 | + }) |
| 43 | + |
| 44 | + it('should do nothing if no target', () => { |
| 45 | + Dep.target = null |
| 46 | + dep.depend() |
| 47 | + }) |
| 48 | + |
| 49 | + it('should add itself to target', () => { |
| 50 | + Dep.target = jasmine.createSpyObj('TARGET', ['addDep']) |
| 51 | + dep.depend() |
| 52 | + expect(Dep.target.addDep).toHaveBeenCalledWith(dep) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('notify()', () => { |
| 57 | + it('should notify subs', () => { |
| 58 | + dep.subs.push(jasmine.createSpyObj('SUB', ['update'])) |
| 59 | + dep.notify() |
| 60 | + expect(dep.subs[0].update).toHaveBeenCalled() |
| 61 | + }) |
| 62 | + }) |
| 63 | +}) |
0 commit comments