|
| 1 | +import React from 'react' |
| 2 | +import { create } from 'react-test-renderer' |
| 3 | +import * as Sinon from 'sinon' |
| 4 | +import { of, Observable, Observer } from 'rxjs' |
| 5 | + |
| 6 | +import { find } from './find' |
| 7 | +import { useObservable } from '../use-observable' |
| 8 | +import { tap } from 'rxjs/operators' |
| 9 | + |
| 10 | +describe('useObservable specs', () => { |
| 11 | + let timer: Sinon.SinonFakeTimers |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + timer = Sinon.useFakeTimers() |
| 15 | + }) |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + timer.restore() |
| 19 | + }) |
| 20 | + |
| 21 | + it('should get value from sync Observable', () => { |
| 22 | + const value = 100 |
| 23 | + function Fixture() { |
| 24 | + const value = useObservable(() => of(100)) |
| 25 | + return <h1>{value}</h1> |
| 26 | + } |
| 27 | + const fixtureNode = <Fixture /> |
| 28 | + |
| 29 | + const testRenderer = create(fixtureNode) |
| 30 | + expect(find(testRenderer.root, 'h1').children).toEqual([]) |
| 31 | + testRenderer.update(fixtureNode) |
| 32 | + |
| 33 | + expect(find(testRenderer.root, 'h1').children).toEqual([`${value}`]) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should render the initialValue', () => { |
| 37 | + const initialValue = 2000 |
| 38 | + const value = 100 |
| 39 | + function Fixture() { |
| 40 | + const value = useObservable(() => of(100), initialValue) |
| 41 | + return <h1>{value}</h1> |
| 42 | + } |
| 43 | + const fixtureNode = <Fixture /> |
| 44 | + |
| 45 | + const testRenderer = create(fixtureNode) |
| 46 | + expect(find(testRenderer.root, 'h1').children).toEqual([`${initialValue}`]) |
| 47 | + testRenderer.update(fixtureNode) |
| 48 | + |
| 49 | + expect(find(testRenderer.root, 'h1').children).toEqual([`${value}`]) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should call teardown logic after unmount', () => { |
| 53 | + const spy = Sinon.spy() |
| 54 | + function Fixture() { |
| 55 | + const value = useObservable( |
| 56 | + () => |
| 57 | + new Observable((observer: Observer<number>) => { |
| 58 | + const timerId = setTimeout(() => { |
| 59 | + observer.next(1) |
| 60 | + observer.complete() |
| 61 | + }, 1000) |
| 62 | + return () => { |
| 63 | + spy() |
| 64 | + clearTimeout(timerId) |
| 65 | + } |
| 66 | + }), |
| 67 | + ) |
| 68 | + return <h1>{value}</h1> |
| 69 | + } |
| 70 | + const fixtureNode = <Fixture /> |
| 71 | + |
| 72 | + const testRenderer = create(fixtureNode) |
| 73 | + expect(spy.callCount).toBe(0) |
| 74 | + testRenderer.unmount() |
| 75 | + expect(spy.callCount).toBe(1) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should emit changed props in observableFactory', () => { |
| 79 | + const spy = Sinon.spy() |
| 80 | + function Fixture(props: { foo: number; bar: string; baz: any }) { |
| 81 | + const value = useObservable((props$: Observable<[number, any]>) => props$.pipe(tap(spy)), null, [ |
| 82 | + props.foo, |
| 83 | + props.baz, |
| 84 | + ] as any) |
| 85 | + return ( |
| 86 | + <> |
| 87 | + <h1>{value && value[0]}</h1> |
| 88 | + <div>{value && value[1].foo}</div> |
| 89 | + </> |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + const props = { |
| 94 | + foo: 1, |
| 95 | + bar: 'bar', |
| 96 | + baz: { |
| 97 | + foo: 1, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + const testRenderer = create(<Fixture {...props} />) |
| 102 | + expect(spy.callCount).toBe(0) |
| 103 | + expect(find(testRenderer.root, 'h1').children).toEqual([]) |
| 104 | + expect(find(testRenderer.root, 'div').children).toEqual([]) |
| 105 | + const newProps = { ...props, bar: 'new bar' } |
| 106 | + testRenderer.update(<Fixture {...newProps} />) |
| 107 | + expect(spy.callCount).toBe(1) |
| 108 | + expect(spy.args[0]).toEqual([[newProps.foo, newProps.baz]]) |
| 109 | + expect(find(testRenderer.root, 'h1').children).toEqual([`${newProps.foo}`]) |
| 110 | + expect(find(testRenderer.root, 'div').children).toEqual([`${newProps.baz.foo}`]) |
| 111 | + |
| 112 | + const renewProps = { ...props, foo: 1000 } |
| 113 | + testRenderer.update(<Fixture {...renewProps} />) |
| 114 | + |
| 115 | + expect(spy.callCount).toBe(2) |
| 116 | + expect(spy.args[1]).toEqual([[renewProps.foo, renewProps.baz]]) |
| 117 | + expect(find(testRenderer.root, 'h1').children).toEqual([`${renewProps.foo}`]) |
| 118 | + expect(find(testRenderer.root, 'div').children).toEqual([`${renewProps.baz.foo}`]) |
| 119 | + }) |
| 120 | +}) |
0 commit comments