|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { render } from 'ripple/server'; |
| 3 | + |
| 4 | +describe('try block with catch and pending (server)', () => { |
| 5 | + it('catch block works when component throws before await with pending block', async () => { |
| 6 | + component ThrowingChild() { |
| 7 | + throw new Error('sync error'); |
| 8 | + let data = await Promise.resolve('hello'); |
| 9 | + <p>{data}</p> |
| 10 | + } |
| 11 | + |
| 12 | + component App() { |
| 13 | + try { |
| 14 | + <ThrowingChild /> |
| 15 | + } pending { |
| 16 | + <p>{'loading...'}</p> |
| 17 | + } catch (err) { |
| 18 | + <p>{'caught error'}</p> |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + const { body } = await render(App); |
| 23 | + expect(body).toContain('caught error'); |
| 24 | + expect(body).not.toContain('loading...'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('catch block works when component throws after await with pending block', async () => { |
| 28 | + component ThrowingAfterAwait() { |
| 29 | + let data = await Promise.resolve('hello'); |
| 30 | + throw new Error('error after await'); |
| 31 | + <p>{data}</p> |
| 32 | + } |
| 33 | + |
| 34 | + component App() { |
| 35 | + try { |
| 36 | + <ThrowingAfterAwait /> |
| 37 | + } pending { |
| 38 | + <p>{'loading...'}</p> |
| 39 | + } catch (err) { |
| 40 | + <p>{'caught error'}</p> |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + const { body } = await render(App); |
| 45 | + expect(body).toContain('caught error'); |
| 46 | + expect(body).not.toContain('loading...'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('catch block works with try/catch/pending when async body rejects', async () => { |
| 50 | + component App() { |
| 51 | + try { |
| 52 | + let data = await Promise.reject(new Error('rejected')); |
| 53 | + <p>{data}</p> |
| 54 | + } pending { |
| 55 | + <p>{'loading...'}</p> |
| 56 | + } catch (err) { |
| 57 | + <p>{'caught rejection'}</p> |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const { body } = await render(App); |
| 62 | + expect(body).toContain('caught rejection'); |
| 63 | + expect(body).not.toContain('loading...'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('removes pending content for nested try/pending blocks', async () => { |
| 67 | + component App() { |
| 68 | + try { |
| 69 | + try { |
| 70 | + let data = await Promise.resolve('resolved'); |
| 71 | + <p>{data}</p> |
| 72 | + } pending { |
| 73 | + <p>{'inner loading...'}</p> |
| 74 | + } |
| 75 | + } pending { |
| 76 | + <p>{'outer loading...'}</p> |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const { body } = await render(App); |
| 81 | + expect(body).toContain('resolved'); |
| 82 | + expect(body).not.toContain('outer loading...'); |
| 83 | + expect(body).not.toContain('inner loading...'); |
| 84 | + }); |
| 85 | +}); |
0 commit comments