|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { getStaticAttrValue } = require('../../../lib/utils/static-attr-value'); |
| 4 | + |
| 5 | +describe('getStaticAttrValue', () => { |
| 6 | + it('returns empty string for null/undefined (valueless attribute)', () => { |
| 7 | + expect(getStaticAttrValue(null)).toBe(''); |
| 8 | + expect(getStaticAttrValue(undefined)).toBe(''); |
| 9 | + }); |
| 10 | + |
| 11 | + it('returns chars for GlimmerTextNode', () => { |
| 12 | + expect(getStaticAttrValue({ type: 'GlimmerTextNode', chars: 'hello' })).toBe('hello'); |
| 13 | + expect(getStaticAttrValue({ type: 'GlimmerTextNode', chars: '' })).toBe(''); |
| 14 | + }); |
| 15 | + |
| 16 | + it('unwraps GlimmerMustacheStatement with BooleanLiteral', () => { |
| 17 | + expect( |
| 18 | + getStaticAttrValue({ |
| 19 | + type: 'GlimmerMustacheStatement', |
| 20 | + path: { type: 'GlimmerBooleanLiteral', value: true }, |
| 21 | + }) |
| 22 | + ).toBe('true'); |
| 23 | + expect( |
| 24 | + getStaticAttrValue({ |
| 25 | + type: 'GlimmerMustacheStatement', |
| 26 | + path: { type: 'GlimmerBooleanLiteral', value: false }, |
| 27 | + }) |
| 28 | + ).toBe('false'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('unwraps GlimmerMustacheStatement with StringLiteral', () => { |
| 32 | + expect( |
| 33 | + getStaticAttrValue({ |
| 34 | + type: 'GlimmerMustacheStatement', |
| 35 | + path: { type: 'GlimmerStringLiteral', value: 'foo' }, |
| 36 | + }) |
| 37 | + ).toBe('foo'); |
| 38 | + expect( |
| 39 | + getStaticAttrValue({ |
| 40 | + type: 'GlimmerMustacheStatement', |
| 41 | + path: { type: 'GlimmerStringLiteral', value: '' }, |
| 42 | + }) |
| 43 | + ).toBe(''); |
| 44 | + }); |
| 45 | + |
| 46 | + it('unwraps GlimmerMustacheStatement with NumberLiteral', () => { |
| 47 | + expect( |
| 48 | + getStaticAttrValue({ |
| 49 | + type: 'GlimmerMustacheStatement', |
| 50 | + path: { type: 'GlimmerNumberLiteral', value: -1 }, |
| 51 | + }) |
| 52 | + ).toBe('-1'); |
| 53 | + expect( |
| 54 | + getStaticAttrValue({ |
| 55 | + type: 'GlimmerMustacheStatement', |
| 56 | + path: { type: 'GlimmerNumberLiteral', value: 0 }, |
| 57 | + }) |
| 58 | + ).toBe('0'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns undefined for GlimmerMustacheStatement with a dynamic PathExpression', () => { |
| 62 | + expect( |
| 63 | + getStaticAttrValue({ |
| 64 | + type: 'GlimmerMustacheStatement', |
| 65 | + path: { type: 'GlimmerPathExpression', original: 'this.foo' }, |
| 66 | + }) |
| 67 | + ).toBeUndefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('joins GlimmerConcatStatement with only static parts', () => { |
| 71 | + expect( |
| 72 | + getStaticAttrValue({ |
| 73 | + type: 'GlimmerConcatStatement', |
| 74 | + parts: [ |
| 75 | + { type: 'GlimmerTextNode', chars: 'prefix-' }, |
| 76 | + { |
| 77 | + type: 'GlimmerMustacheStatement', |
| 78 | + path: { type: 'GlimmerStringLiteral', value: 'mid' }, |
| 79 | + }, |
| 80 | + { type: 'GlimmerTextNode', chars: '-suffix' }, |
| 81 | + ], |
| 82 | + }) |
| 83 | + ).toBe('prefix-mid-suffix'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('joins concat with boolean and number literal parts', () => { |
| 87 | + expect( |
| 88 | + getStaticAttrValue({ |
| 89 | + type: 'GlimmerConcatStatement', |
| 90 | + parts: [ |
| 91 | + { |
| 92 | + type: 'GlimmerMustacheStatement', |
| 93 | + path: { type: 'GlimmerBooleanLiteral', value: true }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + }) |
| 97 | + ).toBe('true'); |
| 98 | + expect( |
| 99 | + getStaticAttrValue({ |
| 100 | + type: 'GlimmerConcatStatement', |
| 101 | + parts: [ |
| 102 | + { |
| 103 | + type: 'GlimmerMustacheStatement', |
| 104 | + path: { type: 'GlimmerNumberLiteral', value: -1 }, |
| 105 | + }, |
| 106 | + ], |
| 107 | + }) |
| 108 | + ).toBe('-1'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns undefined for GlimmerConcatStatement with a dynamic part', () => { |
| 112 | + expect( |
| 113 | + getStaticAttrValue({ |
| 114 | + type: 'GlimmerConcatStatement', |
| 115 | + parts: [ |
| 116 | + { type: 'GlimmerTextNode', chars: 'x-' }, |
| 117 | + { |
| 118 | + type: 'GlimmerMustacheStatement', |
| 119 | + path: { type: 'GlimmerPathExpression', original: 'this.foo' }, |
| 120 | + }, |
| 121 | + ], |
| 122 | + }) |
| 123 | + ).toBeUndefined(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('returns undefined for an unknown node type', () => { |
| 127 | + expect(getStaticAttrValue({ type: 'GlimmerSubExpression' })).toBeUndefined(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments