|
| 1 | +/* eslint-disable no-shadow */ |
| 2 | +/* eslint-disable prefer-destructuring */ |
| 3 | +import { fixture, assert, html, nextFrame, aTimeout } from '@open-wc/testing'; |
| 4 | +import { AmfLoader } from './amf-loader.js'; |
| 5 | +import '../api-console-app.js'; |
| 6 | + |
| 7 | +/** @typedef {import('../src/ApiConsoleApp.js').ApiConsoleApp} ApiConsoleApp */ |
| 8 | + |
| 9 | +describe('ApiConsoleApp', () => { |
| 10 | + /** |
| 11 | + * @returns {Promise<ApiConsoleApp>} |
| 12 | + */ |
| 13 | + async function appFixture(amf) { |
| 14 | + return fixture(html`<api-console-app .amf="${amf}"></api-console-app>`); |
| 15 | + } |
| 16 | + |
| 17 | + describe('gRPC API support', () => { |
| 18 | + let grpcAmf; |
| 19 | + let regularAmf; |
| 20 | + |
| 21 | + before(async () => { |
| 22 | + grpcAmf = await AmfLoader.load({ fileName: 'grpc-test', compact: false }); |
| 23 | + regularAmf = await AmfLoader.load({ fileName: 'demo-api', compact: false }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('_computeRenderInlineTryIt()', () => { |
| 27 | + it('returns false for gRPC API even with wideLayout and method', async () => { |
| 28 | + const element = await appFixture(grpcAmf); |
| 29 | + await nextFrame(); |
| 30 | + |
| 31 | + // Simulate wide layout and method selection |
| 32 | + element.wideLayout = true; |
| 33 | + element.selectedShapeType = 'method'; |
| 34 | + await nextFrame(); |
| 35 | + |
| 36 | + const result = element._computeRenderInlineTryIt(true, true, false); |
| 37 | + assert.isFalse(result, 'Should return false for gRPC API'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns true for regular WebAPI with wideLayout and method', async () => { |
| 41 | + const element = await appFixture(regularAmf); |
| 42 | + await nextFrame(); |
| 43 | + |
| 44 | + element.wideLayout = true; |
| 45 | + element.selectedShapeType = 'method'; |
| 46 | + await nextFrame(); |
| 47 | + |
| 48 | + const result = element._computeRenderInlineTryIt(true, true, false); |
| 49 | + assert.isTrue(result, 'Should return true for regular WebAPI'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns false when not wideLayout', async () => { |
| 53 | + const element = await appFixture(grpcAmf); |
| 54 | + await nextFrame(); |
| 55 | + |
| 56 | + const result = element._computeRenderInlineTryIt(false, true, false); |
| 57 | + assert.isFalse(result, 'Should return false when not wideLayout'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns false when not a method', async () => { |
| 61 | + const element = await appFixture(grpcAmf); |
| 62 | + await nextFrame(); |
| 63 | + |
| 64 | + const result = element._computeRenderInlineTryIt(true, false, false); |
| 65 | + assert.isFalse(result, 'Should return false when not a method'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns false when inlineMethods is true', async () => { |
| 69 | + const element = await appFixture(regularAmf); |
| 70 | + await nextFrame(); |
| 71 | + |
| 72 | + const result = element._computeRenderInlineTryIt(true, true, true); |
| 73 | + assert.isFalse(result, 'Should return false when inlineMethods is true'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns false when not WebAPI (e.g., AsyncAPI)', async () => { |
| 77 | + const element = await appFixture(grpcAmf); |
| 78 | + await nextFrame(); |
| 79 | + |
| 80 | + // Mock _isWebAPI to return false |
| 81 | + const originalIsWebAPI = element._isWebAPI; |
| 82 | + element._isWebAPI = () => false; |
| 83 | + |
| 84 | + const result = element._computeRenderInlineTryIt(true, true, false); |
| 85 | + assert.isFalse(result, 'Should return false when not WebAPI'); |
| 86 | + |
| 87 | + // Restore original method |
| 88 | + element._isWebAPI = originalIsWebAPI; |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('_computeNoTryItValue()', () => { |
| 93 | + it('returns true for gRPC API', async () => { |
| 94 | + const element = await appFixture(grpcAmf); |
| 95 | + await nextFrame(); |
| 96 | + |
| 97 | + const result = element._computeNoTryItValue(false, false); |
| 98 | + assert.isTrue(result, 'Should return true for gRPC API'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns false for regular WebAPI when noTryIt is false', async () => { |
| 102 | + const element = await appFixture(regularAmf); |
| 103 | + await nextFrame(); |
| 104 | + |
| 105 | + const result = element._computeNoTryItValue(false, false); |
| 106 | + assert.isFalse(result, 'Should return false for regular WebAPI'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('returns true when renderInlineTyit is true', async () => { |
| 110 | + const element = await appFixture(regularAmf); |
| 111 | + await nextFrame(); |
| 112 | + |
| 113 | + const result = element._computeNoTryItValue(false, true); |
| 114 | + assert.isTrue(result, 'Should return true when renderInlineTyit is true'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns true when noTryIt is explicitly true', async () => { |
| 118 | + const element = await appFixture(regularAmf); |
| 119 | + await nextFrame(); |
| 120 | + |
| 121 | + const result = element._computeNoTryItValue(true, false); |
| 122 | + assert.isTrue(result, 'Should return true when noTryIt is true'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('returns true when not WebAPI', async () => { |
| 126 | + const element = await appFixture(grpcAmf); |
| 127 | + await nextFrame(); |
| 128 | + |
| 129 | + // Mock _isWebAPI to return false |
| 130 | + const originalIsWebAPI = element._isWebAPI; |
| 131 | + element._isWebAPI = () => false; |
| 132 | + |
| 133 | + const result = element._computeNoTryItValue(false, false); |
| 134 | + assert.isTrue(result, 'Should return true when not WebAPI'); |
| 135 | + |
| 136 | + // Restore original method |
| 137 | + element._isWebAPI = originalIsWebAPI; |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('try-panel rendering', () => { |
| 142 | + it('does not render inline try-panel for gRPC API', async () => { |
| 143 | + const element = await appFixture(grpcAmf); |
| 144 | + await nextFrame(); |
| 145 | + |
| 146 | + // Set wide layout and select a method |
| 147 | + element.wideLayout = true; |
| 148 | + const webApi = AmfLoader.lookupWebApi(grpcAmf); |
| 149 | + const endpoints = element._computeEndpoints(webApi); |
| 150 | + if (endpoints && endpoints.length > 0) { |
| 151 | + const operations = element._computePropertyArray( |
| 152 | + endpoints[0], |
| 153 | + element.ns.aml.vocabularies.apiContract.supportedOperation |
| 154 | + ); |
| 155 | + if (operations && operations.length > 0) { |
| 156 | + element.selectedShape = operations[0]['@id']; |
| 157 | + element.selectedShapeType = 'method'; |
| 158 | + await nextFrame(); |
| 159 | + await nextFrame(); |
| 160 | + |
| 161 | + // Check that inline try-panel is not rendered |
| 162 | + const inlineRequest = element.shadowRoot.querySelector('.inline-request'); |
| 163 | + assert.isNull(inlineRequest, 'Inline try-panel should not be rendered for gRPC API'); |
| 164 | + } |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + it('hides try-it button for gRPC API', async () => { |
| 169 | + const element = await appFixture(grpcAmf); |
| 170 | + await nextFrame(); |
| 171 | + |
| 172 | + // Select a method |
| 173 | + const webApi = AmfLoader.lookupWebApi(grpcAmf); |
| 174 | + const endpoints = element._computeEndpoints(webApi); |
| 175 | + if (endpoints && endpoints.length > 0) { |
| 176 | + const operations = element._computePropertyArray( |
| 177 | + endpoints[0], |
| 178 | + element.ns.aml.vocabularies.apiContract.supportedOperation |
| 179 | + ); |
| 180 | + if (operations && operations.length > 0) { |
| 181 | + element.selectedShape = operations[0]['@id']; |
| 182 | + element.selectedShapeType = 'method'; |
| 183 | + await nextFrame(); |
| 184 | + await nextFrame(); |
| 185 | + |
| 186 | + // Check that _noTryItValue is true |
| 187 | + assert.isTrue(element._noTryItValue, 'Try-it button should be hidden for gRPC API'); |
| 188 | + } |
| 189 | + } |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + describe('_isGrpcApi() detection', () => { |
| 194 | + it('correctly detects gRPC API', async () => { |
| 195 | + const element = await appFixture(grpcAmf); |
| 196 | + await nextFrame(); |
| 197 | + |
| 198 | + const isGrpc = element._isGrpcApi(grpcAmf); |
| 199 | + assert.isTrue(isGrpc, 'Should detect gRPC API'); |
| 200 | + }); |
| 201 | + |
| 202 | + it('correctly detects non-gRPC API', async () => { |
| 203 | + const element = await appFixture(regularAmf); |
| 204 | + await nextFrame(); |
| 205 | + |
| 206 | + const isGrpc = element._isGrpcApi(regularAmf); |
| 207 | + assert.isFalse(isGrpc, 'Should not detect regular API as gRPC'); |
| 208 | + }); |
| 209 | + |
| 210 | + it('returns false when amf is null or undefined', async () => { |
| 211 | + const element = await appFixture(regularAmf); |
| 212 | + await nextFrame(); |
| 213 | + |
| 214 | + const isGrpcNull = element._isGrpcApi(null); |
| 215 | + const isGrpcUndefined = element._isGrpcApi(undefined); |
| 216 | + |
| 217 | + assert.isFalse(isGrpcNull, 'Should return false for null'); |
| 218 | + assert.isFalse(isGrpcUndefined, 'Should return false for undefined'); |
| 219 | + }); |
| 220 | + }); |
| 221 | + |
| 222 | + describe('_updateRenderInlineTyit() integration', () => { |
| 223 | + it('updates _renderInlineTryit and _noTryItValue when wideLayout changes', async () => { |
| 224 | + const element = await appFixture(regularAmf); |
| 225 | + await nextFrame(); |
| 226 | + |
| 227 | + element.selectedShapeType = 'method'; |
| 228 | + element.wideLayout = true; |
| 229 | + await nextFrame(); |
| 230 | + |
| 231 | + assert.isTrue(element._renderInlineTryit, 'Should render inline tryit for regular API'); |
| 232 | + assert.isTrue(element._noTryItValue, 'Should hide try-it button when inline is rendered'); |
| 233 | + }); |
| 234 | + |
| 235 | + it('updates _renderInlineTryit and _noTryItValue when switching to gRPC API', async () => { |
| 236 | + const element = await appFixture(regularAmf); |
| 237 | + await nextFrame(); |
| 238 | + |
| 239 | + element.wideLayout = true; |
| 240 | + element.selectedShapeType = 'method'; |
| 241 | + await nextFrame(); |
| 242 | + await element.updateComplete; |
| 243 | + |
| 244 | + // Initially should render inline tryit for regular API |
| 245 | + assert.isTrue(element._renderInlineTryit, 'Should initially render inline tryit'); |
| 246 | + |
| 247 | + // Switch to gRPC API |
| 248 | + element.amf = grpcAmf; |
| 249 | + await element.updateComplete; |
| 250 | + await nextFrame(); |
| 251 | + |
| 252 | + // Manually trigger update since _processModelChange doesn't call _updateRenderInlineTyit |
| 253 | + // (to avoid performance issues with large APIs) |
| 254 | + element._updateRenderInlineTyit(); |
| 255 | + await element.updateComplete; |
| 256 | + |
| 257 | + // Should not render inline tryit for gRPC |
| 258 | + assert.isFalse(element._renderInlineTryit, 'Should not render inline tryit for gRPC'); |
| 259 | + assert.isTrue(element._noTryItValue, 'Should hide try-it button for gRPC'); |
| 260 | + }); |
| 261 | + }); |
| 262 | + }); |
| 263 | +}); |
0 commit comments