@@ -2,6 +2,7 @@ import { TestBed, type ComponentFixture } from '@angular/core/testing';
22import type { DevframeRpcClient } from 'devframe/client' ;
33import { afterEach , describe , expect , it } from 'vitest' ;
44import { AnalogInspector } from '../pages/analog-inspector' ;
5+ import { AnalogPlayground , fillPattern , patternParams } from '../pages/analog-playground' ;
56
67const project = { analog : true , routes : [ ] , api : [ ] , middleware : [ ] , content : [ ] } ;
78
@@ -13,7 +14,14 @@ function client() {
1314 if ( name === 'analog-render' ) return Promise . resolve ( { rows : [ ] , plan : null } ) ;
1415 if ( name === 'analog-call-api' ) {
1516 sent . push ( arg ?? { } ) ;
16- return Promise . resolve ( { status : 201 , ms : 3 , body : '{}' } ) ;
17+ return Promise . resolve ( {
18+ status : 201 ,
19+ ms : 3 ,
20+ headersMs : 2 ,
21+ size : 2 ,
22+ headers : { 'x-id' : '1' } ,
23+ body : '{}' ,
24+ } ) ;
1725 }
1826 return Promise . resolve ( [ ] ) ;
1927 } ,
@@ -33,10 +41,10 @@ async function settle(fixture: ComponentFixture<unknown>) {
3341
3442async function render ( ) {
3543 const { rpcClient, sent } = client ( ) ;
36- const fixture = TestBed . createComponent ( AnalogInspector ) ;
44+ const fixture = TestBed . createComponent ( AnalogPlayground ) ;
3745 fixture . componentRef . setInput ( 'rpc' , rpcClient ) ;
3846 await settle ( fixture ) ;
39- return { inspector : fixture . componentInstance , sent } ;
47+ return { playground : fixture . componentInstance , sent, fixture } ;
4048}
4149
4250afterEach ( ( ) => {
@@ -45,44 +53,119 @@ afterEach(() => {
4553
4654describe ( 'Analog request playground confirmation' , ( ) => {
4755 it ( 'asks again after a confirmed POST is sent' , async ( ) => {
48- const { inspector , sent } = await render ( ) ;
49- inspector . method . set ( 'POST' ) ;
50- inspector . apiPath . set ( '/api/v1/products' ) ;
51- inspector . confirmSend . set ( true ) ;
52- expect ( inspector . canSend ( ) ) . toBe ( true ) ;
56+ const { playground , sent } = await render ( ) ;
57+ playground . method . set ( 'POST' ) ;
58+ playground . template . set ( '/api/v1/products' ) ;
59+ playground . confirmSend . set ( true ) ;
60+ expect ( playground . canSend ( ) ) . toBe ( true ) ;
5361
54- await inspector . send ( ) ;
62+ await playground . send ( ) ;
5563 expect ( sent ) . toEqual ( [
5664 expect . objectContaining ( { method : 'POST' , path : '/api/v1/products' , confirm : true } ) ,
5765 ] ) ;
58- expect ( inspector . confirmSend ( ) ) . toBe ( false ) ;
59- expect ( inspector . needsConfirm ( ) ) . toBe ( true ) ;
66+ expect ( playground . confirmSend ( ) ) . toBe ( false ) ;
67+ expect ( playground . needsConfirm ( ) ) . toBe ( true ) ;
6068
61- await inspector . send ( ) ;
69+ await playground . send ( ) ;
6270 expect ( sent ) . toHaveLength ( 1 ) ;
6371 } ) ;
6472
65- it ( 'asks again when the method or the path changes' , async ( ) => {
66- const { inspector } = await render ( ) ;
67- inspector . method . set ( 'POST' ) ;
68- inspector . apiPath . set ( '/api/v1/products' ) ;
69- inspector . confirmSend . set ( true ) ;
73+ it ( 'asks again when the method or the URL changes' , async ( ) => {
74+ const { playground } = await render ( ) ;
75+ playground . method . set ( 'POST' ) ;
76+ playground . template . set ( '/api/v1/products/:id ' ) ;
77+ playground . confirmSend . set ( true ) ;
7078
71- inspector . apiPath . set ( '/api/v1/orders ') ;
72- expect ( inspector . needsConfirm ( ) ) . toBe ( true ) ;
79+ playground . setParam ( 'id' , '7 ') ;
80+ expect ( playground . needsConfirm ( ) ) . toBe ( true ) ;
7381
74- inspector . confirmSend . set ( true ) ;
75- inspector . method . set ( 'DELETE' ) ;
76- expect ( inspector . needsConfirm ( ) ) . toBe ( true ) ;
82+ playground . confirmSend . set ( true ) ;
83+ playground . method . set ( 'DELETE' ) ;
84+ expect ( playground . needsConfirm ( ) ) . toBe ( true ) ;
7785 } ) ;
7886
7987 it ( 'asks again when Try loads another route' , async ( ) => {
80- const { inspector } = await render ( ) ;
81- inspector . method . set ( 'PUT' ) ;
82- inspector . apiPath . set ( '/api/v1/products/1' ) ;
83- inspector . confirmSend . set ( true ) ;
88+ const { playground , fixture } = await render ( ) ;
89+ playground . method . set ( 'PUT' ) ;
90+ playground . template . set ( '/api/v1/products/1' ) ;
91+ playground . confirmSend . set ( true ) ;
8492
85- inspector . tryApi ( { method : 'PUT' , path : '/api/v1/orders/:id' , file : 'orders.put.ts' } ) ;
86- expect ( inspector . needsConfirm ( ) ) . toBe ( true ) ;
93+ fixture . componentRef . setInput ( 'target' , {
94+ route : { method : 'PUT' , path : '/api/v1/orders/:id' , file : 'orders.put.ts' } ,
95+ nonce : 1 ,
96+ } ) ;
97+ await settle ( fixture ) ;
98+ expect ( playground . template ( ) ) . toBe ( '/api/v1/orders/:id' ) ;
99+ expect ( playground . needsConfirm ( ) ) . toBe ( true ) ;
100+ } ) ;
101+ } ) ;
102+
103+ describe ( 'Analog request playground' , ( ) => {
104+ it ( 'fills params, adds query, headers and cookies, and keeps history per route' , async ( ) => {
105+ const { playground, sent } = await render ( ) ;
106+ playground . template . set ( '/api/v1/products/:id' ) ;
107+ expect ( playground . params ( ) ) . toEqual ( [ 'id' ] ) ;
108+ playground . setParam ( 'id' , 'a b' ) ;
109+ playground . addPair ( 'query' ) ;
110+ const q = playground . query ( ) [ 0 ] ;
111+ playground . editPair ( 'query' , q . id , 'key' , 'sort' ) ;
112+ playground . editPair ( 'query' , q . id , 'value' , 'price' ) ;
113+ playground . addPair ( 'headers' ) ;
114+ playground . editPair ( 'headers' , playground . headers ( ) [ 0 ] . id , 'key' , 'accept-language' ) ;
115+ playground . editPair ( 'headers' , playground . headers ( ) [ 0 ] . id , 'value' , 'de' ) ;
116+ playground . addPair ( 'cookies' ) ;
117+ playground . editPair ( 'cookies' , playground . cookies ( ) [ 0 ] . id , 'key' , 'theme' ) ;
118+ playground . editPair ( 'cookies' , playground . cookies ( ) [ 0 ] . id , 'value' , 'dark' ) ;
119+ expect ( playground . url ( ) ) . toBe ( '/api/v1/products/a%20b?sort=price' ) ;
120+
121+ await playground . send ( ) ;
122+ expect ( sent [ 0 ] ) . toMatchObject ( {
123+ method : 'GET' ,
124+ path : '/api/v1/products/a%20b?sort=price' ,
125+ headers : { 'accept-language' : 'de' } ,
126+ cookies : { theme : 'dark' } ,
127+ } ) ;
128+ expect ( playground . history ( ) ) . toHaveLength ( 1 ) ;
129+ expect ( playground . history ( ) [ 0 ] ) . toMatchObject ( { status : 201 , ms : 3 } ) ;
130+
131+ playground . template . set ( '/api/v1/other' ) ;
132+ expect ( playground . history ( ) ) . toEqual ( [ ] ) ;
133+ playground . restore ( playground . historyByRoute ( ) [ '/api/v1/products/:id' ] [ 0 ] ) ;
134+ expect ( playground . url ( ) ) . toBe ( '/api/v1/products/a%20b?sort=price' ) ;
135+ } ) ;
136+
137+ it ( 'copies the request as curl and as fetch' , async ( ) => {
138+ const { playground, fixture } = await render ( ) ;
139+ fixture . componentRef . setInput ( 'origin' , 'http://localhost:5173' ) ;
140+ playground . method . set ( 'POST' ) ;
141+ playground . template . set ( "/api/v1/it's" ) ;
142+ playground . body . set ( '{"a":1}' ) ;
143+ expect ( playground . command ( 'curl' ) ) . toBe (
144+ "curl -X POST 'http://localhost:5173/api/v1/it'\\''s' \\\n -H 'content-type: application/json' \\\n --data '{\"a\":1}'" ,
145+ ) ;
146+ expect ( playground . command ( 'fetch' ) ) . toContain (
147+ 'await fetch("http://localhost:5173/api/v1/it\'s"' ,
148+ ) ;
149+ } ) ;
150+
151+ it ( 'reads params from patterns, catch-alls included' , ( ) => {
152+ expect ( patternParams ( '/a/:id/b/:slug' ) ) . toEqual ( [ 'id' , 'slug' ] ) ;
153+ expect ( patternParams ( '/docs/**' ) ) . toEqual ( [ '**' ] ) ;
154+ expect ( fillPattern ( '/docs/**' , { '**' : 'a b/c' } ) ) . toBe ( '/docs/a%20b/c' ) ;
155+ expect ( fillPattern ( '/a/:id' , { } ) ) . toBe ( '/a/:id' ) ;
156+ } ) ;
157+
158+ it ( 'hands a Try route from the inspector to the playground' , async ( ) => {
159+ const { rpcClient } = client ( ) ;
160+ const fixture = TestBed . createComponent ( AnalogInspector ) ;
161+ fixture . componentRef . setInput ( 'rpc' , rpcClient ) ;
162+ await settle ( fixture ) ;
163+ fixture . componentInstance . tryApi ( { method : 'ANY' , path : '/api/v1/hello' , file : 'h.ts' } ) ;
164+ expect ( fixture . componentInstance . tryTarget ( ) ) . toMatchObject ( { nonce : 1 } ) ;
165+ fixture . componentInstance . view . set ( 'render' ) ;
166+ expect ( fixture . componentInstance . tryTarget ( ) ) . toBeNull ( ) ;
167+ fixture . componentInstance . view . set ( 'server' ) ;
168+ fixture . componentInstance . tryApi ( { method : 'GET' , path : '/api/v1/hello' , file : 'h.ts' } ) ;
169+ expect ( fixture . componentInstance . tryTarget ( ) ) . toMatchObject ( { nonce : 2 } ) ;
87170 } ) ;
88171} ) ;
0 commit comments