@@ -2,39 +2,123 @@ import { useDebounce } from "@vueuse/core"
22import { useUrl , createUseUrlInstance } from "../src/index"
33import { ref , reactive , set } from "vue-demi"
44
5- const search = ref ( "query" )
6- const filters = ref ( [ "filter1" , "filter2" , "filter3" ] )
5+ const urlBuilder = createUseUrlInstance ( )
76
8- const params = reactive ( {
9- search : "query" ,
10- param : "something" ,
11- } )
7+ test ( 'Path variables test' , ( ) => {
8+ const { url } = urlBuilder (
9+ {
10+ path : "/api/v1/entity/:id" ,
11+ pathVariables : {
12+ id : ref ( 1 ) ,
13+ } ,
14+ } ,
15+ "https://somedomain.com/" ,
16+ )
17+ expect ( url . value ) . toBe ( 'https://somedomain.com/api/v1/entity/1' ) ;
18+ } ) ;
19+
20+
21+ test ( 'Query params test' , ( ) => {
22+ const { url } = urlBuilder (
23+ {
24+ path : "/api/v1/entity/list" ,
25+ queryParams : {
26+ string : ref ( 'query' ) ,
27+ boolean : false ,
28+ someUndefined : undefined ,
29+ someNull : null ,
30+ } ,
31+ } ,
32+ "https://somedomain.com/" ,
33+ )
34+ expect ( url . value ) . toBe ( 'https://somedomain.com/api/v1/entity/list?string=query&boolean=false' ) ;
35+ } ) ;
36+
37+
38+ test ( 'Query params CSV false test' , ( ) => {
39+ const { url } = urlBuilder (
40+ {
41+ path : "/api/v1/entity/list" ,
42+ queryParams : {
43+ string : [ 'query1' , 'query2' , 'query3' ] ,
44+ } ,
45+ // disableCSV: false
46+ } ,
47+ "https://somedomain.com/" ,
48+ )
49+ expect ( url . value ) . toBe ( 'https://somedomain.com/api/v1/entity/list?string=query1,query2,query3' ) ;
50+ } ) ;
51+
52+
53+ test ( 'Query params CSV true test' , ( ) => {
54+ const { url } = urlBuilder (
55+ {
56+ path : "/api/v1/entity/list" ,
57+ queryParams : {
58+ string : [ 'query1' , 'query2' , 'query3' ] ,
59+ } ,
60+ disableCSV : true
61+ } ,
62+ "https://somedomain.com/" ,
63+ )
64+ expect ( url . value ) . toBe ( 'https://somedomain.com/api/v1/entity/list?string=query1&string=query2&string=query3' ) ;
65+ } ) ;
66+
67+
68+ test ( 'Hash test' , ( ) => {
69+ const { url } = urlBuilder (
70+ {
71+ path : "/api/v1/entity/list" ,
72+ hash : "hash" ,
73+ } ,
74+ "https://somedomain.com/" ,
75+ )
76+ expect ( url . value ) . toBe ( 'https://somedomain.com/api/v1/entity/list#hash' ) ;
77+ } ) ;
78+
79+
80+ test ( 'All together test' , ( ) => {
81+ const { url } = urlBuilder (
82+ {
83+ path : "/api/v1/entity/:id" ,
84+ pathVariables : {
85+ id : ref ( 1 ) ,
86+ } ,
87+ queryParams : {
88+ string : ref ( 'query' ) ,
89+ boolean : false ,
90+ someUndefined : undefined ,
91+ someNull : null ,
92+ } ,
93+ hash : "hash" ,
94+ } ,
95+ "https://somedomain.com/" ,
96+ )
97+ expect ( url . value ) . toBe ( 'https://somedomain.com/api/v1/entity/1?string=query&boolean=false#hash' ) ;
98+ } ) ;
1299
13- const urlBuilder = createUseUrlInstance ( )
14100
15- const { url, queryParams, pathVariables, hash, path, disableCSV } = urlBuilder (
16- {
17- path : "/api/v1/entity/:id/subentity" ,
18- pathVariables : {
19- id : 1 ,
101+ test ( 'Reactivity test' , ( ) => {
102+ const id = ref ( 1 )
103+ const queryParam = ref ( 'query' )
104+ const { url } = urlBuilder (
105+ {
106+ path : "/api/v1/entity/:id" ,
107+ pathVariables : {
108+ id : id ,
109+ } ,
110+ queryParams : {
111+ string : queryParam ,
112+ boolean : false ,
113+ someUndefined : undefined ,
114+ someNull : null ,
115+ } ,
116+ hash : "hash" ,
20117 } ,
21- queryParams : params ,
22- hash : "someHash" ,
23- } ,
24- "https://somedomain.com/" ,
25- )
26-
27- console . log ( url . value )
28-
29- set ( params , "s" , ref ( "extra" ) )
30- setInterval ( ( ) => {
31- pathVariables . id = ( pathVariables . id as number ) + 1
32- console . log ( url . value )
33- } , 1000 )
34-
35- /*search.value = "newQuery"
36- filters.value = ["newFilter1" , "newFilter"]
37-
38- setTimeout(() => {
39- console.log(url.value)
40- },500)*/
118+ "https://somedomain.com/" ,
119+ )
120+ expect ( url . value ) . toBe ( 'https://somedomain.com/api/v1/entity/1?string=query&boolean=false#hash' ) ;
121+ id . value = 2
122+ queryParam . value = 'query2'
123+ expect ( url . value ) . toBe ( 'https://somedomain.com/api/v1/entity/2?string=query2&boolean=false#hash' ) ;
124+ } ) ;
0 commit comments