|
| 1 | +import { User } from '../types'; |
| 2 | +import {http, HttpResponse} from "msw"; |
| 3 | +import {setupServer} from "msw/node"; |
| 4 | + |
| 5 | +// Define request handlers and response resolvers for random user API. |
| 6 | +// By default, we always return the happy path response. |
| 7 | +const handlers = [ |
| 8 | + http.get('https://randomuser.me/api/*', () => { |
| 9 | + return HttpResponse.json(DATA); |
| 10 | + }), |
| 11 | +]; |
| 12 | + |
| 13 | +export const server = setupServer(...handlers); |
| 14 | + |
| 15 | +export const mockServerFailureForGetAllContacts = () => { |
| 16 | + server.use( |
| 17 | + http.get('https://randomuser.me/api/', ({ request }) => { |
| 18 | + // Construct a URL instance out of the intercepted request. |
| 19 | + const url = new URL(request.url); |
| 20 | + // Read the "results" URL query parameter using the "URLSearchParams" API. |
| 21 | + const resultsLength = url.searchParams.get('results'); |
| 22 | + // Simulate a server error for the get all contacts request. |
| 23 | + // We check if the "results" query parameter is set to "25" |
| 24 | + // to know it's the correct request to mock, in our case get all contacts. |
| 25 | + if (resultsLength === '25') { |
| 26 | + return new HttpResponse(null, { status: 500 }); |
| 27 | + } |
| 28 | + |
| 29 | + return HttpResponse.json(DATA); |
| 30 | + }), |
| 31 | + ); |
| 32 | +}; |
| 33 | + |
| 34 | +export const mockServerFailureForGetAllFavorites = () => { |
| 35 | + server.use( |
| 36 | + http.get('https://randomuser.me/api/', ({ request }) => { |
| 37 | + // Construct a URL instance out of the intercepted request. |
| 38 | + const url = new URL(request.url); |
| 39 | + // Read the "results" URL query parameter using the "URLSearchParams" API. |
| 40 | + const resultsLength = url.searchParams.get('results'); |
| 41 | + // Simulate a server error for the get all favorites request. |
| 42 | + // We check if the "results" query parameter is set to "10" |
| 43 | + // to know it's the correct request to mock, in our case get all favorites. |
| 44 | + if (resultsLength === '10') { |
| 45 | + return new HttpResponse(null, { status: 500 }); |
| 46 | + } |
| 47 | + |
| 48 | + return HttpResponse.json(DATA); |
| 49 | + }), |
| 50 | + ); |
| 51 | +}; |
| 52 | +export const DATA: { results: User[] } = { |
| 53 | + results: [ |
| 54 | + { |
| 55 | + name: { |
| 56 | + title: 'Mrs', |
| 57 | + first: 'Ida', |
| 58 | + last: 'Kristensen', |
| 59 | + }, |
| 60 | + email: 'ida.kristensen@example.com', |
| 61 | + id: { |
| 62 | + name: 'CPR', |
| 63 | + value: '250562-5730', |
| 64 | + }, |
| 65 | + picture: { |
| 66 | + large: 'https://randomuser.me/api/portraits/women/26.jpg', |
| 67 | + medium: 'https://randomuser.me/api/portraits/med/women/26.jpg', |
| 68 | + thumbnail: 'https://randomuser.me/api/portraits/thumb/women/26.jpg', |
| 69 | + }, |
| 70 | + cell: '123-4567-890', |
| 71 | + }, |
| 72 | + { |
| 73 | + name: { |
| 74 | + title: 'Mr', |
| 75 | + first: 'Elijah', |
| 76 | + last: 'Ellis', |
| 77 | + }, |
| 78 | + email: 'elijah.ellis@example.com', |
| 79 | + id: { |
| 80 | + name: 'TFN', |
| 81 | + value: '138117486', |
| 82 | + }, |
| 83 | + picture: { |
| 84 | + large: 'https://randomuser.me/api/portraits/men/53.jpg', |
| 85 | + medium: 'https://randomuser.me/api/portraits/med/men/53.jpg', |
| 86 | + thumbnail: 'https://randomuser.me/api/portraits/thumb/men/53.jpg', |
| 87 | + }, |
| 88 | + cell: '123-4567-890', |
| 89 | + }, |
| 90 | + { |
| 91 | + name: { |
| 92 | + title: 'Mr', |
| 93 | + first: 'Miro', |
| 94 | + last: 'Halko', |
| 95 | + }, |
| 96 | + email: 'miro.halko@example.com', |
| 97 | + id: { |
| 98 | + name: 'HETU', |
| 99 | + value: 'NaNNA945undefined', |
| 100 | + }, |
| 101 | + picture: { |
| 102 | + large: 'https://randomuser.me/api/portraits/men/17.jpg', |
| 103 | + medium: 'https://randomuser.me/api/portraits/med/men/17.jpg', |
| 104 | + thumbnail: 'https://randomuser.me/api/portraits/thumb/men/17.jpg', |
| 105 | + }, |
| 106 | + cell: '123-4567-890', |
| 107 | + }, |
| 108 | + ], |
| 109 | +}; |
0 commit comments