Skip to content

Commit

Permalink
chore: renterd jest config
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 21, 2024
1 parent d59af8d commit c9984e4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/renterd/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export default {
'next/dist/build/jest/__mocks__/nextFontMock.js'
),
},
setupFiles: ['./jest.polyfills.js'],
}
31 changes: 31 additions & 0 deletions apps/renterd/jest.polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// jest.polyfills.js
/**
* @note The block below contains polyfills for Node.js globals
* required for Jest to function when running JSDOM tests.
* These HAVE to be require's and HAVE to be in this exact
* order, since "undici" depends on the "TextEncoder" global API.
*
* Consider migrating to a more modern test runner if
* you don't want to deal with this.
*/

const { TextDecoder, TextEncoder, ReadableStream } = require('node:util')

Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
ReadableStream: { value: ReadableStream },
})

const { Blob, File } = require('node:buffer')
const { fetch, Headers, FormData, Request, Response } = require('undici')

Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
})
31 changes: 31 additions & 0 deletions apps/renterd/mock/mock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { SetupServer } from 'msw/node'
import { HttpResponse, http } from 'msw'
import { Bucket } from '@siafoundation/react-renterd'

export function mockApiBusBuckets(server: SetupServer) {
server.use(
http.get('/api/bus/buckets', () => {
return HttpResponse.json([
{
name: 'foo',
policy: {
publicReadAccess: true,
},
},
] as Bucket[])
})
)
}

export function mockMatchMedia() {
window.matchMedia = jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
}))
}

0 comments on commit c9984e4

Please sign in to comment.