1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-24 19:04:08 +01:00

test fixes

This commit is contained in:
Matthias Kretschmann 2023-11-06 13:37:27 +00:00
parent c2741c1711
commit f4faca4e51
Signed by: m
GPG Key ID: 606EEEF3C479A91F
11 changed files with 54 additions and 35 deletions

View File

@ -1,14 +1,10 @@
import { afterAll, test } from 'vitest'
import { test } from 'vitest'
import fs from 'fs/promises'
import path from 'path'
import { generateIcons } from './index'
const distDir = path.resolve(__dirname, 'tmp')
afterAll(() => {
fs.rm(path.resolve(__dirname, 'tmp'), { recursive: true })
})
test('should generate Astro & React components from SVG files', async () => {
// Act
await generateIcons(distDir)
@ -45,4 +41,7 @@ test('should generate Astro & React components from SVG files', async () => {
`Example React component does not exist: ${exampleComponentPathReact}`
)
}
// cleanup
await fs.rm(distDir, { recursive: true, force: true })
})

View File

@ -22,7 +22,7 @@ function removeFolderContents(folderPath: string) {
const filePath = path.join(folderPath, file)
if (fs.lstatSync(filePath).isDirectory()) {
removeFolderContents(filePath)
fs.rmdirSync(filePath)
fs.rmSync(filePath)
} else {
fs.unlinkSync(filePath)
}

View File

@ -9,7 +9,7 @@ const destFolder = path.join('.', 'test/__fixtures__/tmp')
describe('npm run new', () => {
afterEach(async () => {
await fs.rmdir(destFolder, { recursive: true })
await fs.rm(destFolder, { recursive: true })
})
// Mock spinner

View File

@ -1,20 +1,23 @@
import { describe, it } from 'vitest'
import { render, screen } from '@testing-library/react'
import { render, screen, fireEvent } from '@testing-library/react'
import ExifMap from './ExifMap'
describe('ExifMap', () => {
it('renders without crashing', async () => {
render(
<html data-theme="dark">
<body>
<input id="toggle" type="checkbox" />
<ExifMap
gps={{ latitude: 41.89007222222222, longitude: 12.491516666666666 }}
/>
</body>
</html>
<>
<input id="toggle" data-testid="toggle" type="checkbox" />
<ExifMap
gps={{ latitude: 41.89007222222222, longitude: 12.491516666666666 }}
/>
</>
)
await screen.findByText(/wheel to zoom/)
// Simulate a change event on the checkbox
fireEvent.change(screen.getByTestId('toggle'), {
target: { checked: true }
})
})
})

View File

@ -77,7 +77,9 @@ describe('Location component', () => {
})
it('renders nothing and logs error when error is encountered', () => {
const consoleErrorSpy = vi.spyOn(console, 'error')
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
useStoreSpy.mockImplementationOnce(() => ({
data: null,
loading: false,
@ -90,5 +92,8 @@ describe('Location component', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to fetch location: Error'
)
// Restore the original console.error function after the test
consoleErrorSpy.mockRestore()
})
})

View File

@ -39,8 +39,6 @@ export function Preview() {
isDisabled={isLoading}
/>
{console.log(txConfig)}
{error || prepareError ? (
<div className={styles.alert}>{error || prepareError}</div>
) : null}

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from 'react'
import useSWR from 'swr'
import useSWR, { type SWRResponse } from 'swr'
import { useNetwork, useAccount } from 'wagmi'
import type { GetToken } from './types'
@ -9,7 +9,7 @@ const apiUrl = import.meta.env.PUBLIC_WEB3_API_URL
//
// Wrapper for fetching user tokens with swr.
//
export function useFetchTokens() {
export function useFetchTokens(): SWRResponse<GetToken[] | undefined, Error> {
const { chain } = useNetwork()
const { address } = useAccount()

View File

@ -8,6 +8,7 @@ import getCollectionPhotos from '@test/__fixtures__/getCollectionPhotos.json'
let getCollectionSpy: any
let readOutExifSpy: any
let consoleErrorSpy: any
beforeEach(() => {
getCollectionSpy = vi.spyOn(astroContent, 'getCollection')
@ -17,11 +18,14 @@ beforeEach(() => {
exif: 'mocked exif',
iptc: 'mocked iptc'
}))
// Silence console.error
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
getCollectionSpy.mockRestore()
readOutExifSpy.mockRestore()
consoleErrorSpy.mockRestore()
})
describe('loadAndFormatCollection', () => {

View File

@ -23,23 +23,36 @@ describe('getRepo Function', () => {
}
}
const mockFetch = async () => ({ json: async () => mockResponseData })
it('should fetch repo data', async () => {
const mockFetch = async () => ({ json: async () => mockResponseData })
const originalFetch = window.fetch
// Silence console.log, console.info, and console.error
const originalConsoleLog = console.log
const originalConsoleInfo = console.info
const originalConsoleError = console.error
console.log = () => {}
console.info = () => {}
console.error = () => {}
window.fetch = mockFetch as any
const repoInfo = await getRepo('mockuser/mockrepo')
window.fetch = originalFetch
expect(repoInfo).toEqual(mockResponseData.data.user.repository)
// Restore the original fetch and console functions
window.fetch = originalFetch
console.log = originalConsoleLog
console.info = originalConsoleInfo
console.error = originalConsoleError
})
it('should handle errors', async () => {
const consoleMock = vi
const consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)
const originalFetch = window.fetch
;(window as any).fetch = async () => ({
@ -51,9 +64,9 @@ describe('getRepo Function', () => {
window.fetch = originalFetch
expect(repoInfo).toBeUndefined()
expect(consoleMock).toHaveBeenCalled()
expect(consoleMock).toHaveBeenLastCalledWith(['Mock error message'])
expect(consoleErrorMock).toHaveBeenCalled()
expect(consoleErrorMock).toHaveBeenLastCalledWith(['Mock error message'])
consoleMock.mockReset()
consoleErrorMock.mockReset()
})
})

View File

@ -19,6 +19,7 @@ export default getViteConfig({
all: true,
exclude: [
'**/*.d.ts',
'**/types.ts',
'**/*.test.ts',
'**/*.spec.ts',
'**/test/**/*',

View File

@ -17,11 +17,7 @@
},
"typeRoots": ["./src/@types", "./node_modules/@types"]
},
"exclude": [
"src/images/components/**/*",
"src/content",
"scripts/create-icons/tmp/"
],
"exclude": ["src/images/components/**/*", "src/content"],
"include": [
"src/**/*",
"content/**/*",