|
| 1 | +import { html, fixture, expect } from '@open-wc/testing'; |
| 2 | +import { FormField } from '../src/formfield/FormField'; |
| 3 | +import { assertScreenshot, getClip } from './utils.test'; |
| 4 | + |
| 5 | +describe('temba-field', () => { |
| 6 | + it('renders field with plain text errors', async () => { |
| 7 | + const formField: FormField = await fixture(html` |
| 8 | + <temba-field |
| 9 | + label="Test Field" |
| 10 | + name="test" |
| 11 | + .errors=${['This is a plain text error', 'Another error message']} |
| 12 | + > |
| 13 | + <input type="text" /> |
| 14 | + </temba-field> |
| 15 | + `); |
| 16 | + |
| 17 | + await formField.updateComplete; |
| 18 | + |
| 19 | + // Check that errors are rendered |
| 20 | + const errorElements = formField.shadowRoot.querySelectorAll('.alert-error'); |
| 21 | + expect(errorElements.length).to.equal(2); |
| 22 | + expect(errorElements[0].textContent.trim()).to.equal( |
| 23 | + 'This is a plain text error' |
| 24 | + ); |
| 25 | + expect(errorElements[1].textContent.trim()).to.equal( |
| 26 | + 'Another error message' |
| 27 | + ); |
| 28 | + |
| 29 | + await assertScreenshot('formfield/plain-text-errors', getClip(formField)); |
| 30 | + }); |
| 31 | + |
| 32 | + it('renders field with markdown errors', async () => { |
| 33 | + const formField: FormField = await fixture(html` |
| 34 | + <temba-field |
| 35 | + label="Test Field" |
| 36 | + name="test" |
| 37 | + .errors=${[ |
| 38 | + 'This is **bold** text', |
| 39 | + 'This has a [link](https://example.com)', |
| 40 | + 'This is *italic* and **bold** with a [link](https://example.com)' |
| 41 | + ]} |
| 42 | + > |
| 43 | + <input type="text" /> |
| 44 | + </temba-field> |
| 45 | + `); |
| 46 | + |
| 47 | + await formField.updateComplete; |
| 48 | + |
| 49 | + // Check that errors are rendered |
| 50 | + const errorElements = formField.shadowRoot.querySelectorAll('.alert-error'); |
| 51 | + expect(errorElements.length).to.equal(3); |
| 52 | + |
| 53 | + // First error should have bold text |
| 54 | + const firstError = errorElements[0]; |
| 55 | + const boldElement = firstError.querySelector('strong'); |
| 56 | + expect(boldElement).to.not.be.null; |
| 57 | + expect(boldElement.textContent).to.equal('bold'); |
| 58 | + |
| 59 | + // Second error should have a link |
| 60 | + const secondError = errorElements[1]; |
| 61 | + const linkElement = secondError.querySelector('a'); |
| 62 | + expect(linkElement).to.not.be.null; |
| 63 | + expect(linkElement.getAttribute('href')).to.equal('https://example.com'); |
| 64 | + expect(linkElement.textContent).to.equal('link'); |
| 65 | + |
| 66 | + // Third error should have both bold, italic, and link |
| 67 | + const thirdError = errorElements[2]; |
| 68 | + const thirdBoldElement = thirdError.querySelector('strong'); |
| 69 | + const thirdItalicElement = thirdError.querySelector('em'); |
| 70 | + const thirdLinkElement = thirdError.querySelector('a'); |
| 71 | + expect(thirdBoldElement).to.not.be.null; |
| 72 | + expect(thirdItalicElement).to.not.be.null; |
| 73 | + expect(thirdLinkElement).to.not.be.null; |
| 74 | + |
| 75 | + await assertScreenshot('formfield/markdown-errors', getClip(formField)); |
| 76 | + }); |
| 77 | + |
| 78 | + it('renders field without errors', async () => { |
| 79 | + const formField: FormField = await fixture(html` |
| 80 | + <temba-field label="Test Field" name="test"> |
| 81 | + <input type="text" /> |
| 82 | + </temba-field> |
| 83 | + `); |
| 84 | + |
| 85 | + await formField.updateComplete; |
| 86 | + |
| 87 | + // Check that no errors are rendered |
| 88 | + const errorElements = formField.shadowRoot.querySelectorAll('.alert-error'); |
| 89 | + expect(errorElements.length).to.equal(0); |
| 90 | + |
| 91 | + await assertScreenshot('formfield/no-errors', getClip(formField)); |
| 92 | + }); |
| 93 | + |
| 94 | + it('renders in widget-only mode with errors', async () => { |
| 95 | + const formField: FormField = await fixture(html` |
| 96 | + <temba-field |
| 97 | + widget_only |
| 98 | + .errors=${['Widget only **error** with [link](https://example.com)']} |
| 99 | + > |
| 100 | + <input type="text" /> |
| 101 | + </temba-field> |
| 102 | + `); |
| 103 | + |
| 104 | + await formField.updateComplete; |
| 105 | + |
| 106 | + // Check that error is rendered in widget-only mode |
| 107 | + const errorElements = formField.shadowRoot.querySelectorAll('.alert-error'); |
| 108 | + expect(errorElements.length).to.equal(1); |
| 109 | + |
| 110 | + const errorElement = errorElements[0]; |
| 111 | + const boldElement = errorElement.querySelector('strong'); |
| 112 | + const linkElement = errorElement.querySelector('a'); |
| 113 | + expect(boldElement).to.not.be.null; |
| 114 | + expect(linkElement).to.not.be.null; |
| 115 | + |
| 116 | + await assertScreenshot( |
| 117 | + 'formfield/widget-only-markdown-errors', |
| 118 | + getClip(formField) |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
0 commit comments