|
1 | 1 | import { of, Subject, Observable, observable } from 'rxjs'; |
2 | | -import { render, waitForElement, cleanup } from '@testing-library/react'; |
| 2 | +import { render, waitForElement, cleanup, act } from '@testing-library/react'; |
3 | 3 | import * as React from 'react'; |
4 | 4 | import 'jest-dom/extend-expect'; |
| 5 | +import { SuspenseWithPerf, AuthCheck } from './components'; |
| 6 | +import { FirebaseAppProvider } from './firebaseContext'; |
| 7 | +import { auth, performance, User } from 'firebase/app'; |
5 | 8 |
|
6 | | -const mockTrace = { |
7 | | - start: jest.fn(), |
8 | | - stop: jest.fn() |
9 | | -}; |
10 | | - |
11 | | -const mockPerf = { |
12 | | - trace: jest.fn(() => mockTrace) |
13 | | -}; |
14 | | - |
15 | | -const mockFirebase = { |
16 | | - performance: jest.fn(() => mockPerf) |
17 | | -}; |
18 | | - |
19 | | -describe('SuspenseWithPerf', () => { |
20 | | - afterEach(cleanup); |
| 9 | +const traceStart = jest.fn(); |
| 10 | +const traceEnd = jest.fn(); |
21 | 11 |
|
22 | | - test.todo( |
23 | | - 'renders the same as Suspense (fallback until children stop throwing a promise)' |
24 | | - ); |
| 12 | +const createTrace = jest.fn(() => ({ |
| 13 | + start: traceStart, |
| 14 | + stop: traceEnd |
| 15 | +})); |
25 | 16 |
|
26 | | - test.todo('creates a trace with the correct name'); |
| 17 | +const mockPerf = jest.fn(() => { |
| 18 | + return { trace: createTrace }; |
| 19 | +}); |
27 | 20 |
|
28 | | - test.todo('starts a trace when it first renders'); |
| 21 | +const mockAuth = jest.fn(() => { |
| 22 | + return { |
| 23 | + onIdTokenChanged: jest.fn() |
| 24 | + }; |
| 25 | +}); |
29 | 26 |
|
30 | | - test.todo('stops the trace when it renders children'); |
| 27 | +const mockFirebase = { |
| 28 | + performance: mockPerf, |
| 29 | + auth: mockAuth |
| 30 | +}; |
31 | 31 |
|
32 | | - test.todo('can find fireperf from Context'); |
| 32 | +const Provider = ({ children }) => ( |
| 33 | + <FirebaseAppProvider firebaseApp={mockFirebase}> |
| 34 | + {children} |
| 35 | + </FirebaseAppProvider> |
| 36 | +); |
33 | 37 |
|
34 | | - test.todo('can use firePerf from props'); |
| 38 | +describe('SuspenseWithPerf', () => { |
| 39 | + afterEach(() => { |
| 40 | + cleanup(); |
| 41 | + jest.clearAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('behaves the same as Suspense (render fallback until thrown promise resolves)', async () => { |
| 45 | + const o$ = new Subject(); |
| 46 | + let shouldThrow = true; |
| 47 | + |
| 48 | + const promise = new Promise((resolve, reject) => { |
| 49 | + o$.subscribe(() => { |
| 50 | + shouldThrow = false; |
| 51 | + resolve(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + const Fallback = () => <h1 data-testid="fallback">Fallback</h1>; |
| 56 | + |
| 57 | + const Comp = () => { |
| 58 | + if (shouldThrow) { |
| 59 | + throw promise; |
| 60 | + } |
| 61 | + |
| 62 | + return <h1 data-testid="child">Actual</h1>; |
| 63 | + }; |
| 64 | + |
| 65 | + const SuspenseComp = () => { |
| 66 | + return ( |
| 67 | + <React.Suspense fallback={<Fallback />}> |
| 68 | + <Comp /> |
| 69 | + </React.Suspense> |
| 70 | + ); |
| 71 | + }; |
| 72 | + |
| 73 | + const SuspenseWithPerfComp = () => { |
| 74 | + return ( |
| 75 | + <Provider> |
| 76 | + <SuspenseWithPerf fallback={<Fallback />} traceId="test"> |
| 77 | + <Comp /> |
| 78 | + </SuspenseWithPerf> |
| 79 | + </Provider> |
| 80 | + ); |
| 81 | + }; |
| 82 | + |
| 83 | + const { queryAllByTestId, getAllByTestId } = render( |
| 84 | + <> |
| 85 | + <SuspenseComp /> |
| 86 | + <SuspenseWithPerfComp /> |
| 87 | + </> |
| 88 | + ); |
| 89 | + |
| 90 | + expect(queryAllByTestId('fallback').length).toEqual(2); |
| 91 | + expect(queryAllByTestId('child').length).toEqual(0); |
| 92 | + |
| 93 | + act(() => o$.next('a')); |
| 94 | + await waitForElement(() => getAllByTestId('child')); |
| 95 | + |
| 96 | + expect(queryAllByTestId('fallback').length).toEqual(0); |
| 97 | + expect(queryAllByTestId('child').length).toEqual(2); |
| 98 | + |
| 99 | + // expect(suspense.innerHTML).toEqual('Fallback'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('creates a trace with the correct name', () => { |
| 103 | + const traceName = 'trace'; |
| 104 | + render( |
| 105 | + <Provider> |
| 106 | + <SuspenseWithPerf traceId={traceName} fallback={'loading'}> |
| 107 | + children |
| 108 | + </SuspenseWithPerf> |
| 109 | + </Provider> |
| 110 | + ); |
| 111 | + |
| 112 | + expect(createTrace).toHaveBeenCalledWith(traceName); |
| 113 | + }); |
| 114 | + |
| 115 | + it('starts a trace when a promise is thrown and stops when it resolves', async () => { |
| 116 | + const o$ = new Subject(); |
| 117 | + let shouldThrow = true; |
| 118 | + |
| 119 | + const promise = new Promise((resolve, reject) => { |
| 120 | + o$.subscribe(() => { |
| 121 | + shouldThrow = false; |
| 122 | + resolve(); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + const Fallback = () => <h1 data-testid="fallback">Fallback</h1>; |
| 127 | + |
| 128 | + const Comp = () => { |
| 129 | + if (shouldThrow) { |
| 130 | + throw promise; |
| 131 | + } |
| 132 | + |
| 133 | + return <h1 data-testid="child">Actual</h1>; |
| 134 | + }; |
| 135 | + |
| 136 | + const { getByTestId } = render( |
| 137 | + <Provider> |
| 138 | + <SuspenseWithPerf fallback={<Fallback />} traceId="test lifecycle"> |
| 139 | + <Comp /> |
| 140 | + </SuspenseWithPerf> |
| 141 | + </Provider> |
| 142 | + ); |
| 143 | + |
| 144 | + expect(getByTestId('fallback')).toBeInTheDocument(); |
| 145 | + |
| 146 | + expect(traceStart).toHaveBeenCalledTimes(1); |
| 147 | + expect(traceEnd).toHaveBeenCalledTimes(0); |
| 148 | + |
| 149 | + act(() => o$.next('a')); |
| 150 | + await waitForElement(() => getByTestId('child')); |
| 151 | + expect(getByTestId('child')).toBeInTheDocument(); |
| 152 | + |
| 153 | + expect(traceStart).toHaveBeenCalledTimes(1); |
| 154 | + expect(traceEnd).toHaveBeenCalledTimes(1); |
| 155 | + }); |
| 156 | + |
| 157 | + it('can find fireperf from Context', () => { |
| 158 | + render( |
| 159 | + <Provider> |
| 160 | + <SuspenseWithPerf traceId={'hello'} fallback={'loading'}> |
| 161 | + {'children'} |
| 162 | + </SuspenseWithPerf> |
| 163 | + </Provider> |
| 164 | + ); |
| 165 | + |
| 166 | + expect(mockPerf).toHaveBeenCalled(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('can use firePerf from props', () => { |
| 170 | + render( |
| 171 | + <SuspenseWithPerf |
| 172 | + traceId={'hello'} |
| 173 | + fallback={'loading'} |
| 174 | + firePerf={mockPerf()} |
| 175 | + > |
| 176 | + {'children'} |
| 177 | + </SuspenseWithPerf> |
| 178 | + ); |
| 179 | + |
| 180 | + expect(createTrace).toHaveBeenCalled(); |
| 181 | + }); |
35 | 182 | }); |
36 | 183 |
|
37 | 184 | describe('AuthCheck', () => { |
38 | | - afterEach(cleanup); |
39 | | - |
40 | | - test.todo('can find firebase Auth from Context'); |
41 | | - |
42 | | - test.todo('can use firebase Auth from props'); |
| 185 | + afterEach(() => { |
| 186 | + cleanup(); |
| 187 | + jest.clearAllMocks(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('can find firebase Auth from Context', () => { |
| 191 | + expect(() => |
| 192 | + render( |
| 193 | + <Provider> |
| 194 | + <React.Suspense fallback={'loading'}> |
| 195 | + <AuthCheck fallback={'loading'}>{'children'}</AuthCheck> |
| 196 | + </React.Suspense> |
| 197 | + </Provider> |
| 198 | + ) |
| 199 | + ).not.toThrow(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('can use firebase Auth from props', () => { |
| 203 | + expect(() => |
| 204 | + render( |
| 205 | + <React.Suspense fallback={'loading'}> |
| 206 | + <AuthCheck |
| 207 | + fallback={'not signed in'} |
| 208 | + auth={(mockFirebase.auth() as unknown) as auth.Auth} |
| 209 | + > |
| 210 | + {'signed in'} |
| 211 | + </AuthCheck> |
| 212 | + </React.Suspense> |
| 213 | + ) |
| 214 | + ).not.toThrow(); |
| 215 | + |
| 216 | + // if this doesn't throw, it's good |
| 217 | + }); |
43 | 218 |
|
44 | 219 | test.todo('renders the fallback if a user is not signed in'); |
45 | 220 |
|
|
0 commit comments