|
| 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 detail message', 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 generic object message', async () => { |
| 25 | + const result = render(StandardErrorAlert, { |
| 26 | + error: new AlertError({ foo: 'bar' }) |
| 27 | + }); |
| 28 | + expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s); |
| 29 | + expect(result.container.textContent).toContain('There has been an error'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('AlertError with generic object message', async () => { |
| 33 | + const result = render(StandardErrorAlert, { |
| 34 | + error: new AlertError({ foo: 'bar' }) |
| 35 | + }); |
| 36 | + expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s); |
| 37 | + expect(result.container.textContent).toContain('There has been an error'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('Generic error with string message', async () => { |
| 41 | + const result = render(StandardErrorAlert, { |
| 42 | + error: new Error('error message') |
| 43 | + }); |
| 44 | + expect(result.container.textContent).toContain('error message'); |
| 45 | + expect(result.container.textContent).not.toContain('There has been an error'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('Generic object message with detail', async () => { |
| 49 | + const result = render(StandardErrorAlert, { |
| 50 | + error: { detail: 'error message' } |
| 51 | + }); |
| 52 | + expect(result.container.textContent).toContain('error message'); |
| 53 | + expect(result.container.textContent).not.toContain('detail'); |
| 54 | + expect(result.container.textContent).not.toContain('There has been an error'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('Generic object message without detail', async () => { |
| 58 | + const result = render(StandardErrorAlert, { |
| 59 | + error: { foo: 'bar' } |
| 60 | + }); |
| 61 | + expect(result.container.textContent).toMatch(/{.*"foo".*:.*"bar".*}/s); |
| 62 | + expect(result.container.textContent).toContain('There has been an error'); |
| 63 | + }); |
| 64 | +}); |
0 commit comments