|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { render } from '@testing-library/svelte'; |
| 3 | + |
| 4 | +import StandardErrorAlert from '../src/lib/components/common/StandardErrorAlert.svelte'; |
| 5 | +import { AlertError } from '../src/lib/common/errors'; |
| 6 | + |
| 7 | +describe('StandardErrorAlert', () => { |
| 8 | + it('AlertError with string message', async () => { |
| 9 | + const result = render(StandardErrorAlert, { |
| 10 | + error: new AlertError('error message') |
| 11 | + }); |
| 12 | + expect(result.container.textContent).toContain('error message'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('AlertError with string detail', async () => { |
| 16 | + const result = render(StandardErrorAlert, { |
| 17 | + error: new AlertError({ detail: 'error message' }) |
| 18 | + }); |
| 19 | + expect(result.container.textContent).toContain('error message'); |
| 20 | + expect(result.container.textContent).not.toContain('detail'); |
| 21 | + expect(result.container.textContent).not.toContain('There has been an error'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('AlertError with object detail and __root__ loc', async () => { |
| 25 | + const result = render(StandardErrorAlert, { |
| 26 | + error: new AlertError( |
| 27 | + { |
| 28 | + detail: [ |
| 29 | + { |
| 30 | + loc: ['body', '__root__'], |
| 31 | + msg: 'error message', |
| 32 | + type: 'value_error' |
| 33 | + } |
| 34 | + ] |
| 35 | + }, |
| 36 | + 422 |
| 37 | + ) |
| 38 | + }); |
| 39 | + expect(result.container.textContent).toContain('error message'); |
| 40 | + expect(result.container.textContent).not.toContain('detail'); |
| 41 | + expect(result.container.textContent).not.toContain('There has been an error'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('AlertError with generic object message', async () => { |
| 45 | + const result = render(StandardErrorAlert, { |
| 46 | + error: new AlertError({ foo: 'bar' }) |
| 47 | + }); |
| 48 | + expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s); |
| 49 | + expect(result.container.textContent).toContain('There has been an error'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('AlertError with generic object message', async () => { |
| 53 | + const result = render(StandardErrorAlert, { |
| 54 | + error: new AlertError({ foo: 'bar' }) |
| 55 | + }); |
| 56 | + expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s); |
| 57 | + expect(result.container.textContent).toContain('There has been an error'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('Generic error with string message', async () => { |
| 61 | + const result = render(StandardErrorAlert, { |
| 62 | + error: new Error('error message') |
| 63 | + }); |
| 64 | + expect(result.container.textContent).toContain('error message'); |
| 65 | + expect(result.container.textContent).not.toContain('There has been an error'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('Generic object message with detail', async () => { |
| 69 | + const result = render(StandardErrorAlert, { |
| 70 | + error: { detail: 'error message' } |
| 71 | + }); |
| 72 | + expect(result.container.textContent).toContain('error message'); |
| 73 | + expect(result.container.textContent).not.toContain('detail'); |
| 74 | + expect(result.container.textContent).not.toContain('There has been an error'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('Generic object message without detail', async () => { |
| 78 | + const result = render(StandardErrorAlert, { |
| 79 | + error: { foo: 'bar' } |
| 80 | + }); |
| 81 | + expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s); |
| 82 | + expect(result.container.textContent).toContain('There has been an error'); |
| 83 | + }); |
| 84 | +}); |
0 commit comments