|
1 | 1 | import { |
2 | 2 | definedTypeLinkNode, |
3 | 3 | definedTypeNode, |
| 4 | + enumEmptyVariantTypeNode, |
| 5 | + enumTupleVariantTypeNode, |
| 6 | + enumTypeNode, |
| 7 | + enumValueNode, |
4 | 8 | fixedSizeTypeNode, |
| 9 | + numberTypeNode, |
5 | 10 | programNode, |
6 | 11 | stringTypeNode, |
7 | 12 | structFieldTypeNode, |
8 | 13 | structTypeNode, |
| 14 | + tupleTypeNode, |
9 | 15 | } from '@codama/nodes'; |
10 | 16 | import { visit } from '@codama/visitors-core'; |
11 | 17 | import { test } from 'vitest'; |
@@ -91,3 +97,95 @@ test('it can override the import of a linked type', async () => { |
91 | 97 | '../../hooked': ['Symbol', 'SymbolArgs', 'getSymbolEncoder', 'getSymbolDecoder'], |
92 | 98 | }); |
93 | 99 | }); |
| 100 | + |
| 101 | +test('it knows if an enum value is a scalar enum using link nodes', async () => { |
| 102 | + // Given a program with a scalar enum linked in a default value. |
| 103 | + const node = programNode({ |
| 104 | + definedTypes: [ |
| 105 | + definedTypeNode({ |
| 106 | + name: 'person', |
| 107 | + type: structTypeNode([ |
| 108 | + structFieldTypeNode({ |
| 109 | + defaultValue: enumValueNode('direction', 'up'), |
| 110 | + name: 'movement', |
| 111 | + type: definedTypeLinkNode('direction'), |
| 112 | + }), |
| 113 | + ]), |
| 114 | + }), |
| 115 | + definedTypeNode({ |
| 116 | + name: 'direction', |
| 117 | + type: enumTypeNode([ |
| 118 | + enumEmptyVariantTypeNode('up'), |
| 119 | + enumEmptyVariantTypeNode('right'), |
| 120 | + enumEmptyVariantTypeNode('down'), |
| 121 | + enumEmptyVariantTypeNode('left'), |
| 122 | + ]), |
| 123 | + }), |
| 124 | + ], |
| 125 | + name: 'myProgram', |
| 126 | + publicKey: '1111', |
| 127 | + }); |
| 128 | + |
| 129 | + // When we render it. |
| 130 | + const renderMap = visit(node, getRenderMapVisitor()); |
| 131 | + |
| 132 | + // Then we expect the direction enum to be exported as a scalar enum. |
| 133 | + await renderMapContains(renderMap, 'types/person.ts', [ |
| 134 | + 'movement: value.movement ?? Direction.Up', |
| 135 | + 'export type Person = { movement: Direction }', |
| 136 | + 'export type PersonArgs = { movement?: DirectionArgs }', |
| 137 | + 'getDirectionEncoder()', |
| 138 | + 'getDirectionDecoder()', |
| 139 | + ]); |
| 140 | + |
| 141 | + // And we expect the following imports. |
| 142 | + await renderMapContainsImports(renderMap, 'types/person.ts', { |
| 143 | + '.': ['Direction', 'DirectionArgs', 'getDirectionEncoder', 'getDirectionDecoder'], |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +test('it knows if an enum value is a data enum using link nodes', async () => { |
| 148 | + // Given a program with a data enum linked in a default value. |
| 149 | + const node = programNode({ |
| 150 | + definedTypes: [ |
| 151 | + definedTypeNode({ |
| 152 | + name: 'person', |
| 153 | + type: structTypeNode([ |
| 154 | + structFieldTypeNode({ |
| 155 | + defaultValue: enumValueNode('action', 'stop'), |
| 156 | + name: 'nextAction', |
| 157 | + type: definedTypeLinkNode('action'), |
| 158 | + }), |
| 159 | + ]), |
| 160 | + }), |
| 161 | + definedTypeNode({ |
| 162 | + name: 'action', |
| 163 | + type: enumTypeNode([ |
| 164 | + enumEmptyVariantTypeNode('stop'), |
| 165 | + enumEmptyVariantTypeNode('turnRight'), |
| 166 | + enumEmptyVariantTypeNode('turnLeft'), |
| 167 | + enumTupleVariantTypeNode('moveForward', tupleTypeNode([numberTypeNode('u8')])), |
| 168 | + ]), |
| 169 | + }), |
| 170 | + ], |
| 171 | + name: 'myProgram', |
| 172 | + publicKey: '1111', |
| 173 | + }); |
| 174 | + |
| 175 | + // When we render it. |
| 176 | + const renderMap = visit(node, getRenderMapVisitor()); |
| 177 | + |
| 178 | + // Then we expect the action enum to be exported as a data enum. |
| 179 | + await renderMapContains(renderMap, 'types/person.ts', [ |
| 180 | + "nextAction: value.nextAction ?? action('Stop')", |
| 181 | + 'export type Person = { nextAction: Action }', |
| 182 | + 'export type PersonArgs = { nextAction?: ActionArgs }', |
| 183 | + 'getActionEncoder()', |
| 184 | + 'getActionDecoder()', |
| 185 | + ]); |
| 186 | + |
| 187 | + // And we expect the following imports. |
| 188 | + await renderMapContainsImports(renderMap, 'types/person.ts', { |
| 189 | + '.': ['Action', 'ActionArgs', 'getActionEncoder', 'getActionDecoder'], |
| 190 | + }); |
| 191 | +}); |
0 commit comments