|
| 1 | +import { parseFormLifecycleEvent } from '../Forms'; |
| 2 | + |
| 3 | +describe('parseFormLifecycleEvent', () => { |
| 4 | + let consoleErrorSpy: jest.SpyInstance; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); |
| 8 | + }); |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + consoleErrorSpy.mockRestore(); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('valid events', () => { |
| 15 | + it('parses a valid formShown event', () => { |
| 16 | + const result = parseFormLifecycleEvent({ |
| 17 | + type: 'formShown', |
| 18 | + formId: 'abc123', |
| 19 | + formName: 'Welcome Form', |
| 20 | + }); |
| 21 | + |
| 22 | + expect(result).toEqual({ |
| 23 | + type: 'formShown', |
| 24 | + formId: 'abc123', |
| 25 | + formName: 'Welcome Form', |
| 26 | + }); |
| 27 | + expect(consoleErrorSpy).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('parses a valid formDismissed event', () => { |
| 31 | + const result = parseFormLifecycleEvent({ |
| 32 | + type: 'formDismissed', |
| 33 | + formId: 'abc123', |
| 34 | + formName: 'Welcome Form', |
| 35 | + }); |
| 36 | + |
| 37 | + expect(result).toEqual({ |
| 38 | + type: 'formDismissed', |
| 39 | + formId: 'abc123', |
| 40 | + formName: 'Welcome Form', |
| 41 | + }); |
| 42 | + expect(consoleErrorSpy).not.toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('parses a valid formCtaClicked event with all fields', () => { |
| 46 | + const result = parseFormLifecycleEvent({ |
| 47 | + type: 'formCtaClicked', |
| 48 | + formId: 'abc123', |
| 49 | + formName: 'Welcome Form', |
| 50 | + buttonLabel: 'Shop Now', |
| 51 | + deepLinkUrl: 'myapp://products', |
| 52 | + }); |
| 53 | + |
| 54 | + expect(result).toEqual({ |
| 55 | + type: 'formCtaClicked', |
| 56 | + formId: 'abc123', |
| 57 | + formName: 'Welcome Form', |
| 58 | + buttonLabel: 'Shop Now', |
| 59 | + deepLinkUrl: 'myapp://products', |
| 60 | + }); |
| 61 | + expect(consoleErrorSpy).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('defaults deepLinkUrl to empty string when absent', () => { |
| 65 | + const result = parseFormLifecycleEvent({ |
| 66 | + type: 'formCtaClicked', |
| 67 | + formId: 'abc123', |
| 68 | + formName: 'Welcome Form', |
| 69 | + buttonLabel: 'Shop Now', |
| 70 | + }); |
| 71 | + |
| 72 | + expect(result).toEqual({ |
| 73 | + type: 'formCtaClicked', |
| 74 | + formId: 'abc123', |
| 75 | + formName: 'Welcome Form', |
| 76 | + buttonLabel: 'Shop Now', |
| 77 | + deepLinkUrl: '', |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('strips extra fields not part of the event type', () => { |
| 82 | + const result = parseFormLifecycleEvent({ |
| 83 | + type: 'formShown', |
| 84 | + formId: 'abc123', |
| 85 | + formName: 'Welcome Form', |
| 86 | + unexpectedField: 'should be ignored', |
| 87 | + }); |
| 88 | + |
| 89 | + expect(result).toEqual({ |
| 90 | + type: 'formShown', |
| 91 | + formId: 'abc123', |
| 92 | + formName: 'Welcome Form', |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('invalid type', () => { |
| 98 | + it('returns null for unknown type', () => { |
| 99 | + const result = parseFormLifecycleEvent({ |
| 100 | + type: 'formExploded', |
| 101 | + formId: 'abc123', |
| 102 | + formName: 'Welcome Form', |
| 103 | + }); |
| 104 | + |
| 105 | + expect(result).toBeNull(); |
| 106 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 107 | + expect.stringContaining('invalid type: "formExploded"') |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns null for missing type', () => { |
| 112 | + const result = parseFormLifecycleEvent({ |
| 113 | + formId: 'abc123', |
| 114 | + formName: 'Welcome Form', |
| 115 | + }); |
| 116 | + |
| 117 | + expect(result).toBeNull(); |
| 118 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 119 | + expect.stringContaining('invalid type') |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns null for empty string type', () => { |
| 124 | + const result = parseFormLifecycleEvent({ |
| 125 | + type: '', |
| 126 | + formId: 'abc123', |
| 127 | + formName: 'Welcome Form', |
| 128 | + }); |
| 129 | + |
| 130 | + expect(result).toBeNull(); |
| 131 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 132 | + expect.stringContaining('invalid type') |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + it('returns null for non-string type', () => { |
| 137 | + const result = parseFormLifecycleEvent({ |
| 138 | + type: 42, |
| 139 | + formId: 'abc123', |
| 140 | + formName: 'Welcome Form', |
| 141 | + }); |
| 142 | + |
| 143 | + expect(result).toBeNull(); |
| 144 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 145 | + expect.stringContaining('invalid type') |
| 146 | + ); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('missing required fields', () => { |
| 151 | + it('returns null when formId is missing', () => { |
| 152 | + const result = parseFormLifecycleEvent({ |
| 153 | + type: 'formShown', |
| 154 | + formName: 'Welcome Form', |
| 155 | + }); |
| 156 | + |
| 157 | + expect(result).toBeNull(); |
| 158 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 159 | + expect.stringContaining('missing required field(s): formId') |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
| 163 | + it('returns null when formName is missing', () => { |
| 164 | + const result = parseFormLifecycleEvent({ |
| 165 | + type: 'formShown', |
| 166 | + formId: 'abc123', |
| 167 | + }); |
| 168 | + |
| 169 | + expect(result).toBeNull(); |
| 170 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 171 | + expect.stringContaining('missing required field(s): formName') |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + it('returns null when formId is empty string', () => { |
| 176 | + const result = parseFormLifecycleEvent({ |
| 177 | + type: 'formDismissed', |
| 178 | + formId: '', |
| 179 | + formName: 'Welcome Form', |
| 180 | + }); |
| 181 | + |
| 182 | + expect(result).toBeNull(); |
| 183 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 184 | + expect.stringContaining('formId') |
| 185 | + ); |
| 186 | + }); |
| 187 | + |
| 188 | + it('returns null when formName is empty string', () => { |
| 189 | + const result = parseFormLifecycleEvent({ |
| 190 | + type: 'formDismissed', |
| 191 | + formId: 'abc123', |
| 192 | + formName: '', |
| 193 | + }); |
| 194 | + |
| 195 | + expect(result).toBeNull(); |
| 196 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 197 | + expect.stringContaining('formName') |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + it('returns null when buttonLabel is missing for formCtaClicked', () => { |
| 202 | + const result = parseFormLifecycleEvent({ |
| 203 | + type: 'formCtaClicked', |
| 204 | + formId: 'abc123', |
| 205 | + formName: 'Welcome Form', |
| 206 | + deepLinkUrl: 'myapp://products', |
| 207 | + }); |
| 208 | + |
| 209 | + expect(result).toBeNull(); |
| 210 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 211 | + expect.stringContaining('missing required field(s): buttonLabel') |
| 212 | + ); |
| 213 | + }); |
| 214 | + |
| 215 | + it('returns null when buttonLabel is empty string for formCtaClicked', () => { |
| 216 | + const result = parseFormLifecycleEvent({ |
| 217 | + type: 'formCtaClicked', |
| 218 | + formId: 'abc123', |
| 219 | + formName: 'Welcome Form', |
| 220 | + buttonLabel: '', |
| 221 | + deepLinkUrl: 'myapp://products', |
| 222 | + }); |
| 223 | + |
| 224 | + expect(result).toBeNull(); |
| 225 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 226 | + expect.stringContaining('buttonLabel') |
| 227 | + ); |
| 228 | + }); |
| 229 | + |
| 230 | + it('reports multiple missing fields together', () => { |
| 231 | + const result = parseFormLifecycleEvent({ |
| 232 | + type: 'formCtaClicked', |
| 233 | + }); |
| 234 | + |
| 235 | + expect(result).toBeNull(); |
| 236 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 237 | + expect.stringContaining('formId, formName, buttonLabel') |
| 238 | + ); |
| 239 | + }); |
| 240 | + |
| 241 | + it('does not require buttonLabel for formShown', () => { |
| 242 | + const result = parseFormLifecycleEvent({ |
| 243 | + type: 'formShown', |
| 244 | + formId: 'abc123', |
| 245 | + formName: 'Welcome Form', |
| 246 | + }); |
| 247 | + |
| 248 | + expect(result).not.toBeNull(); |
| 249 | + }); |
| 250 | + |
| 251 | + it('does not require buttonLabel for formDismissed', () => { |
| 252 | + const result = parseFormLifecycleEvent({ |
| 253 | + type: 'formDismissed', |
| 254 | + formId: 'abc123', |
| 255 | + formName: 'Welcome Form', |
| 256 | + }); |
| 257 | + |
| 258 | + expect(result).not.toBeNull(); |
| 259 | + }); |
| 260 | + }); |
| 261 | +}); |
0 commit comments