refactor(tests): ⚡️ Add msw and mock authentication
This commit is contained in:
30
apps/builder/mocks/index.ts
Normal file
30
apps/builder/mocks/index.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { rest, setupWorker } from 'msw'
|
||||
import { setupServer } from 'msw/node'
|
||||
|
||||
const handlers = () => [
|
||||
rest.get('http://localhost:3000/api/auth/session', (req, res, ctx) => {
|
||||
const authenticatedUser = JSON.parse(
|
||||
typeof localStorage !== 'undefined'
|
||||
? (localStorage.getItem('authenticatedUser') as string)
|
||||
: '{"id":"proUser","name":"John Smith","email":"john@smith.com","emailVerified":null,"image":"https://avatars.githubusercontent.com/u/16015833?v=4","plan":"PRO","stripeId":null}'
|
||||
)
|
||||
return res(
|
||||
ctx.json({
|
||||
user: authenticatedUser,
|
||||
expires: '2022-03-13T17:02:42.317Z',
|
||||
})
|
||||
)
|
||||
}),
|
||||
]
|
||||
|
||||
export const enableMocks = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
const server = setupServer(...handlers())
|
||||
server.listen()
|
||||
} else {
|
||||
const worker = setupWorker(...handlers())
|
||||
worker.start({
|
||||
onUnhandledRequest: 'bypass',
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user