|
| 1 | +import {describe, it, expect} from 'vitest'; |
| 2 | +import {TextWithEmoji} from './TextWithEmoji'; |
| 3 | +import {Text, Image, Container} from '@pmndrs/uikit'; |
| 4 | + |
| 5 | +describe('TextWithEmoji Primitives', () => { |
| 6 | + it('should parse plain text and spaces correctly', () => { |
| 7 | + const parent = new Container(); |
| 8 | + const textWithEmoji = new TextWithEmoji({ |
| 9 | + text: 'Hello World', |
| 10 | + fontSize: 16, |
| 11 | + }); |
| 12 | + parent.add(textWithEmoji); |
| 13 | + |
| 14 | + // Should have: Text('Hello'), Container(space), Text('World') |
| 15 | + expect(textWithEmoji.children).toHaveLength(3); |
| 16 | + expect(textWithEmoji.children[0]).toBeInstanceOf(Text); |
| 17 | + expect(textWithEmoji.children[1]).toBeInstanceOf(Container); |
| 18 | + expect(textWithEmoji.children[2]).toBeInstanceOf(Text); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should parse and render emojis correctly', () => { |
| 22 | + const parent = new Container(); |
| 23 | + const textWithEmoji = new TextWithEmoji({ |
| 24 | + text: 'Hello 🚀 World', |
| 25 | + fontSize: 16, |
| 26 | + }); |
| 27 | + parent.add(textWithEmoji); |
| 28 | + |
| 29 | + // Should have: Text('Hello'), Container(space), Image(emoji), Container(space), Text('World') |
| 30 | + expect(textWithEmoji.children).toHaveLength(5); |
| 31 | + expect(textWithEmoji.children[0]).toBeInstanceOf(Text); |
| 32 | + expect(textWithEmoji.children[1]).toBeInstanceOf(Container); |
| 33 | + expect(textWithEmoji.children[2]).toBeInstanceOf(Image); |
| 34 | + expect(textWithEmoji.children[3]).toBeInstanceOf(Container); |
| 35 | + expect(textWithEmoji.children[4]).toBeInstanceOf(Text); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should handle single newline correctly', () => { |
| 39 | + const parent = new Container(); |
| 40 | + const textWithEmoji = new TextWithEmoji({ |
| 41 | + text: 'Hello\nWorld', |
| 42 | + fontSize: 16, |
| 43 | + }); |
| 44 | + parent.add(textWithEmoji); |
| 45 | + |
| 46 | + expect(textWithEmoji.children).toHaveLength(3); |
| 47 | + expect(textWithEmoji.children[0]).toBeInstanceOf(Text); |
| 48 | + expect(textWithEmoji.children[1]).toBeInstanceOf(Container); |
| 49 | + expect(textWithEmoji.children[2]).toBeInstanceOf(Text); |
| 50 | + |
| 51 | + const newlineContainer = textWithEmoji.children[1] as Container; |
| 52 | + expect(newlineContainer.properties.value.width).toBe('100%'); |
| 53 | + expect(newlineContainer.properties.value.height).toBe(0); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle double newlines correctly', () => { |
| 57 | + const parent = new Container(); |
| 58 | + const textWithEmoji = new TextWithEmoji({ |
| 59 | + text: 'Hello\n\nWorld', |
| 60 | + fontSize: 16, |
| 61 | + }); |
| 62 | + parent.add(textWithEmoji); |
| 63 | + |
| 64 | + expect(textWithEmoji.children).toHaveLength(4); |
| 65 | + expect(textWithEmoji.children[0]).toBeInstanceOf(Text); |
| 66 | + expect(textWithEmoji.children[1]).toBeInstanceOf(Container); |
| 67 | + expect(textWithEmoji.children[2]).toBeInstanceOf(Container); |
| 68 | + expect(textWithEmoji.children[3]).toBeInstanceOf(Text); |
| 69 | + |
| 70 | + const newline1 = textWithEmoji.children[1] as Container; |
| 71 | + const newline2 = textWithEmoji.children[2] as Container; |
| 72 | + |
| 73 | + expect(newline1.properties.value.width).toBe('100%'); |
| 74 | + expect(newline1.properties.value.height).toBe(0); |
| 75 | + |
| 76 | + expect(newline2.properties.value.width).toBe('100%'); |
| 77 | + expect(newline2.properties.value.height).toBe(16); // matches fontSize 16 |
| 78 | + }); |
| 79 | + |
| 80 | + it('should handle leading newline correctly', () => { |
| 81 | + const parent = new Container(); |
| 82 | + const textWithEmoji = new TextWithEmoji({ |
| 83 | + text: '\nHello', |
| 84 | + fontSize: 16, |
| 85 | + }); |
| 86 | + parent.add(textWithEmoji); |
| 87 | + |
| 88 | + expect(textWithEmoji.children).toHaveLength(2); |
| 89 | + expect(textWithEmoji.children[0]).toBeInstanceOf(Container); |
| 90 | + expect(textWithEmoji.children[1]).toBeInstanceOf(Text); |
| 91 | + |
| 92 | + const newline = textWithEmoji.children[0] as Container; |
| 93 | + expect(newline.properties.value.width).toBe('100%'); |
| 94 | + expect(newline.properties.value.height).toBe(16); // matches fontSize 16 |
| 95 | + }); |
| 96 | +}); |
0 commit comments