-
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.
tech-story: [M3-9017] - Add MSW crud domains (#11428)
* add MSW crud domains * Added changeset: Add MSW crud domains * feedback @jaalah-akamai * feebback @pmakode-akamai
- Loading branch information
1 parent
bab026d
commit caca6dd
Showing
13 changed files
with
354 additions
and
13 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/manager/.changeset/pr-11428-tech-stories-1734382681885.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 MSW crud domains ([#11428](https://github.com/linode/manager/pull/11428)) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
cloneDomain, | ||
createDomain, | ||
deleteDomains, | ||
getDomains, | ||
importDomain, | ||
updateDomain, | ||
} from 'src/mocks/presets/crud/handlers/domains'; | ||
|
||
import type { MockPresetCrud } from 'src/mocks/types'; | ||
|
||
export const domainCrudPreset: MockPresetCrud = { | ||
group: { id: 'Domains' }, | ||
handlers: [ | ||
createDomain, | ||
deleteDomains, | ||
updateDomain, | ||
getDomains, | ||
cloneDomain, | ||
importDomain, | ||
], | ||
id: 'domains:crud', | ||
label: 'Domains CRUD', | ||
}; |
252 changes: 252 additions & 0 deletions
252
packages/manager/src/mocks/presets/crud/handlers/domains.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,252 @@ | ||
import { DateTime } from 'luxon'; | ||
import { http } from 'msw'; | ||
|
||
import { domainFactory, domainRecordFactory } 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 { Domain, DomainRecord } 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 getDomains = () => [ | ||
http.get( | ||
'*/v4/domains/:id/records', | ||
async ({ | ||
request, | ||
}): Promise< | ||
StrictResponse<APIErrorResponse | APIPaginatedResponse<DomainRecord>> | ||
> => { | ||
const domainRecords = domainRecordFactory.buildList(3); | ||
|
||
return makePaginatedResponse({ | ||
data: domainRecords, | ||
request, | ||
}); | ||
} | ||
), | ||
|
||
http.get( | ||
'*/v4/domains', | ||
async ({ | ||
request, | ||
}): Promise< | ||
StrictResponse<APIErrorResponse | APIPaginatedResponse<Domain>> | ||
> => { | ||
const domains = await mswDB.getAll('domains'); | ||
|
||
if (!domains) { | ||
return makeNotFoundResponse(); | ||
} | ||
|
||
return makePaginatedResponse({ | ||
data: domains, | ||
request, | ||
}); | ||
} | ||
), | ||
|
||
http.get( | ||
'*/v4/domains/:id', | ||
async ({ params }): Promise<StrictResponse<APIErrorResponse | Domain>> => { | ||
const id = Number(params.id); | ||
const domain = await mswDB.get('domains', id); | ||
|
||
if (!domain) { | ||
return makeNotFoundResponse(); | ||
} | ||
|
||
return makeResponse(domain); | ||
} | ||
), | ||
]; | ||
|
||
export const createDomain = (mockState: MockState) => [ | ||
http.post( | ||
'*/v4/domains', | ||
async ({ request }): Promise<StrictResponse<APIErrorResponse | Domain>> => { | ||
const payload = await request.clone().json(); | ||
|
||
const domain = domainFactory.build({ | ||
...payload, | ||
created: DateTime.now().toISO(), | ||
last_updated: DateTime.now().toISO(), | ||
updated: DateTime.now().toISO(), | ||
}); | ||
|
||
await mswDB.add('domains', domain, mockState); | ||
|
||
queueEvents({ | ||
event: { | ||
action: 'domain_create', | ||
entity: { | ||
id: domain.id, | ||
label: domain.domain, | ||
type: 'domain', | ||
url: `/v4/domains/${domain.id}`, | ||
}, | ||
}, | ||
mockState, | ||
sequence: [{ status: 'notification' }], | ||
}); | ||
|
||
return makeResponse(domain); | ||
} | ||
), | ||
]; | ||
|
||
export const updateDomain = (mockState: MockState) => [ | ||
http.put( | ||
'*/v4/domains/:id', | ||
async ({ | ||
params, | ||
request, | ||
}): Promise<StrictResponse<APIErrorResponse | Domain>> => { | ||
const id = Number(params.id); | ||
const domain = await mswDB.get('domains', id); | ||
|
||
if (!domain) { | ||
return makeNotFoundResponse(); | ||
} | ||
|
||
const payload = { | ||
...(await request.clone().json()), | ||
last_updated: DateTime.now().toISO(), | ||
updated: DateTime.now().toISO(), | ||
}; | ||
const updatedDomain = { ...domain, ...payload }; | ||
|
||
await mswDB.update('domains', id, updatedDomain, mockState); | ||
|
||
queueEvents({ | ||
event: { | ||
action: 'domain_update', | ||
entity: { | ||
id: domain.id, | ||
label: domain.domain, | ||
type: 'domain', | ||
url: `/v4/domains/${domain.id}`, | ||
}, | ||
}, | ||
mockState, | ||
sequence: [{ status: 'notification' }], | ||
}); | ||
|
||
return makeResponse(updatedDomain); | ||
} | ||
), | ||
]; | ||
|
||
export const cloneDomain = (mockState: MockState) => [ | ||
http.post( | ||
'*/v4/domains/:id/clone', | ||
async ({ params }): Promise<StrictResponse<APIErrorResponse | Domain>> => { | ||
const id = Number(params.id); | ||
const domain = await mswDB.get('domains', id); | ||
|
||
if (!domain) { | ||
return makeNotFoundResponse(); | ||
} | ||
|
||
const clonedDomain = { | ||
...domain, | ||
created: DateTime.now().toISO(), | ||
last_updated: DateTime.now().toISO(), | ||
updated: DateTime.now().toISO(), | ||
}; | ||
|
||
await mswDB.add('domains', clonedDomain, mockState); | ||
|
||
queueEvents({ | ||
event: { | ||
action: 'domain_create', | ||
entity: { | ||
id: domain.id, | ||
label: domain.domain, | ||
type: 'domain', | ||
url: `/v4/domains/${domain.id}`, | ||
}, | ||
}, | ||
mockState, | ||
sequence: [{ status: 'notification' }], | ||
}); | ||
|
||
return makeResponse(domain); | ||
} | ||
), | ||
]; | ||
|
||
export const importDomain = (mockState: MockState) => [ | ||
http.post( | ||
'*/v4/domains/import', | ||
async ({ request }): Promise<StrictResponse<APIErrorResponse | Domain>> => { | ||
const payload = await request.clone().json(); | ||
|
||
const domain = domainFactory.build({ | ||
...payload, | ||
created: DateTime.now().toISO(), | ||
last_updated: DateTime.now().toISO(), | ||
updated: DateTime.now().toISO(), | ||
}); | ||
|
||
await mswDB.add('domains', domain, mockState); | ||
|
||
queueEvents({ | ||
event: { | ||
action: 'domain_create', | ||
entity: { | ||
id: domain.id, | ||
label: domain.domain, | ||
type: 'domain', | ||
url: `/v4/domains/${domain.id}`, | ||
}, | ||
}, | ||
mockState, | ||
sequence: [{ status: 'notification' }], | ||
}); | ||
|
||
return makeResponse(domain); | ||
} | ||
), | ||
]; | ||
|
||
export const deleteDomains = (mockState: MockState) => [ | ||
http.delete( | ||
'*/v4/domains/:id', | ||
async ({ params }): Promise<StrictResponse<{} | APIErrorResponse>> => { | ||
const id = Number(params.id); | ||
const domain = await mswDB.get('domains', id); | ||
|
||
if (!domain) { | ||
return makeNotFoundResponse(); | ||
} | ||
|
||
await mswDB.delete('domains', id, mockState); | ||
|
||
queueEvents({ | ||
event: { | ||
action: 'domain_delete', | ||
entity: { | ||
id: domain.id, | ||
label: domain.domain, | ||
type: 'domain', | ||
url: `/v4/domains/${domain.id}`, | ||
}, | ||
}, | ||
mockState, | ||
sequence: [{ status: 'notification' }], | ||
}); | ||
|
||
return makeResponse({}); | ||
} | ||
), | ||
]; |
Oops, something went wrong.