From 5e988cc9fd64a7e1e263fdd61a00668cada52360 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 1 Oct 2018 18:40:44 -0400 Subject: [PATCH] test: fix observer warning case --- .../observer/__tests__/observable.spec.ts | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/observer/__tests__/observable.spec.ts b/packages/observer/__tests__/observable.spec.ts index d50d2ca323..bdf4a75a83 100644 --- a/packages/observer/__tests__/observable.spec.ts +++ b/packages/observer/__tests__/observable.spec.ts @@ -124,20 +124,36 @@ describe('observer/observable', () => { }) 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 - expect(() => observable(1)).toThrowError(msg) + assertValue(1) // string - expect(() => observable('foo')).toThrowError(msg) + assertValue('foo') // boolean - expect(() => observable(false)).toThrowError(msg) + assertValue(false) // null - expect(() => observable(null)).toThrowError(msg) + assertValue(null) // undefined should work because it returns empty object observable - expect(() => observable(undefined)).not.toThrowError(msg) + lastMsg = '' + observable(undefined) + expect(lastMsg).toBe('') // symbol const s = Symbol() - expect(() => observable(s)).toThrowError(msg) + assertValue(s) + + warn.mockRestore() + // built-ins should work and return same value const p = Promise.resolve() expect(observable(p)).toBe(p) -- 2.47.3