Skip to content

Commit a1c7c7f

Browse files
committed
add test case
1 parent 4839400 commit a1c7c7f

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

src/app/modules/provider/providers/trustpilot.ts.spec.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ describe('TrustPilot Class', () => {
2828
const enrichedUrl = trustPilot['enrichUrl'](inputUrl);
2929
expect(enrichedUrl).toBe(inputUrl);
3030
});
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+
});
3137
});
3238

3339
describe('factory', () => {
@@ -36,7 +42,9 @@ describe('TrustPilot Class', () => {
3642
<div data-rating-typography="true">4.5</div>
3743
<span class="typography_display-s__qOjh6">Amazon</span>
3844
<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&nbsp; • &nbsp;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&nbsp; • &nbsp;Bad</span>
47+
</span>
4048
`;
4149

4250
(axios.get as jest.Mock).mockResolvedValue({ data: html });
@@ -52,8 +60,49 @@ describe('TrustPilot Class', () => {
5260
});
5361
});
5462

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&nbsp; • &nbsp;</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"> • &nbsp;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+
55105
it('should handle request errors gracefully', async () => {
56-
// Mock axios.get to throw an error
57106
(axios.get as jest.Mock).mockRejectedValue(new Error('Request failed'));
58107

59108
const result = await trustPilot.factory('invalidurl.com');

0 commit comments

Comments
 (0)