-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83f6f8e
commit 6d3b54f
Showing
19 changed files
with
471 additions
and
409 deletions.
There are no files selected for viewing
Binary file not shown.
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
256 changes: 139 additions & 117 deletions
256
v3/packages/internarbeidsflate-decorator-v3/src/__mocks__/mock-handlers.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 |
---|---|---|
@@ -1,134 +1,156 @@ | ||
import { http, HttpHandler, HttpResponse } from 'msw' | ||
import { http, HttpHandler, HttpResponse } from 'msw'; | ||
import WS from 'vitest-websocket-mock'; | ||
import { FailureConfig } from './mock-error-config'; | ||
import { Veileder } from '../types/Veileder'; | ||
export const urlPrefix = 'http://localhost:8080'; | ||
export const urlPrefix = 'http://localhost:4000'; | ||
|
||
export const mockMe: Veileder = { | ||
ident: 'Z999999', | ||
navn: 'Fornavn Ettersen', | ||
fornavn: 'Fornavn', | ||
etternavn: 'Ettersen', | ||
enheter: [ | ||
{ enhetId: '0219', navn: 'NAV Bærum' }, | ||
{ enhetId: '0118', navn: 'NAV Aremark' }, | ||
{ enhetId: '0604', navn: 'NAV Kongsberg' }, | ||
{ enhetId: '0602', navn: 'NAV Drammer' } | ||
] | ||
ident: 'Z999999', | ||
navn: 'Fornavn Ettersen', | ||
fornavn: 'Fornavn', | ||
etternavn: 'Ettersen', | ||
enheter: [ | ||
{ enhetId: '0219', navn: 'NAV Bærum' }, | ||
{ enhetId: '0118', navn: 'NAV Aremark' }, | ||
{ enhetId: '0604', navn: 'NAV Kongsberg' }, | ||
{ enhetId: '0602', navn: 'NAV Drammer' }, | ||
], | ||
}; | ||
|
||
|
||
const getUrl = (path: string) => `${urlPrefix}/modiacontextholder/api/context${path}` | ||
const getUrl = (path: string) => | ||
`${urlPrefix}/modiacontextholder/api/context${path}`; | ||
|
||
const getErrorResponse = (status = 500) => { | ||
return new HttpResponse(null, { status }) | ||
} | ||
return new HttpResponse(null, { status }); | ||
}; | ||
|
||
const getSuccessResponse = ({ status, body }: { status?: number | null, body?: unknown } = { status: 200, body: null }) => { | ||
return new HttpResponse(JSON.stringify(body ?? {}), { status: status ?? 200 }) | ||
} | ||
const getSuccessResponse = ( | ||
{ status, body }: { status?: number | null; body?: unknown } = { | ||
status: 200, | ||
body: null, | ||
}, | ||
) => { | ||
return new HttpResponse(JSON.stringify(body ?? {}), { | ||
status: status ?? 200, | ||
}); | ||
}; | ||
|
||
type Context = { aktivEnhet: string | null; aktivBruker: string | null }; | ||
const context: Context = { aktivEnhet: '0118', aktivBruker: '10108000398' }; | ||
let context: Context = { aktivEnhet: '0118', aktivBruker: '10108000398' }; | ||
|
||
export const updateMockContext = (key: keyof Context, value: string | null) => context[key] = value | ||
export const updateMockContext = (newContext: Partial<Context>) => { | ||
console.log('updating mock', newContext) | ||
context = { | ||
...context, | ||
...newContext, | ||
} | ||
}; | ||
|
||
const controlSignal = (data: 'NY_AKTIV_ENHET' | 'NY_AKTIV_BRUKER') => { | ||
return JSON.stringify({ type: 'control', data }); | ||
} | ||
return JSON.stringify({ type: 'control', data }); | ||
}; | ||
|
||
export const getHandlers = (ws: WS, errorConfig: FailureConfig): HttpHandler[] => { | ||
ws.on('message', console.log) | ||
return [ | ||
http.post(getUrl(''), async ({ request }) => { | ||
const { eventType, verdi } = await request.json() as { eventType: 'NY_AKTIV_ENHET' | 'NY_AKTIV_BRUKER', verdi: string } | ||
if (eventType === 'NY_AKTIV_ENHET') { | ||
if (errorConfig.contextholder.updateEnhet) { | ||
return getErrorResponse() | ||
} | ||
context.aktivEnhet = verdi | ||
ws.send(controlSignal('NY_AKTIV_ENHET')) | ||
return getSuccessResponse() | ||
} | ||
else if (eventType === 'NY_AKTIV_BRUKER') { | ||
if (errorConfig.contextholder.updateBruker) { | ||
return getErrorResponse() | ||
} | ||
context.aktivBruker = verdi | ||
ws.send(controlSignal('NY_AKTIV_BRUKER')) | ||
return getSuccessResponse() | ||
} | ||
else { | ||
return getErrorResponse() | ||
} | ||
}), | ||
http.delete(getUrl('/aktivenhet'), () => { | ||
if (errorConfig.contextholder.deleteEnhet) { | ||
return getErrorResponse() | ||
} | ||
context.aktivEnhet = null | ||
ws.send(controlSignal('NY_AKTIV_ENHET')) | ||
return getSuccessResponse() | ||
}), | ||
http.delete(getUrl('/aktivbruker'), () => { | ||
if (errorConfig.contextholder.deleteBruker) { | ||
return getErrorResponse() | ||
} | ||
context.aktivBruker = null | ||
ws.send(controlSignal('NY_AKTIV_BRUKER')) | ||
return getSuccessResponse() | ||
}), | ||
http.get(getUrl('/aktivenhet'), () => { | ||
if (errorConfig.contextholder.getEnhet) { | ||
return getErrorResponse() | ||
} | ||
return getSuccessResponse({ body: { aktivEnhet: context.aktivEnhet } }) | ||
}), | ||
http.get(getUrl('/aktivbruker'), () => { | ||
if (errorConfig.contextholder.getBruker) { | ||
return getErrorResponse() | ||
} | ||
return getSuccessResponse({ body: { aktivBruker: context.aktivBruker } }) | ||
}), | ||
http.get(getUrl(''), () => { | ||
if (errorConfig.contextholder.get) { | ||
return getErrorResponse() | ||
} | ||
return getSuccessResponse({ body: { aktivEnhet: context.aktivEnhet, aktivBruker: context.aktivBruker } }) | ||
}), | ||
http.get(getUrl('/aktor/v2'), async ({ request }) => { | ||
const { fnr } = await request.json() as { fnr: string } | ||
const data = { | ||
fnr, | ||
aktorId: `000${fnr}000` | ||
}; | ||
if (errorConfig.aktorIdEndpoint) { | ||
return getErrorResponse() | ||
} | ||
return getSuccessResponse({ body: data }) | ||
}), | ||
http.get(getUrl('/decorator'), () => { | ||
if (errorConfig.meEndpoint) { | ||
return getErrorResponse() | ||
} | ||
return getSuccessResponse({ body: mockMe }) | ||
}), | ||
http.get(getUrl('/enhet/:enhetId'), ({ params }) => { | ||
const { enhetId } = params | ||
if (errorConfig.enhetEndpoint) { | ||
return getErrorResponse() | ||
} | ||
const enhet = mockMe.enheter.find((enhet) => enhet.enhetId === enhetId) | ||
if (!enhet) { | ||
return getErrorResponse(404) | ||
} | ||
return getSuccessResponse({ body: enhet }) | ||
}), | ||
http.post(getUrl('/bytt-bruker-nokkel'), async ({ request }) => { | ||
const { userKey } = await request.json() as { userKey: string } | ||
console.log(userKey) | ||
return getSuccessResponse({ body: `10108000398` }) | ||
}) | ||
|
||
] | ||
} | ||
export const getHandlers = ( | ||
ws: WS, | ||
errorConfig: FailureConfig, | ||
): HttpHandler[] => { | ||
ws.on('message', console.log); | ||
return [ | ||
http.post(getUrl(''), async ({ request }) => { | ||
const { eventType, verdi } = (await request.json()) as { | ||
eventType: 'NY_AKTIV_ENHET' | 'NY_AKTIV_BRUKER'; | ||
verdi: string; | ||
}; | ||
if (eventType === 'NY_AKTIV_ENHET') { | ||
if (errorConfig.contextholder.updateEnhet) { | ||
return getErrorResponse(); | ||
} | ||
context.aktivEnhet = verdi; | ||
ws.send(controlSignal('NY_AKTIV_ENHET')); | ||
return getSuccessResponse(); | ||
} else if (eventType === 'NY_AKTIV_BRUKER') { | ||
if (errorConfig.contextholder.updateBruker) { | ||
return getErrorResponse(); | ||
} | ||
context.aktivBruker = verdi; | ||
ws.send(controlSignal('NY_AKTIV_BRUKER')); | ||
return getSuccessResponse(); | ||
} else { | ||
return getErrorResponse(); | ||
} | ||
}), | ||
http.delete(getUrl('/aktivenhet'), () => { | ||
if (errorConfig.contextholder.deleteEnhet) { | ||
return getErrorResponse(); | ||
} | ||
context.aktivEnhet = null; | ||
ws.send(controlSignal('NY_AKTIV_ENHET')); | ||
return getSuccessResponse(); | ||
}), | ||
http.delete(getUrl('/aktivbruker'), () => { | ||
if (errorConfig.contextholder.deleteBruker) { | ||
return getErrorResponse(); | ||
} | ||
context.aktivBruker = null; | ||
ws.send(controlSignal('NY_AKTIV_BRUKER')); | ||
return getSuccessResponse(); | ||
}), | ||
http.get(getUrl('/v2/aktivenhet'), () => { | ||
if (errorConfig.contextholder.getEnhet) { | ||
return getErrorResponse(); | ||
} | ||
return getSuccessResponse({ body: { aktivEnhet: context.aktivEnhet } }); | ||
}), | ||
http.get(getUrl('/v2/aktivbruker'), () => { | ||
if (errorConfig.contextholder.getBruker) { | ||
return getErrorResponse(); | ||
} | ||
return getSuccessResponse({ body: { aktivBruker: context.aktivBruker } }); | ||
}), | ||
http.get(getUrl(''), () => { | ||
if (errorConfig.contextholder.get) { | ||
return getErrorResponse(); | ||
} | ||
return getSuccessResponse({ | ||
body: { | ||
aktivEnhet: context.aktivEnhet, | ||
aktivBruker: context.aktivBruker, | ||
}, | ||
}); | ||
}), | ||
http.get(getUrl('/aktor/v2'), async ({ request }) => { | ||
const { fnr } = (await request.json()) as { fnr: string }; | ||
const data = { | ||
fnr, | ||
aktorId: `000${fnr}000`, | ||
}; | ||
if (errorConfig.aktorIdEndpoint) { | ||
return getErrorResponse(); | ||
} | ||
return getSuccessResponse({ body: data }); | ||
}), | ||
http.get(getUrl('/decorator'), () => { | ||
if (errorConfig.meEndpoint) { | ||
return getErrorResponse(); | ||
} | ||
return getSuccessResponse({ body: mockMe }); | ||
}), | ||
http.get(getUrl('/enhet/:enhetId'), ({ params }) => { | ||
const { enhetId } = params; | ||
if (errorConfig.enhetEndpoint) { | ||
return getErrorResponse(); | ||
} | ||
const enhet = mockMe.enheter.find((enhet) => enhet.enhetId === enhetId); | ||
if (!enhet) { | ||
return getErrorResponse(404); | ||
} | ||
return getSuccessResponse({ body: enhet }); | ||
}), | ||
http.post(getUrl('/bytt-bruker-nokkel'), async ({ request }) => { | ||
const { userKey } = (await request.json()) as { userKey: string }; | ||
console.log(userKey); | ||
return getSuccessResponse({ body: `10108000398` }); | ||
}), | ||
]; | ||
}; |
Oops, something went wrong.