|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { renderDocNodes } from '../../../../../server/utils/docs/render' |
| 3 | +import type { DenoDocNode } from '#shared/types/deno-doc' |
| 4 | +import type { MergedSymbol } from '../../../../../server/utils/docs/types' |
| 5 | + |
| 6 | +// ============================================================================= |
| 7 | +// Issue #1943: class getters shown as methods |
| 8 | +// https://github.com/npmx-dev/npmx.dev/issues/1943 |
| 9 | +// ============================================================================= |
| 10 | + |
| 11 | +function createClassSymbol(classDef: DenoDocNode['classDef']): MergedSymbol { |
| 12 | + const node: DenoDocNode = { |
| 13 | + name: 'TestClass', |
| 14 | + kind: 'class', |
| 15 | + classDef, |
| 16 | + } |
| 17 | + return { |
| 18 | + name: 'TestClass', |
| 19 | + kind: 'class', |
| 20 | + nodes: [node], |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +describe('issue #1943 - class getters separated from methods', () => { |
| 25 | + it('renders getters under a "Getters" heading, not "Methods"', async () => { |
| 26 | + const symbol = createClassSymbol({ |
| 27 | + methods: [ |
| 28 | + { |
| 29 | + name: 'clientId', |
| 30 | + kind: 'getter', |
| 31 | + functionDef: { |
| 32 | + returnType: { repr: 'string', kind: 'keyword', keyword: 'string' }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + ], |
| 36 | + }) |
| 37 | + |
| 38 | + const html = await renderDocNodes([symbol], new Map()) |
| 39 | + |
| 40 | + expect(html).toContain('<h4>Getters</h4>') |
| 41 | + expect(html).toContain('get clientId') |
| 42 | + expect(html).not.toContain('<h4>Methods</h4>') |
| 43 | + }) |
| 44 | + |
| 45 | + it('renders regular methods under "Methods" heading', async () => { |
| 46 | + const symbol = createClassSymbol({ |
| 47 | + methods: [ |
| 48 | + { |
| 49 | + name: 'connect', |
| 50 | + kind: 'method', |
| 51 | + functionDef: { |
| 52 | + params: [], |
| 53 | + returnType: { repr: 'void', kind: 'keyword', keyword: 'void' }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }) |
| 58 | + |
| 59 | + const html = await renderDocNodes([symbol], new Map()) |
| 60 | + |
| 61 | + expect(html).toContain('<h4>Methods</h4>') |
| 62 | + expect(html).toContain('connect(') |
| 63 | + expect(html).not.toContain('<h4>Getters</h4>') |
| 64 | + }) |
| 65 | + |
| 66 | + it('renders both getters and methods in separate sections', async () => { |
| 67 | + const symbol = createClassSymbol({ |
| 68 | + methods: [ |
| 69 | + { |
| 70 | + name: 'clientId', |
| 71 | + kind: 'getter', |
| 72 | + functionDef: { |
| 73 | + returnType: { repr: 'string', kind: 'keyword', keyword: 'string' }, |
| 74 | + }, |
| 75 | + jsDoc: { doc: 'The client ID' }, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: 'connect', |
| 79 | + kind: 'method', |
| 80 | + functionDef: { |
| 81 | + params: [ |
| 82 | + { |
| 83 | + kind: 'identifier', |
| 84 | + name: 'url', |
| 85 | + tsType: { repr: 'string', kind: 'keyword', keyword: 'string' }, |
| 86 | + }, |
| 87 | + ], |
| 88 | + returnType: { repr: 'void', kind: 'keyword', keyword: 'void' }, |
| 89 | + }, |
| 90 | + jsDoc: { doc: 'Connect to server' }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }) |
| 94 | + |
| 95 | + const html = await renderDocNodes([symbol], new Map()) |
| 96 | + |
| 97 | + // Both sections should exist |
| 98 | + expect(html).toContain('<h4>Getters</h4>') |
| 99 | + expect(html).toContain('<h4>Methods</h4>') |
| 100 | + |
| 101 | + // Getter should use "get" prefix without parentheses |
| 102 | + expect(html).toContain('get clientId') |
| 103 | + expect(html).toContain('The client ID') |
| 104 | + |
| 105 | + // Method should have parentheses |
| 106 | + expect(html).toContain('connect(') |
| 107 | + expect(html).toContain('Connect to server') |
| 108 | + |
| 109 | + // Getters section should appear before Methods section |
| 110 | + const gettersIndex = html.indexOf('<h4>Getters</h4>') |
| 111 | + const methodsIndex = html.indexOf('<h4>Methods</h4>') |
| 112 | + expect(gettersIndex).toBeLessThan(methodsIndex) |
| 113 | + }) |
| 114 | + |
| 115 | + it('renders static getter correctly', async () => { |
| 116 | + const symbol = createClassSymbol({ |
| 117 | + methods: [ |
| 118 | + { |
| 119 | + name: 'instance', |
| 120 | + kind: 'getter', |
| 121 | + isStatic: true, |
| 122 | + functionDef: { |
| 123 | + returnType: { repr: 'TestClass', kind: 'typeRef', typeRef: { typeName: 'TestClass' } }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + ], |
| 127 | + }) |
| 128 | + |
| 129 | + const html = await renderDocNodes([symbol], new Map()) |
| 130 | + |
| 131 | + expect(html).toContain('static get instance') |
| 132 | + }) |
| 133 | +}) |
0 commit comments