vue3-yuanma/packages/observer/__tests__/collections/WeakSet.spec.ts

91 lines
2.5 KiB
TypeScript
Raw Normal View History

2018-09-20 23:43:14 +08:00
import { observable, isObservable, autorun, unwrap } from '../../src'
describe('observer/collections', () => {
describe('WeakSet', () => {
it('instanceof', () => {
const original = new Set()
const observed = observable(original)
expect(isObservable(observed)).toBe(true)
expect(original instanceof Set).toBe(true)
expect(observed instanceof Set).toBe(true)
})
2018-09-20 23:43:14 +08:00
it('should observe mutations', () => {
let dummy
const value = {}
const set = observable(new WeakSet())
autorun(() => (dummy = set.has(value)))
2018-09-20 23:43:14 +08:00
expect(dummy).toBe(false)
set.add(value)
expect(dummy).toBe(true)
set.delete(value)
expect(dummy).toBe(false)
})
2018-09-20 23:43:14 +08:00
it('should not observe custom property mutations', () => {
let dummy
const set: any = observable(new WeakSet())
autorun(() => (dummy = set.customProp))
2018-09-20 23:43:14 +08:00
expect(dummy).toBe(undefined)
set.customProp = 'Hello World'
expect(dummy).toBe(undefined)
})
2018-09-20 23:43:14 +08:00
it('should not observe non value changing mutations', () => {
let dummy
const value = {}
const set = observable(new WeakSet())
const setSpy = jest.fn(() => (dummy = set.has(value)))
autorun(setSpy)
2018-09-20 23:43:14 +08:00
expect(dummy).toBe(false)
expect(setSpy).toHaveBeenCalledTimes(1)
set.add(value)
expect(dummy).toBe(true)
expect(setSpy).toHaveBeenCalledTimes(2)
set.add(value)
expect(dummy).toBe(true)
expect(setSpy).toHaveBeenCalledTimes(2)
set.delete(value)
expect(dummy).toBe(false)
expect(setSpy).toHaveBeenCalledTimes(3)
set.delete(value)
expect(dummy).toBe(false)
expect(setSpy).toHaveBeenCalledTimes(3)
})
2018-09-20 23:43:14 +08:00
it('should not observe raw data', () => {
const value = {}
let dummy
const set = observable(new WeakSet())
autorun(() => (dummy = unwrap(set).has(value)))
2018-09-20 23:43:14 +08:00
expect(dummy).toBe(false)
set.add(value)
expect(dummy).toBe(false)
})
it('should not be triggered by raw mutations', () => {
const value = {}
let dummy
const set = observable(new WeakSet())
autorun(() => (dummy = set.has(value)))
2018-09-20 23:43:14 +08:00
expect(dummy).toBe(false)
unwrap(set).add(value)
expect(dummy).toBe(false)
})
2018-09-20 23:43:14 +08:00
it('should not pollute original Set with Proxies', () => {
const set = new WeakSet()
const observed = observable(set)
const value = observable({})
observed.add(value)
expect(observed.has(value)).toBe(true)
expect(set.has(value)).toBe(false)
})
2018-09-20 23:43:14 +08:00
})
})