test: fix observer warning case

This commit is contained in:
Evan You 2018-10-01 18:40:44 -04:00
parent 612167f470
commit 5e988cc9fd

View File

@ -124,20 +124,36 @@ describe('observer/observable', () => {
}) })
test('unobservable values', () => { test('unobservable values', () => {
const msg = 'not observable' const warn = jest.spyOn(console, 'warn')
let lastMsg: string
warn.mockImplementation(msg => {
lastMsg = msg
})
const getMsg = (value: any) => `value is not observable: ${String(value)}`
const assertValue = (value: any) => {
observable(value)
expect(lastMsg).toMatch(getMsg(value))
}
// number // number
expect(() => observable(1)).toThrowError(msg) assertValue(1)
// string // string
expect(() => observable('foo')).toThrowError(msg) assertValue('foo')
// boolean // boolean
expect(() => observable(false)).toThrowError(msg) assertValue(false)
// null // null
expect(() => observable(null)).toThrowError(msg) assertValue(null)
// undefined should work because it returns empty object observable // undefined should work because it returns empty object observable
expect(() => observable(undefined)).not.toThrowError(msg) lastMsg = ''
observable(undefined)
expect(lastMsg).toBe('')
// symbol // symbol
const s = Symbol() const s = Symbol()
expect(() => observable(s)).toThrowError(msg) assertValue(s)
warn.mockRestore()
// built-ins should work and return same value // built-ins should work and return same value
const p = Promise.resolve() const p = Promise.resolve()
expect(observable(p)).toBe(p) expect(observable(p)).toBe(p)