|
1 | 1 | import { useObservable } from './use-observable'; |
2 | | -import { renderHook, cleanup, act } from 'react-hooks-testing-library'; |
| 2 | +import { renderHook, act } from '@testing-library/react-hooks'; |
3 | 3 | import { of, Subject, Observable, observable } from 'rxjs'; |
4 | 4 | import { delay } from 'rxjs/operators'; |
5 | | -import { |
6 | | - render, |
7 | | - waitForElement, |
8 | | - getByTestId, |
9 | | - queryByTestId |
10 | | -} from 'react-testing-library'; |
| 5 | +import { render, waitForElement, cleanup } from '@testing-library/react'; |
11 | 6 | import { ReactFireOptions } from '..'; |
12 | 7 | import * as React from 'react'; |
13 | 8 | import 'jest-dom/extend-expect'; |
@@ -86,40 +81,20 @@ it('works with Suspense', async () => { |
86 | 81 | expect(queryByTestId(fallbackComponentId)).toBeNull(); |
87 | 82 | }); |
88 | 83 |
|
89 | | -it('updates the component as the observable emits new values', async () => { |
90 | | - const observableFirstValue = 'a'; |
| 84 | +it('emits new values as the observable changes', async () => { |
| 85 | + const startVal = 'start'; |
| 86 | + const values = ['a', 'b', 'c']; |
91 | 87 | const observableSecondValue = 'b'; |
92 | 88 | const observable$ = new Subject(); |
93 | | - const actualComponentId = 'actual-component'; |
94 | | - const fallbackComponentId = 'fallback-component'; |
95 | | - |
96 | | - const FallbackComponent = () => ( |
97 | | - <h1 data-testid={fallbackComponentId}>Fallback</h1> |
98 | | - ); |
99 | | - |
100 | | - const Component = () => { |
101 | | - const val = useObservable(observable$, 'test-suspense'); |
102 | | - return <h1 data-testid={actualComponentId}>{val}}</h1>; |
103 | | - }; |
104 | 89 |
|
105 | | - const { queryByTestId, getByTestId } = render( |
106 | | - <React.Suspense fallback={<FallbackComponent />}> |
107 | | - <Component /> |
108 | | - </React.Suspense> |
109 | | - ); |
110 | | - |
111 | | - act(() => observable$.next(observableFirstValue)); |
112 | | - await waitForElement(() => getByTestId(actualComponentId)); |
113 | | - |
114 | | - // check that the value matches |
115 | | - expect(getByTestId(actualComponentId)).toHaveTextContent( |
116 | | - observableFirstValue |
| 90 | + const { result } = renderHook(() => |
| 91 | + useObservable(observable$, 'test', startVal) |
117 | 92 | ); |
118 | 93 |
|
119 | | - act(() => observable$.next(observableSecondValue)); |
| 94 | + expect(result.current).toEqual(startVal); |
120 | 95 |
|
121 | | - // check that the component was updated with the new value |
122 | | - expect(getByTestId(actualComponentId)).toHaveTextContent( |
123 | | - observableSecondValue |
124 | | - ); |
| 96 | + values.forEach(value => { |
| 97 | + act(() => observable$.next(value)); |
| 98 | + expect(result.current).toEqual(value); |
| 99 | + }); |
125 | 100 | }); |
0 commit comments