-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change: [M3-8583] - Add support ticket mocks to MSW 2.0 (#10937)
* Add getSupportTickets and createSupportTicket mocks * Mock getSupportTicket and getSupportTicketReplies requests * Fix getSupportTicket mock req, add closeSupportTicket req * Mock createSupportTicketReply * Clean up createSupportTicket req, fix getSupportTicketReplies req * Add events * Uncomment seeder * Clean up in create request * Added changeset: Add support ticket mocks to MSW 2.0 * Address feedback: add new entities to mergedContext in load function * Address feedback: mock support ticket reply more accurately
- Loading branch information
Showing
9 changed files
with
264 additions
and
2 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/manager/.changeset/pr-10937-tech-stories-1728079160083.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/manager": Tech Stories | ||
--- | ||
|
||
Add support ticket mocks to MSW 2.0 ([#10937](https://github.com/linode/manager/pull/10937)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
packages/manager/src/mocks/presets/crud/handlers/supportTickets.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import { DateTime } from 'luxon'; | ||
import { http } from 'msw'; | ||
|
||
import { supportReplyFactory, supportTicketFactory } from 'src/factories'; | ||
import { mswDB } from 'src/mocks/indexedDB'; | ||
import { queueEvents } from 'src/mocks/utilities/events'; | ||
import { | ||
makeNotFoundResponse, | ||
makePaginatedResponse, | ||
makeResponse, | ||
} from 'src/mocks/utilities/response'; | ||
|
||
import type { SupportReply, SupportTicket } from '@linode/api-v4'; | ||
import type { StrictResponse } from 'msw'; | ||
import type { MockState } from 'src/mocks/types'; | ||
import type { | ||
APIErrorResponse, | ||
APIPaginatedResponse, | ||
} from 'src/mocks/utilities/response'; | ||
|
||
export const createSupportTicket = (mockState: MockState) => [ | ||
http.post( | ||
'*/support/tickets', | ||
async ({ | ||
request, | ||
}): Promise<StrictResponse<APIErrorResponse | SupportTicket>> => { | ||
const payload = await request.clone().json(); | ||
|
||
const supportTicket = supportTicketFactory.build({ | ||
closable: true, | ||
description: payload['description'], | ||
entity: { | ||
id: 1, | ||
label: 'mock-linode-1', | ||
type: 'linode', | ||
url: '/v4/linode/instances/1', | ||
}, | ||
opened: DateTime.now().toISO(), | ||
severity: 1, | ||
status: 'open', | ||
summary: payload['summary'], | ||
updated: DateTime.now().toISO(), | ||
}); | ||
|
||
await mswDB.add('supportTickets', supportTicket, mockState); | ||
|
||
queueEvents({ | ||
event: { | ||
action: 'ticket_create', | ||
entity: { | ||
id: supportTicket.id, | ||
label: supportTicket.summary, | ||
type: 'support_ticket', | ||
url: `/v4/support/tickets/${supportTicket.id}`, | ||
}, | ||
}, | ||
mockState, | ||
sequence: [{ status: 'notification' }], | ||
}); | ||
|
||
return makeResponse(supportTicket); | ||
} | ||
), | ||
]; | ||
|
||
export const getSupportTickets = () => [ | ||
http.get( | ||
'*/support/tickets', | ||
async ({ | ||
request, | ||
}): Promise< | ||
StrictResponse<APIErrorResponse | APIPaginatedResponse<SupportTicket>> | ||
> => { | ||
const supportTickets = await mswDB.getAll('supportTickets'); | ||
|
||
if (!supportTickets) { | ||
return makeNotFoundResponse(); | ||
} | ||
return makePaginatedResponse({ | ||
data: supportTickets, | ||
request, | ||
}); | ||
} | ||
), | ||
http.get( | ||
'*/support/tickets/:ticketId', | ||
async ({ | ||
params, | ||
}): Promise<StrictResponse<APIErrorResponse | SupportTicket>> => { | ||
const id = Number(params.ticketId); | ||
const supportTicket = await mswDB.get('supportTickets', id); | ||
|
||
if (!supportTicket) { | ||
return makeNotFoundResponse(); | ||
} | ||
return makeResponse(supportTicket); | ||
} | ||
), | ||
]; | ||
|
||
export const closeSupportTicket = (mockState: MockState) => [ | ||
http.post('*/support/tickets/:ticketId/close', async ({ params }) => { | ||
const id = Number(params.ticketId); | ||
const supportTicket = await mswDB.get('supportTickets', id); | ||
|
||
if (!supportTicket) { | ||
return makeNotFoundResponse(); | ||
} | ||
|
||
mswDB.update( | ||
'supportTickets', | ||
id, | ||
{ ...supportTicket, closed: DateTime.now().toISO(), status: 'closed' }, | ||
mockState | ||
); | ||
|
||
return makeResponse({}); | ||
}), | ||
]; | ||
|
||
export const getSupportTicketReplies = () => [ | ||
http.get( | ||
'*/support/tickets/:ticketId/replies', | ||
async ({ | ||
params, | ||
request, | ||
}): Promise< | ||
StrictResponse<APIErrorResponse | APIPaginatedResponse<SupportReply>> | ||
> => { | ||
const id = Number(params.ticketId); | ||
const supportReplies = await mswDB.get('supportReplies', id); | ||
|
||
if (!supportReplies) { | ||
return makePaginatedResponse({ | ||
data: [], | ||
request, | ||
}); | ||
} | ||
return makePaginatedResponse({ | ||
data: [supportReplies], | ||
request, | ||
}); | ||
} | ||
), | ||
]; | ||
|
||
export const createSupportTicketReply = (mockState: MockState) => [ | ||
http.post( | ||
'*/support/tickets/:ticketId/replies', | ||
async ({ | ||
params, | ||
request, | ||
}): Promise<StrictResponse<APIErrorResponse | SupportReply>> => { | ||
const payload = await request.clone().json(); | ||
const id = Number(params.ticketId); | ||
const supportTicket = await mswDB.get('supportTickets', id); | ||
|
||
const supportTicketReply = supportReplyFactory.build({ | ||
created_by: 'test-account', | ||
description: payload['description'], | ||
friendly_name: 'test-account', | ||
from_linode: false, | ||
}); | ||
|
||
await mswDB.add('supportReplies', supportTicketReply, mockState); | ||
|
||
queueEvents({ | ||
event: { | ||
action: 'ticket_update', | ||
entity: { | ||
id: supportTicket?.id ?? -1, | ||
label: supportTicket?.summary ?? null, | ||
type: 'support_ticket', | ||
url: `/v4/support/tickets/${supportTicket?.id}`, | ||
}, | ||
}, | ||
mockState, | ||
sequence: [{ status: 'notification' }], | ||
}); | ||
|
||
return makeResponse(supportTicketReply); | ||
} | ||
), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { linodesSeeder } from './linodes'; | ||
import { placementGroupSeeder } from './placementGroups'; | ||
import { supportTicketsSeeder } from './supportTickets'; | ||
import { volumesSeeder } from './volumes'; | ||
|
||
export const dbSeeders = [linodesSeeder, placementGroupSeeder, volumesSeeder]; | ||
export const dbSeeders = [ | ||
linodesSeeder, | ||
placementGroupSeeder, | ||
supportTicketsSeeder, | ||
volumesSeeder, | ||
]; |
28 changes: 28 additions & 0 deletions
28
packages/manager/src/mocks/presets/crud/seeds/supportTickets.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { getSeedsCountMap } from 'src/dev-tools/utils'; | ||
import { supportTicketFactory } from 'src/factories'; | ||
import { mswDB } from 'src/mocks/indexedDB'; | ||
|
||
import type { MockSeeder, MockState } from 'src/mocks/types'; | ||
|
||
export const supportTicketsSeeder: MockSeeder = { | ||
canUpdateCount: true, | ||
desc: 'Support Tickets Seeds', | ||
group: { id: 'Support Tickets' }, | ||
id: 'support-tickets:crud', | ||
label: 'Support Tickets', | ||
|
||
seeder: async (mockState: MockState) => { | ||
const seedsCountMap = getSeedsCountMap(); | ||
const count = seedsCountMap[supportTicketsSeeder.id] ?? 0; | ||
const supportTickets = supportTicketFactory.buildList(count); | ||
|
||
const updatedMockState = { | ||
...mockState, | ||
supportTickets: mockState.supportTickets.concat(supportTickets), | ||
}; | ||
|
||
await mswDB.saveStore(updatedMockState, 'seedState'); | ||
|
||
return updatedMockState; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
closeSupportTicket, | ||
createSupportTicket, | ||
createSupportTicketReply, | ||
getSupportTicketReplies, | ||
getSupportTickets, | ||
} from 'src/mocks/presets/crud/handlers/supportTickets'; | ||
|
||
import type { MockPresetCrud } from 'src/mocks/types'; | ||
|
||
export const supportTicketCrudPreset: MockPresetCrud = { | ||
group: { id: 'Support Tickets' }, | ||
handlers: [ | ||
createSupportTicket, | ||
closeSupportTicket, | ||
getSupportTickets, | ||
getSupportTicketReplies, | ||
createSupportTicketReply, | ||
], | ||
id: 'support-tickets:crud', | ||
label: 'Support Tickets CRUD', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters