Skip to content

chore: add unittests for core/observer/dep #7738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions test/unit/modules/observer/dep.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Dep from 'core/observer/dep'

describe('Dep', () => {
let dep

beforeEach(() => {
dep = new Dep()
})

describe('instance', () => {
it('should be created with correct properties', () => {
expect(dep.subs.length).toBe(0)
expect(new Dep().id).toBe(dep.id + 1)
})
})

describe('addSub()', () => {
it('should add sub', () => {
dep.addSub(null)
expect(dep.subs.length).toBe(1)
expect(dep.subs[0]).toBe(null)
})
})

describe('removeSub()', () => {
it('should remove sub', () => {
dep.subs.push(null)
dep.removeSub(null)
expect(dep.subs.length).toBe(0)
})
})

describe('depend()', () => {
let _target

beforeAll(() => {
_target = Dep.target
})

afterAll(() => {
Dep.target = _target
})

it('should do nothing if no target', () => {
Dep.target = null
dep.depend()
})

it('should add itself to target', () => {
Dep.target = jasmine.createSpyObj('TARGET', ['addDep'])
dep.depend()
expect(Dep.target.addDep).toHaveBeenCalledWith(dep)
})
})

describe('notify()', () => {
it('should notify subs', () => {
dep.subs.push(jasmine.createSpyObj('SUB', ['update']))
dep.notify()
expect(dep.subs[0].update).toHaveBeenCalled()
})
})
})