Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kremalicious committed Nov 6, 2023
1 parent c2741c1 commit f4faca4
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 35 deletions.
9 changes: 4 additions & 5 deletions scripts/create-icons/index.test.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 })
})
2 changes: 1 addition & 1 deletion scripts/move-downloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/new/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions src/components/Exif/ExifMap.test.tsx
Original file line number Diff line number Diff line change
@@ -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 }
})
})
})
7 changes: 6 additions & 1 deletion src/components/Location/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
})
})
2 changes: 0 additions & 2 deletions src/features/Web3/components/Preview/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export function Preview() {
isDisabled={isLoading}
/>

{console.log(txConfig)}

{error || prepareError ? (
<div className={styles.alert}>{error || prepareError}</div>
) : null}
Expand Down
4 changes: 2 additions & 2 deletions src/features/Web3/hooks/useFetchTokens/useFetchTokens.tsx
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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()

Expand Down
6 changes: 5 additions & 1 deletion src/lib/astro/loadAndFormatCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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', () => {
Expand Down
29 changes: 21 additions & 8 deletions src/lib/github/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => ({
Expand All @@ -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()
})
})
1 change: 1 addition & 0 deletions test/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default getViteConfig({
all: true,
exclude: [
'**/*.d.ts',
'**/types.ts',
'**/*.test.ts',
'**/*.spec.ts',
'**/test/**/*',
Expand Down
6 changes: 1 addition & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*",
Expand Down

1 comment on commit f4faca4

@vercel
Copy link

@vercel vercel bot commented on f4faca4 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blog – ./

blog-git-main-kremalicious.vercel.app
blog-kremalicious.vercel.app
kremalicious.vercel.app

Please sign in to comment.