|
| 1 | +import ts from 'typescript'; |
| 2 | + |
| 3 | +import { hasStaticInitializerInClass } from '../decorators-to-static/convert-static-members'; |
| 4 | + |
| 5 | +describe('convert-static-members', () => { |
| 6 | + describe('hasStaticInitializerInClass', () => { |
| 7 | + it('returns true for a static property with an initializer', () => { |
| 8 | + const classWithStaticMembers = ts.factory.createClassDeclaration( |
| 9 | + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], |
| 10 | + ts.factory.createIdentifier('ClassWithStaticMember'), |
| 11 | + undefined, |
| 12 | + undefined, |
| 13 | + [ |
| 14 | + ts.factory.createPropertyDeclaration( |
| 15 | + [ts.factory.createToken(ts.SyntaxKind.StaticKeyword)], |
| 16 | + ts.factory.createIdentifier('propertyName'), |
| 17 | + undefined, |
| 18 | + undefined, |
| 19 | + ts.factory.createStringLiteral('initial value') |
| 20 | + ), |
| 21 | + ] |
| 22 | + ); |
| 23 | + expect(classWithStaticMembers.members.some(hasStaticInitializerInClass)).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns true for a private static property with an initializer', () => { |
| 27 | + const classWithStaticMembers = ts.factory.createClassDeclaration( |
| 28 | + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], |
| 29 | + ts.factory.createIdentifier('ClassWithStaticMember'), |
| 30 | + undefined, |
| 31 | + undefined, |
| 32 | + [ |
| 33 | + ts.factory.createPropertyDeclaration( |
| 34 | + [ts.factory.createToken(ts.SyntaxKind.PrivateKeyword), ts.factory.createToken(ts.SyntaxKind.StaticKeyword)], |
| 35 | + ts.factory.createIdentifier('propertyName'), |
| 36 | + undefined, |
| 37 | + undefined, |
| 38 | + ts.factory.createStringLiteral('initial value') |
| 39 | + ), |
| 40 | + ] |
| 41 | + ); |
| 42 | + expect(classWithStaticMembers.members.some(hasStaticInitializerInClass)).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns true for a static property with an initializer with multiple members', () => { |
| 46 | + const classWithStaticMembers = ts.factory.createClassDeclaration( |
| 47 | + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], |
| 48 | + ts.factory.createIdentifier('ClassWithStaticAndNonStaticMembers'), |
| 49 | + undefined, |
| 50 | + undefined, |
| 51 | + [ |
| 52 | + ts.factory.createPropertyDeclaration( |
| 53 | + undefined, |
| 54 | + ts.factory.createIdentifier('nonStaticProperty'), |
| 55 | + undefined, |
| 56 | + undefined, |
| 57 | + ts.factory.createStringLiteral('some value') |
| 58 | + ), |
| 59 | + ts.factory.createPropertyDeclaration( |
| 60 | + [ts.factory.createToken(ts.SyntaxKind.StaticKeyword)], |
| 61 | + ts.factory.createIdentifier('propertyName'), |
| 62 | + undefined, |
| 63 | + undefined, |
| 64 | + ts.factory.createStringLiteral('initial value') |
| 65 | + ), |
| 66 | + ] |
| 67 | + ); |
| 68 | + expect(classWithStaticMembers.members.some(hasStaticInitializerInClass)).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns false for a class without any members', () => { |
| 72 | + const classWithStaticMembers = ts.factory.createClassDeclaration( |
| 73 | + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], |
| 74 | + ts.factory.createIdentifier('ClassWithNoMembers'), |
| 75 | + undefined, |
| 76 | + undefined, |
| 77 | + [] // no members for this class |
| 78 | + ); |
| 79 | + expect(classWithStaticMembers.members.some(hasStaticInitializerInClass)).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns false for a static property without an initializer', () => { |
| 83 | + const classWithStaticMembers = ts.factory.createClassDeclaration( |
| 84 | + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], |
| 85 | + ts.factory.createIdentifier('ClassWithUninitializedStaticMember'), |
| 86 | + undefined, |
| 87 | + undefined, |
| 88 | + [ |
| 89 | + ts.factory.createPropertyDeclaration( |
| 90 | + [ts.factory.createToken(ts.SyntaxKind.StaticKeyword)], |
| 91 | + ts.factory.createIdentifier('propertyName'), |
| 92 | + undefined, |
| 93 | + undefined, |
| 94 | + undefined // the initializer is false |
| 95 | + ), |
| 96 | + ] |
| 97 | + ); |
| 98 | + expect(classWithStaticMembers.members.some(hasStaticInitializerInClass)).toBe(false); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns false for a private static property without an initializer', () => { |
| 102 | + const classWithStaticMembers = ts.factory.createClassDeclaration( |
| 103 | + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], |
| 104 | + ts.factory.createIdentifier('ClassWithUninitializedStaticMember'), |
| 105 | + undefined, |
| 106 | + undefined, |
| 107 | + [ |
| 108 | + ts.factory.createPropertyDeclaration( |
| 109 | + [ts.factory.createToken(ts.SyntaxKind.PrivateKeyword), ts.factory.createToken(ts.SyntaxKind.StaticKeyword)], |
| 110 | + ts.factory.createIdentifier('propertyName'), |
| 111 | + undefined, |
| 112 | + undefined, |
| 113 | + undefined // the initializer is false |
| 114 | + ), |
| 115 | + ] |
| 116 | + ); |
| 117 | + expect(classWithStaticMembers.members.some(hasStaticInitializerInClass)).toBe(false); |
| 118 | + }); |
| 119 | + |
| 120 | + it('returns false for a modified property with an initializer', () => { |
| 121 | + const classWithStaticMembers = ts.factory.createClassDeclaration( |
| 122 | + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], |
| 123 | + ts.factory.createIdentifier('ClassWithNonStaticMember'), |
| 124 | + undefined, |
| 125 | + undefined, |
| 126 | + [ |
| 127 | + ts.factory.createPropertyDeclaration( |
| 128 | + [ts.factory.createToken(ts.SyntaxKind.PrivateKeyword)], // the property is declared as private |
| 129 | + ts.factory.createIdentifier('propertyName'), |
| 130 | + undefined, |
| 131 | + undefined, |
| 132 | + ts.factory.createStringLiteral('initial value') |
| 133 | + ), |
| 134 | + ] |
| 135 | + ); |
| 136 | + expect(classWithStaticMembers.members.some(hasStaticInitializerInClass)).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns false for an unmodified property with an initializer', () => { |
| 140 | + const classWithStaticMembers = ts.factory.createClassDeclaration( |
| 141 | + [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)], |
| 142 | + ts.factory.createIdentifier('ClassWithUnmodifiedMembers'), |
| 143 | + undefined, |
| 144 | + undefined, |
| 145 | + [ |
| 146 | + ts.factory.createPropertyDeclaration( |
| 147 | + undefined, // the property declaration has no modifiers |
| 148 | + ts.factory.createIdentifier('propertyName'), |
| 149 | + undefined, |
| 150 | + undefined, |
| 151 | + ts.factory.createStringLiteral('initial value') |
| 152 | + ), |
| 153 | + ] |
| 154 | + ); |
| 155 | + expect(classWithStaticMembers.members.some(hasStaticInitializerInClass)).toBe(false); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments