dep.spec.js 1.3 KB

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