@@ -2,8 +2,12 @@ import { v4 as uuidv4 } from 'uuid';
22import {
33 sendRequest ,
44 FlagFeedbackEvent ,
5+ RecommendationsEvent ,
6+ RecommendationsInteractionEvent ,
57 FeedbackTypeOptions ,
68 FLAG_FEEDBACK_EVENT_URL ,
9+ RECCOMMENDATION_EVENT_URL ,
10+ RECCOMMENDATION_INTERACTION_EVENT_URL ,
711} from '../feedbackApiUtils' ;
812import client from '../client' ;
913
@@ -12,6 +16,8 @@ jest.mock('../client');
1216
1317describe ( 'FeedBackUtility Tests' , ( ) => {
1418 let flagFeedbackEvent ;
19+ let recommendationsEvent ;
20+ let recommendationsInteractionEvent ;
1521
1622 afterEach ( ( ) => {
1723 jest . clearAllMocks ( ) ;
@@ -26,54 +32,301 @@ describe('FeedBackUtility Tests', () => {
2632 feedback_type : FeedbackTypeOptions . flagged ,
2733 feedback_reason : 'Inappropriate Language' ,
2834 } ) ;
35+
36+ recommendationsEvent = new RecommendationsEvent ( {
37+ context : { model_version : 1 , breadcrumbs : '#Title#->Random' } ,
38+ contentnode_id : uuidv4 ( ) ,
39+ content_id : uuidv4 ( ) ,
40+ target_channel_id : uuidv4 ( ) ,
41+ user_id : uuidv4 ( ) ,
42+ content : [
43+ {
44+ content_id : uuidv4 ( ) ,
45+ node_id : uuidv4 ( ) ,
46+ channel_id : uuidv4 ( ) ,
47+ score : 4 ,
48+ } ,
49+ ] ,
50+ } ) ;
51+
52+ recommendationsInteractionEvent = new RecommendationsInteractionEvent ( {
53+ context : { test_key : 'test_value' } ,
54+ contentnode_id : uuidv4 ( ) ,
55+ content_id : uuidv4 ( ) ,
56+ feedback_type : FeedbackTypeOptions . ignored ,
57+ feedback_reason : '----' ,
58+ recommendation_event_id : uuidv4 ( ) , //currently this is random to test but should have the actual
59+ // recommendation event id of the recommendation event
60+ } ) ;
61+
62+ // Reset all client method mocks
2963 client . post . mockRestore ( ) ;
64+ client . put . mockRestore ( ) ;
65+ client . patch . mockRestore ( ) ;
66+ client . delete . mockRestore ( ) ;
67+ client . get . mockRestore ( ) ;
3068 } ) ;
3169
32- it ( 'should generate data object without functions' , ( ) => {
33- const dataObject = flagFeedbackEvent . getDataObject ( ) ;
34- expect ( dataObject . id ) . toEqual ( 'mocked-uuid' ) ;
35- expect ( dataObject . context ) . toEqual ( { key : 'value' } ) ;
36- expect ( dataObject . contentnode_id ) . toEqual ( 'mocked-uuid' ) ;
37- expect ( dataObject . content_id ) . toEqual ( 'mocked-uuid' ) ;
70+ describe ( 'FlagFeedbackEvent Tests' , ( ) => {
71+ it ( 'should generate data object without functions' , ( ) => {
72+ const dataObject = flagFeedbackEvent . getDataObject ( ) ;
73+ expect ( dataObject . id ) . toEqual ( 'mocked-uuid' ) ;
74+ expect ( dataObject . context ) . toEqual ( { key : 'value' } ) ;
75+ expect ( dataObject . contentnode_id ) . toEqual ( 'mocked-uuid' ) ;
76+ expect ( dataObject . content_id ) . toEqual ( 'mocked-uuid' ) ;
77+ expect ( dataObject . getDataObject ) . toBeUndefined ( ) ;
78+ expect ( dataObject . target_topic_id ) . toEqual ( 'mocked-uuid' ) ;
79+ expect ( dataObject . feedback_type ) . toEqual ( FeedbackTypeOptions . flagged ) ;
80+ expect ( dataObject . feedback_reason ) . toEqual ( 'Inappropriate Language' ) ;
81+ expect ( dataObject . URL ) . toBeUndefined ( ) ;
82+ } ) ;
83+
84+ it ( 'should throw an error when URL is not defined' , ( ) => {
85+ flagFeedbackEvent . URL = undefined ;
86+ expect ( ( ) => flagFeedbackEvent . getUrl ( ) ) . toThrowError (
87+ 'URL is not defined for the FeedBack Object.' ,
88+ ) ;
89+ } ) ;
3890
39- expect ( dataObject . getDataObject ) . toBeUndefined ( ) ;
40- expect ( dataObject . target_topic_id ) . toEqual ( 'mocked-uuid' ) ;
41- expect ( dataObject . feedback_type ) . toEqual ( FeedbackTypeOptions . flagged ) ;
42- expect ( dataObject . feedback_reason ) . toEqual ( 'Inappropriate Language' ) ;
91+ it ( 'should return the correct URL when URL is defined' , ( ) => {
92+ const result = flagFeedbackEvent . getUrl ( ) ;
93+ expect ( result ) . toEqual ( FLAG_FEEDBACK_EVENT_URL ) ;
94+ } ) ;
4395
44- expect ( dataObject . URL ) . toBeUndefined ( ) ;
45- } ) ;
96+ it ( 'should send a request using sendRequest function' , async ( ) => {
97+ client . post . mockResolvedValue ( Promise . resolve ( { data : 'Mocked API Response' } ) ) ;
4698
47- it ( 'should throw an error when URL is not defined' , ( ) => {
48- flagFeedbackEvent . URL = undefined ;
49- expect ( ( ) => flagFeedbackEvent . getUrl ( ) ) . toThrowError (
50- 'URL is not defined for the FeedBack Object.' ,
51- ) ;
52- } ) ;
99+ const result = await sendRequest ( flagFeedbackEvent ) ;
100+
101+ expect ( result ) . toEqual ( 'Mocked API Response' ) ;
102+ expect ( client . post ) . toHaveBeenCalledWith (
103+ FLAG_FEEDBACK_EVENT_URL ,
104+ flagFeedbackEvent . getDataObject ( ) ,
105+ ) ;
106+ } ) ;
53107
54- it ( 'should return the correct URL when URL is defined' , ( ) => {
55- const result = flagFeedbackEvent . getUrl ( ) ;
56- expect ( result ) . toEqual ( FLAG_FEEDBACK_EVENT_URL ) ;
108+ it . skip ( 'should handle errors when sending a request using sendRequest function' , async ( ) => {
109+ client . post . mockRejectedValue ( new Error ( 'Mocked API Error' ) ) ;
110+ await expect ( sendRequest ( flagFeedbackEvent ) ) . rejects . toThrowError ( 'Mocked API Error' ) ;
111+ expect ( client . post ) . toHaveBeenCalledWith (
112+ FLAG_FEEDBACK_EVENT_URL ,
113+ flagFeedbackEvent . getDataObject ( ) ,
114+ ) ;
115+ } ) ;
57116 } ) ;
58117
59- it ( 'should send a request using sendRequest function' , async ( ) => {
60- client . post . mockResolvedValue ( Promise . resolve ( { data : 'Mocked API Response' } ) ) ;
118+ describe ( 'RecommendationsEvent Tests' , ( ) => {
119+ it ( 'should generate data object without functions' , ( ) => {
120+ const dataObject = recommendationsEvent . getDataObject ( ) ;
121+ expect ( dataObject . id ) . toEqual ( 'mocked-uuid' ) ;
122+ expect ( dataObject . context ) . toEqual ( { model_version : 1 , breadcrumbs : '#Title#->Random' } ) ;
123+ expect ( dataObject . contentnode_id ) . toEqual ( 'mocked-uuid' ) ;
124+ expect ( dataObject . content_id ) . toEqual ( 'mocked-uuid' ) ;
125+ expect ( dataObject . target_channel_id ) . toEqual ( 'mocked-uuid' ) ;
126+ expect ( dataObject . user_id ) . toEqual ( 'mocked-uuid' ) ;
127+ expect ( dataObject . content ) . toEqual ( [
128+ {
129+ content_id : 'mocked-uuid' ,
130+ node_id : 'mocked-uuid' ,
131+ channel_id : 'mocked-uuid' ,
132+ score : 4 ,
133+ } ,
134+ ] ) ;
135+ expect ( dataObject . getDataObject ) . toBeUndefined ( ) ;
136+ expect ( dataObject . URL ) . toBeUndefined ( ) ;
137+ } ) ;
138+
139+ it ( 'should throw an error when URL is not defined' , ( ) => {
140+ recommendationsEvent . URL = undefined ;
141+ expect ( ( ) => recommendationsEvent . getUrl ( ) ) . toThrowError (
142+ 'URL is not defined for the FeedBack Object.' ,
143+ ) ;
144+ } ) ;
145+
146+ it ( 'should return the correct URL when URL is defined' , ( ) => {
147+ const result = recommendationsEvent . getUrl ( ) ;
148+ expect ( result ) . toEqual ( RECCOMMENDATION_EVENT_URL ) ;
149+ } ) ;
150+
151+ describe ( 'HTTP Methods' , ( ) => {
152+ it ( 'should send POST request successfully' , async ( ) => {
153+ client . post . mockResolvedValue ( Promise . resolve ( { data : 'Mocked API Response' } ) ) ;
154+ const result = await sendRequest ( recommendationsEvent , 'post' ) ;
155+ expect ( result ) . toEqual ( 'Mocked API Response' ) ;
156+ expect ( client . post ) . toHaveBeenCalledWith (
157+ RECCOMMENDATION_EVENT_URL ,
158+ recommendationsEvent . getDataObject ( ) ,
159+ ) ;
160+ } ) ;
161+
162+ it ( 'should send PUT request successfully' , async ( ) => {
163+ client . put . mockResolvedValue ( Promise . resolve ( { data : 'Mocked API Response' } ) ) ;
164+ const result = await sendRequest ( recommendationsEvent , 'put' ) ;
165+ expect ( result ) . toEqual ( 'Mocked API Response' ) ;
166+ expect ( client . put ) . toHaveBeenCalledWith (
167+ RECCOMMENDATION_EVENT_URL ,
168+ recommendationsEvent . getDataObject ( ) ,
169+ ) ;
170+ } ) ;
171+
172+ it ( 'should send PATCH request successfully' , async ( ) => {
173+ client . patch . mockResolvedValue ( Promise . resolve ( { data : 'Mocked API Response' } ) ) ;
174+ const result = await sendRequest ( recommendationsEvent , 'patch' ) ;
175+ expect ( result ) . toEqual ( 'Mocked API Response' ) ;
176+ expect ( client . patch ) . toHaveBeenCalledWith (
177+ RECCOMMENDATION_EVENT_URL ,
178+ recommendationsEvent . getDataObject ( ) ,
179+ ) ;
180+ } ) ;
181+
182+ it ( 'should handle errors for POST request' , async ( ) => {
183+ client . post . mockRejectedValue ( new Error ( 'Mocked API Error' ) ) ;
184+ await expect ( sendRequest ( recommendationsEvent , 'post' ) ) . rejects . toThrowError (
185+ 'Mocked API Error' ,
186+ ) ;
187+ expect ( client . post ) . toHaveBeenCalledWith (
188+ RECCOMMENDATION_EVENT_URL ,
189+ recommendationsEvent . getDataObject ( ) ,
190+ ) ;
191+ } ) ;
192+
193+ it ( 'should handle errors for PUT request' , async ( ) => {
194+ client . put . mockRejectedValue ( new Error ( 'Mocked API Error' ) ) ;
195+ await expect ( sendRequest ( recommendationsEvent , 'put' ) ) . rejects . toThrowError (
196+ 'Mocked API Error' ,
197+ ) ;
198+ expect ( client . put ) . toHaveBeenCalledWith (
199+ RECCOMMENDATION_EVENT_URL ,
200+ recommendationsEvent . getDataObject ( ) ,
201+ ) ;
202+ } ) ;
61203
62- const result = await sendRequest ( flagFeedbackEvent ) ;
204+ it ( 'should handle errors for PATCH request' , async ( ) => {
205+ client . patch . mockRejectedValue ( new Error ( 'Mocked API Error' ) ) ;
206+ await expect ( sendRequest ( recommendationsEvent , 'patch' ) ) . rejects . toThrowError (
207+ 'Mocked API Error' ,
208+ ) ;
209+ expect ( client . patch ) . toHaveBeenCalledWith (
210+ RECCOMMENDATION_EVENT_URL ,
211+ recommendationsEvent . getDataObject ( ) ,
212+ ) ;
213+ } ) ;
63214
64- expect ( result ) . toEqual ( 'Mocked API Response' ) ;
65- expect ( client . post ) . toHaveBeenCalledWith (
66- FLAG_FEEDBACK_EVENT_URL ,
67- flagFeedbackEvent . getDataObject ( ) ,
68- ) ;
215+ it ( 'should throw error for unsupported DELETE method' , async ( ) => {
216+ await expect ( sendRequest ( recommendationsEvent , 'delete' ) ) . rejects . toThrowError (
217+ 'Unsupported HTTP method: delete' ,
218+ ) ;
219+ } ) ;
220+
221+ it ( 'should throw error for unsupported GET method' , async ( ) => {
222+ await expect ( sendRequest ( recommendationsEvent , 'get' ) ) . rejects . toThrowError (
223+ 'Unsupported HTTP method: get' ,
224+ ) ;
225+ } ) ;
226+ } ) ;
69227 } ) ;
70228
71- it . skip ( 'should handle errors when sending a request using sendRequest function' , async ( ) => {
72- client . post . mockRejectedValue ( new Error ( 'Mocked API Error' ) ) ;
73- await expect ( sendRequest ( flagFeedbackEvent ) ) . rejects . toThrowError ( 'Mocked API Error' ) ;
74- expect ( client . post ) . toHaveBeenCalledWith (
75- FLAG_FEEDBACK_EVENT_URL ,
76- flagFeedbackEvent . getDataObject ( ) ,
77- ) ;
229+ describe ( 'RecommendationsInteractionEvent Tests' , ( ) => {
230+ it ( 'should generate data object without functions' , ( ) => {
231+ const dataObject = recommendationsInteractionEvent . getDataObject ( ) ;
232+ expect ( dataObject . id ) . toEqual ( 'mocked-uuid' ) ;
233+ expect ( dataObject . context ) . toEqual ( { test_key : 'test_value' } ) ;
234+ expect ( dataObject . contentnode_id ) . toEqual ( 'mocked-uuid' ) ;
235+ expect ( dataObject . content_id ) . toEqual ( 'mocked-uuid' ) ;
236+ expect ( dataObject . feedback_type ) . toEqual ( FeedbackTypeOptions . ignored ) ;
237+ expect ( dataObject . feedback_reason ) . toEqual ( '----' ) ;
238+ expect ( dataObject . recommendation_event_id ) . toEqual ( 'mocked-uuid' ) ;
239+ expect ( dataObject . getDataObject ) . toBeUndefined ( ) ;
240+ expect ( dataObject . URL ) . toBeUndefined ( ) ;
241+ } ) ;
242+
243+ it ( 'should throw an error when URL is not defined' , ( ) => {
244+ recommendationsInteractionEvent . URL = undefined ;
245+ expect ( ( ) => recommendationsInteractionEvent . getUrl ( ) ) . toThrowError (
246+ 'URL is not defined for the FeedBack Object.' ,
247+ ) ;
248+ } ) ;
249+
250+ it ( 'should return the correct URL when URL is defined' , ( ) => {
251+ const result = recommendationsInteractionEvent . getUrl ( ) ;
252+ expect ( result ) . toEqual ( RECCOMMENDATION_INTERACTION_EVENT_URL ) ;
253+ } ) ;
254+
255+ describe ( 'HTTP Methods' , ( ) => {
256+ it ( 'should send POST request successfully' , async ( ) => {
257+ client . post . mockResolvedValue ( Promise . resolve ( { data : 'Mocked API Response' } ) ) ;
258+ const result = await sendRequest ( recommendationsInteractionEvent , 'post' ) ;
259+ expect ( result ) . toEqual ( 'Mocked API Response' ) ;
260+ expect ( client . post ) . toHaveBeenCalledWith (
261+ RECCOMMENDATION_INTERACTION_EVENT_URL ,
262+ recommendationsInteractionEvent . getDataObject ( ) ,
263+ ) ;
264+ } ) ;
265+
266+ it ( 'should send PUT request successfully' , async ( ) => {
267+ client . put . mockResolvedValue ( Promise . resolve ( { data : 'Mocked API Response' } ) ) ;
268+ const result = await sendRequest ( recommendationsInteractionEvent , 'put' ) ;
269+ expect ( result ) . toEqual ( 'Mocked API Response' ) ;
270+ expect ( client . put ) . toHaveBeenCalledWith (
271+ RECCOMMENDATION_INTERACTION_EVENT_URL ,
272+ recommendationsInteractionEvent . getDataObject ( ) ,
273+ ) ;
274+ } ) ;
275+
276+ it ( 'should send PATCH request successfully' , async ( ) => {
277+ client . patch . mockResolvedValue ( Promise . resolve ( { data : 'Mocked API Response' } ) ) ;
278+ const result = await sendRequest ( recommendationsInteractionEvent , 'patch' ) ;
279+ expect ( result ) . toEqual ( 'Mocked API Response' ) ;
280+ expect ( client . patch ) . toHaveBeenCalledWith (
281+ RECCOMMENDATION_INTERACTION_EVENT_URL ,
282+ recommendationsInteractionEvent . getDataObject ( ) ,
283+ ) ;
284+ } ) ;
285+
286+ it ( 'should handle errors for POST request' , async ( ) => {
287+ client . post . mockRejectedValue ( new Error ( 'Mocked API Error' ) ) ;
288+ await expect ( sendRequest ( recommendationsInteractionEvent , 'post' ) ) . rejects . toThrowError (
289+ 'Mocked API Error' ,
290+ ) ;
291+ expect ( client . post ) . toHaveBeenCalledWith (
292+ RECCOMMENDATION_INTERACTION_EVENT_URL ,
293+ recommendationsInteractionEvent . getDataObject ( ) ,
294+ ) ;
295+ } ) ;
296+
297+ it ( 'should handle errors for PUT request' , async ( ) => {
298+ client . put . mockRejectedValue ( new Error ( 'Mocked API Error' ) ) ;
299+ await expect ( sendRequest ( recommendationsInteractionEvent , 'put' ) ) . rejects . toThrowError (
300+ 'Mocked API Error' ,
301+ ) ;
302+ expect ( client . put ) . toHaveBeenCalledWith (
303+ RECCOMMENDATION_INTERACTION_EVENT_URL ,
304+ recommendationsInteractionEvent . getDataObject ( ) ,
305+ ) ;
306+ } ) ;
307+
308+ it ( 'should handle errors for PATCH request' , async ( ) => {
309+ client . patch . mockRejectedValue ( new Error ( 'Mocked API Error' ) ) ;
310+ await expect ( sendRequest ( recommendationsInteractionEvent , 'patch' ) ) . rejects . toThrowError (
311+ 'Mocked API Error' ,
312+ ) ;
313+ expect ( client . patch ) . toHaveBeenCalledWith (
314+ RECCOMMENDATION_INTERACTION_EVENT_URL ,
315+ recommendationsInteractionEvent . getDataObject ( ) ,
316+ ) ;
317+ } ) ;
318+
319+ it ( 'should throw error for unsupported DELETE method' , async ( ) => {
320+ await expect ( sendRequest ( recommendationsInteractionEvent , 'delete' ) ) . rejects . toThrowError (
321+ 'Unsupported HTTP method: delete' ,
322+ ) ;
323+ } ) ;
324+
325+ it ( 'should throw error for unsupported GET method' , async ( ) => {
326+ await expect ( sendRequest ( recommendationsInteractionEvent , 'get' ) ) . rejects . toThrowError (
327+ 'Unsupported HTTP method: get' ,
328+ ) ;
329+ } ) ;
330+ } ) ;
78331 } ) ;
79332} ) ;
0 commit comments