|
| 1 | +/*You are given the following API - |
| 2 | + |
| 3 | +GET /api/comments |
| 4 | + |
| 5 | +This will return a list of all comments. A comment object contains the following information |
| 6 | +userId - ID of the user who commented |
| 7 | +data - comment data |
| 8 | + |
| 9 | + |
| 10 | +Given a userId, return an Array of comment data of all the comments by the given user. |
| 11 | +Apart from .json(), don’t use any other methods on the response object returned from fetch() call. This can cause your tests to fail. |
| 12 | + |
| 13 | +Input - |
| 14 | +userId - the user id whose comment is to be returned. |
| 15 | +Output - |
| 16 | +A list of comments by the given user id |
| 17 | + |
| 18 | +Sample input 1 - |
| 19 | + |
| 20 | +userId = 1 |
| 21 | + |
| 22 | +Sample API response |
| 23 | +comments = [ |
| 24 | + |
| 25 | +{ |
| 26 | + 'userId': '1', |
| 27 | + "data": 'This looks slick!' |
| 28 | +}, |
| 29 | + |
| 30 | +{ |
| 31 | + 'userId': '2', |
| 32 | + "data": 'I think this can be improved.' |
| 33 | +}, |
| 34 | + |
| 35 | +{ |
| 36 | + 'userId': '1', |
| 37 | + "data": 'What kind of improvement?' |
| 38 | +}] |
| 39 | + |
| 40 | +Sample output 1 - |
| 41 | +['This looks slick!', 'What kind of improvement?'] |
| 42 | +*/ |
| 43 | + |
| 44 | +// TODO - Implement getCommentsByUserId() function |
| 45 | + |
| 46 | +async function getCommentsByUserId(userId) { |
| 47 | + |
| 48 | + return fetch("/api/comments") |
| 49 | + .then(response => response.json()) |
| 50 | + .then(comments => { |
| 51 | + |
| 52 | + let filteredComments = |
| 53 | + comments.filter(comments => comments.userId === userId) |
| 54 | + |
| 55 | + let result = []; |
| 56 | + for(let i=0; i< filteredComments.length; i++){ |
| 57 | + result[i] = filteredComments[i].data; |
| 58 | + } |
| 59 | + return result; |
| 60 | + }) |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +// ----------- Don't modify ----------- |
| 65 | +const mockFetch = (url, responseData) => { |
| 66 | + const mockJsonPromise = Promise.resolve(responseData); |
| 67 | + const mockFetchPromise = (callUrl) => { |
| 68 | + if (url === callUrl) { |
| 69 | + return Promise.resolve({ |
| 70 | + json: () => mockJsonPromise |
| 71 | + }); |
| 72 | + } else { |
| 73 | + return Promise.reject('404: No such url') |
| 74 | + } |
| 75 | + } |
| 76 | + global.fetch = mockFetchPromise; |
| 77 | +} |
| 78 | + |
| 79 | +const successResponse = [ |
| 80 | + { |
| 81 | + 'userId': '1', |
| 82 | + "data": 'This looks slick!' |
| 83 | + }, |
| 84 | + { |
| 85 | + 'userId': '2', |
| 86 | + "data": 'I think this can be improved.' |
| 87 | + }, |
| 88 | + { |
| 89 | + 'userId': '1', |
| 90 | + "data": 'What kind of improvement?' |
| 91 | + }]; |
| 92 | +mockFetch('/api/comments', successResponse); |
| 93 | + |
| 94 | +module.exports = getCommentsByUserId; |
| 95 | +// ----------- Don't modify ----------- |
| 96 | + |
| 97 | +getCommentsByUserId("1").then((res) => { |
| 98 | + console.log(res); |
| 99 | +}); |
0 commit comments