test tweaks

This commit is contained in:
Matthias Kretschmann 2022-11-17 20:58:40 +00:00
parent cb26018953
commit 93fa06bf1b
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 23 additions and 14 deletions

View File

@ -1,12 +1,5 @@
import { render, screen, act } from '@testing-library/react'
import Header from '.'
import { dataLocation } from '../../../tests/__fixtures__/location'
;(global.fetch as jest.Mock) = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(dataLocation)
})
)
describe('Header', () => {
it('renders correctly', async () => {

View File

@ -1,12 +1,5 @@
import { render, screen, act } from '@testing-library/react'
import Site from '.'
import { dataLocation } from '../../../tests/__fixtures__/location'
;(global.fetch as jest.Mock) = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(dataLocation)
})
)
describe('Site', () => {
it('renders without crashing', async () => {

8
src/lib/umami.test.ts Normal file
View File

@ -0,0 +1,8 @@
import { UMAMI_SCRIPT_URL, UMAMI_WEBSITE_ID } from './umami'
describe('lib/umami', () => {
test('exports env vars', async () => {
expect(UMAMI_SCRIPT_URL).toBe(process.env.NEXT_PUBLIC_UMAMI_SCRIPT_URL)
expect(UMAMI_WEBSITE_ID).toBe(process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID)
})
})

View File

@ -1,6 +1,9 @@
import '@testing-library/jest-dom/extend-expect'
import 'jest-canvas-mock'
import { jest } from '@jest/globals'
import { useLocation } from '../src/hooks/useLocation'
import framer from 'framer-motion'
import { dataLocation } from './__fixtures__/location'
import giphy from './__fixtures__/giphy.json'
import './__mocks__/matchMedia'
@ -20,6 +23,10 @@ jest.mock('next/head', () => {
}
})
jest.mock('../src/hooks/useLocation', () => ({
useLocation: jest.fn().mockImplementation(() => dataLocation)
}))
jest.mock('@giphy/js-fetch-api', () => ({
GiphyFetch: jest.fn().mockImplementation(() => ({
random: jest.fn().mockImplementation(() => Promise.resolve(giphy))
@ -27,8 +34,16 @@ jest.mock('@giphy/js-fetch-api', () => ({
}))
const unmockedFetch = global.fetch
const unmockedEnv = process.env
beforeEach(() => {
// jest.resetModules()
global.fetch = unmockedFetch
process.env = { ...unmockedEnv }
})
afterEach(() => {
global.fetch = unmockedFetch
process.env = unmockedEnv
jest.restoreAllMocks()
})