@@ -28,6 +28,12 @@ describe('TrustPilot Class', () => {
28
28
const enrichedUrl = trustPilot [ 'enrichUrl' ] ( inputUrl ) ;
29
29
expect ( enrichedUrl ) . toBe ( inputUrl ) ;
30
30
} ) ;
31
+
32
+ it ( 'should handle URLs without schema' , ( ) => {
33
+ const inputUrl = 'www.trustpilot.com/review/amazon.pl' ;
34
+ const enrichedUrl = trustPilot [ 'enrichUrl' ] ( inputUrl ) ;
35
+ expect ( enrichedUrl ) . toBe ( 'https://www.trustpilot.com/review/www.trustpilot.com/review/amazon.pl' ) ;
36
+ } ) ;
31
37
} ) ;
32
38
33
39
describe ( 'factory' , ( ) => {
@@ -36,7 +42,9 @@ describe('TrustPilot Class', () => {
36
42
<div data-rating-typography="true">4.5</div>
37
43
<span class="typography_display-s__qOjh6">Amazon</span>
38
44
<img alt="TrustScore 4.5 out of 5">
39
- <span role="link" tabindex="0" class="styles_clickable__zQWyh"><span class="typography_body-l__KUYFJ typography_appearance-subtle__8_H2l styles_text__W4hWi">163 • Bad</span></span>
45
+ <span class="styles_clickable__zQWyh">
46
+ <span class="typography_body-l__KUYFJ typography_appearance-subtle__8_H2l styles_text__W4hWi">163 • Bad</span>
47
+ </span>
40
48
` ;
41
49
42
50
( axios . get as jest . Mock ) . mockResolvedValue ( { data : html } ) ;
@@ -52,8 +60,49 @@ describe('TrustPilot Class', () => {
52
60
} ) ;
53
61
} ) ;
54
62
63
+ it ( 'should handle missing ratingDescription gracefully' , async ( ) => {
64
+ const html = `
65
+ <div data-rating-typography="true">4.5</div>
66
+ <span class="typography_display-s__qOjh6">Amazon</span>
67
+ <img alt="TrustScore 4.5 out of 5">
68
+ <span class="styles_clickable__zQWyh"><span class="typography_body-l__KUYFJ typography_appearance-subtle__8_H2l styles_text__W4hWi">163 • </span></span>
69
+ ` ;
70
+
71
+ ( axios . get as jest . Mock ) . mockResolvedValue ( { data : html } ) ;
72
+
73
+ const data = await trustPilot . factory ( 'amazon.pl' ) ;
74
+
75
+ expect ( data ) . toEqual ( {
76
+ companyName : 'Amazon' ,
77
+ totalReviews : '163' ,
78
+ trustScore : '4.5' ,
79
+ ratingDescription : 'no customer reviews for this site' , // default message
80
+ rating : '4.5' ,
81
+ } ) ;
82
+ } ) ;
83
+
84
+ it ( 'should handle case with no total reviews' , async ( ) => {
85
+ const html = `
86
+ <div data-rating-typography="true">4.5</div>
87
+ <span class="typography_display-s__qOjh6">Amazon</span>
88
+ <img alt="TrustScore 4.5 out of 5">
89
+ <span class="styles_clickable__zQWyh"><span class="typography_body-l__KUYFJ typography_appearance-subtle__8_H2l styles_text__W4hWi"> • Bad</span></span>
90
+ ` ;
91
+
92
+ ( axios . get as jest . Mock ) . mockResolvedValue ( { data : html } ) ;
93
+
94
+ const data = await trustPilot . factory ( 'amazon.pl' ) ;
95
+
96
+ expect ( data ) . toEqual ( {
97
+ companyName : 'Amazon' ,
98
+ totalReviews : '' , // no total reviews
99
+ trustScore : '4.5' ,
100
+ ratingDescription : 'Bad' ,
101
+ rating : '4.5' ,
102
+ } ) ;
103
+ } ) ;
104
+
55
105
it ( 'should handle request errors gracefully' , async ( ) => {
56
- // Mock axios.get to throw an error
57
106
( axios . get as jest . Mock ) . mockRejectedValue ( new Error ( 'Request failed' ) ) ;
58
107
59
108
const result = await trustPilot . factory ( 'invalidurl.com' ) ;
0 commit comments