1+ import { renderHook } from '@testing-library/react-hooks' ;
2+ import { describe , expect , it } from "vitest" ;
3+ import usePropertyReplacement from "./UsePropertyReplacement" ;
4+
5+ describe ( "usePropertyReplacement additional tests" , ( ) => {
6+ const properties = {
7+ name : "Test Name" ,
8+ id : "123" ,
9+ nested : {
10+ property : "nested value" ,
11+ deep : {
12+ key : "deep value"
13+ }
14+ } ,
15+ special : "value with special characters: !@#$%^&*()"
16+ } ;
17+
18+ it ( "url encodes markdown link replacements" , ( ) => {
19+ const content = "[link](https://example.com/{{nested.deep.key}})" ;
20+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
21+ expect ( result . current ) . toBe ( "[link](https://example.com/deep%20value)" ) ;
22+ } ) ;
23+
24+ it ( "performs basic url encoded replacement in markdown link" , ( ) => {
25+ const content = "[link](https://example.com/{{name}})" ;
26+ const expected = "[link](https://example.com/Test%20Name)" ;
27+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
28+ expect ( result . current ) . toBe ( expected ) ;
29+ } ) ;
30+
31+ it ( "ignores whitespace between link text and URL in markdown link" , ( ) => {
32+ const content = "[link] (https://example.com/{{name}})" ;
33+ const expected = "[link](https://example.com/Test%20Name)" ;
34+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
35+ expect ( result . current ) . toBe ( expected ) ;
36+ } ) ;
37+
38+ it ( "falls through to preexisting replacement behavior for invalid link format" , ( ) => {
39+ const content = "[link] hello there (https://example.com/{{name}})" ;
40+ const expected = "[link] hello there (https://example.com/Test Name)" ;
41+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
42+ expect ( result . current ) . toBe ( expected ) ;
43+ } ) ;
44+
45+ it ( "does not url encode standalone replacements outside markdown links" , ( ) => {
46+ const content = "[link](https://example.com/) blab blah {{name}} blah [another link](https://example.com/)" ;
47+ const expected = "[link](https://example.com/) blab blah Test Name blah [another link](https://example.com/)" ;
48+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
49+ expect ( result . current ) . toBe ( expected ) ;
50+ } ) ;
51+
52+ it ( "does not url encode standalone replacements when no markdown links are present" , ( ) => {
53+ const content = "hello there (foo {{name}})" ;
54+ const expected = "hello there (foo Test Name)" ;
55+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
56+ expect ( result . current ) . toBe ( expected ) ;
57+ } ) ;
58+
59+ it ( "only url encodes replacements in markdown links" , ( ) => {
60+ const content = "{{nested.deep.key}} [link](https://example.com/{{nested.deep.key}})" ;
61+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties , true , false ) ) ;
62+ expect ( result . current ) . toBe ( "deep value [link](https://example.com/deep%20value)" ) ;
63+ } ) ;
64+
65+ it ( "handles empty properties object" , ( ) => {
66+ const content = "Hello, {{name}}!" ;
67+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , { } ) ) ;
68+ expect ( result . current ) . toBe ( "Hello, !" ) ;
69+ } ) ;
70+
71+ it ( "honors allowPropertyReplacement = false when data missing" , ( ) => {
72+ const content = "Hello, {{name}}!" ;
73+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , { } , false ) ) ;
74+ expect ( result . current ) . toBe ( "Hello, {{name}}!" ) ;
75+ } ) ;
76+
77+ it ( "honors allowPropertyReplacement = false when data present" , ( ) => {
78+ const content = "Hello, {{name}}!" ;
79+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties , false ) ) ;
80+ expect ( result . current ) . toBe ( "Hello, {{name}}!" ) ;
81+ } ) ;
82+
83+ it ( "processes content with multiple nested properties" , ( ) => {
84+ const content = "Nested: {{nested.property}}, Deep: {{nested.deep.key}}" ;
85+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
86+ expect ( result . current ) . toBe ( "Nested: nested value, Deep: deep value" ) ;
87+ } ) ;
88+
89+ it ( "handles content with no mustache tags" , ( ) => {
90+ const content = "This is plain text." ;
91+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
92+ expect ( result . current ) . toBe ( content ) ;
93+ } ) ;
94+
95+ it ( "handles content with special characters in property values" , ( ) => {
96+ const content = "Special: {{special}}" ;
97+ const { result } = renderHook ( ( ) => usePropertyReplacement ( content , properties ) ) ;
98+ expect ( result . current ) . toBe ( "Special: value with special characters: !@#$%^&*()" ) ;
99+ } ) ;
100+ } ) ;
0 commit comments