|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +describe('Request Content Type Logic', () => { |
| 4 | + // Helper function to extract the logic from RestCodeSamples |
| 5 | + function shouldShowRequestContentType(codeExamples) { |
| 6 | + const requestContentTypesDiffer = |
| 7 | + codeExamples.length > 1 && |
| 8 | + !codeExamples.every( |
| 9 | + (example) => example.request?.contentType === codeExamples[0].request?.contentType, |
| 10 | + ) |
| 11 | + return requestContentTypesDiffer |
| 12 | + } |
| 13 | + |
| 14 | + function shouldShowResponseContentType(codeExamples) { |
| 15 | + const responseContentTypesDiffer = |
| 16 | + codeExamples.length > 1 && |
| 17 | + !codeExamples.every( |
| 18 | + (example) => example.response?.contentType === codeExamples[0].response?.contentType, |
| 19 | + ) |
| 20 | + return responseContentTypesDiffer |
| 21 | + } |
| 22 | + |
| 23 | + function generateExampleOptions(codeExamples) { |
| 24 | + const requestContentTypesDiffer = shouldShowRequestContentType(codeExamples) |
| 25 | + const responseContentTypesDiffer = shouldShowResponseContentType(codeExamples) |
| 26 | + const showExampleOptionMediaType = responseContentTypesDiffer || requestContentTypesDiffer |
| 27 | + |
| 28 | + return codeExamples.map((example, index) => { |
| 29 | + const requestContentType = example.request?.contentType |
| 30 | + const responseContentType = example.response?.contentType |
| 31 | + |
| 32 | + let text = example.request?.description || `Example ${index + 1}` |
| 33 | + |
| 34 | + if (showExampleOptionMediaType) { |
| 35 | + if (requestContentTypesDiffer && responseContentTypesDiffer) { |
| 36 | + // Show both request and response content types |
| 37 | + text = `${text} (${requestContentType} → ${responseContentType})` |
| 38 | + } else if (requestContentTypesDiffer) { |
| 39 | + // Show only request content type |
| 40 | + text = `${text} (${requestContentType})` |
| 41 | + } else if (responseContentTypesDiffer) { |
| 42 | + // Show only response content type |
| 43 | + text = `${text} (${responseContentType})` |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return text |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + test('detects request content types differ correctly', () => { |
| 52 | + const codeExamples = [ |
| 53 | + { |
| 54 | + request: { contentType: 'text/plain', description: 'Example' }, |
| 55 | + response: { contentType: 'text/html' }, |
| 56 | + }, |
| 57 | + { |
| 58 | + request: { contentType: 'text/x-markdown', description: 'Rendering markdown' }, |
| 59 | + response: { contentType: 'text/html' }, |
| 60 | + }, |
| 61 | + ] |
| 62 | + |
| 63 | + expect(shouldShowRequestContentType(codeExamples)).toBe(true) |
| 64 | + expect(shouldShowResponseContentType(codeExamples)).toBe(false) |
| 65 | + }) |
| 66 | + |
| 67 | + test('detects response content types differ correctly', () => { |
| 68 | + const codeExamples = [ |
| 69 | + { |
| 70 | + request: { contentType: 'application/json', description: 'JSON example' }, |
| 71 | + response: { contentType: 'application/json' }, |
| 72 | + }, |
| 73 | + { |
| 74 | + request: { contentType: 'application/json', description: 'Another JSON example' }, |
| 75 | + response: { contentType: 'text/html' }, |
| 76 | + }, |
| 77 | + ] |
| 78 | + |
| 79 | + expect(shouldShowRequestContentType(codeExamples)).toBe(false) |
| 80 | + expect(shouldShowResponseContentType(codeExamples)).toBe(true) |
| 81 | + }) |
| 82 | + |
| 83 | + test('generates correct options for markdown/raw scenario', () => { |
| 84 | + const markdownRawExamples = [ |
| 85 | + { |
| 86 | + request: { |
| 87 | + contentType: 'text/plain', |
| 88 | + description: 'Example', |
| 89 | + }, |
| 90 | + response: { |
| 91 | + contentType: 'text/html', |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + request: { |
| 96 | + contentType: 'text/x-markdown', |
| 97 | + description: 'Rendering markdown', |
| 98 | + }, |
| 99 | + response: { |
| 100 | + contentType: 'text/html', |
| 101 | + }, |
| 102 | + }, |
| 103 | + ] |
| 104 | + |
| 105 | + const options = generateExampleOptions(markdownRawExamples) |
| 106 | + |
| 107 | + expect(options).toEqual(['Example (text/plain)', 'Rendering markdown (text/x-markdown)']) |
| 108 | + }) |
| 109 | + |
| 110 | + test('generates correct options when both request and response differ', () => { |
| 111 | + const mixedExamples = [ |
| 112 | + { |
| 113 | + request: { |
| 114 | + contentType: 'application/json', |
| 115 | + description: 'JSON request', |
| 116 | + }, |
| 117 | + response: { |
| 118 | + contentType: 'application/json', |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + request: { |
| 123 | + contentType: 'text/plain', |
| 124 | + description: 'Plain text request', |
| 125 | + }, |
| 126 | + response: { |
| 127 | + contentType: 'text/html', |
| 128 | + }, |
| 129 | + }, |
| 130 | + ] |
| 131 | + |
| 132 | + const options = generateExampleOptions(mixedExamples) |
| 133 | + |
| 134 | + expect(options).toEqual([ |
| 135 | + 'JSON request (application/json → application/json)', |
| 136 | + 'Plain text request (text/plain → text/html)', |
| 137 | + ]) |
| 138 | + }) |
| 139 | + |
| 140 | + test('does not show content types when they are all the same', () => { |
| 141 | + const sameContentTypeExamples = [ |
| 142 | + { |
| 143 | + request: { |
| 144 | + contentType: 'application/json', |
| 145 | + description: 'First example', |
| 146 | + }, |
| 147 | + response: { |
| 148 | + contentType: 'application/json', |
| 149 | + }, |
| 150 | + }, |
| 151 | + { |
| 152 | + request: { |
| 153 | + contentType: 'application/json', |
| 154 | + description: 'Second example', |
| 155 | + }, |
| 156 | + response: { |
| 157 | + contentType: 'application/json', |
| 158 | + }, |
| 159 | + }, |
| 160 | + ] |
| 161 | + |
| 162 | + const options = generateExampleOptions(sameContentTypeExamples) |
| 163 | + |
| 164 | + expect(options).toEqual(['First example', 'Second example']) |
| 165 | + }) |
| 166 | + |
| 167 | + test('handles single example correctly', () => { |
| 168 | + const singleExample = [ |
| 169 | + { |
| 170 | + request: { |
| 171 | + contentType: 'application/json', |
| 172 | + description: 'Only example', |
| 173 | + }, |
| 174 | + response: { |
| 175 | + contentType: 'application/json', |
| 176 | + }, |
| 177 | + }, |
| 178 | + ] |
| 179 | + |
| 180 | + expect(shouldShowRequestContentType(singleExample)).toBe(false) |
| 181 | + expect(shouldShowResponseContentType(singleExample)).toBe(false) |
| 182 | + |
| 183 | + const options = generateExampleOptions(singleExample) |
| 184 | + expect(options).toEqual(['Only example']) |
| 185 | + }) |
| 186 | +}) |
0 commit comments