Skip to content

Add basic test for node env #1378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/use-swr-immutable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('useSWR - immutable', () => {
useData()
return null
}

function Page() {
const [showComponent, setShowComponent] = useState(false)
const { data } = useData()
Expand Down
42 changes: 42 additions & 0 deletions test/use-swr-node-env.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @jest-environment node
*/

import React from 'react'
import { renderToString } from 'react-dom/server'
import useSWR from '../src'
import useSWRImmutable from '../immutable'
import { createKey } from './utils'
import { IS_SERVER } from '../src/utils/env'

describe('useSWR', () => {
it('env IS_SERVER is true in node env', () => {
expect(IS_SERVER).toBe(true)
})

it('should render fallback if provided on server side', async () => {
const key = createKey()
const useData = () => useSWR(key, k => k, { fallbackData: 'fallback' })

function Page() {
const { data } = useData()
return <p>{data}</p>
}

const string = renderToString(<Page />)
expect(string).toContain('fallback')
})

it('should not revalidate useSWRImmutable on server side', async () => {
const key = createKey()
const useData = () => useSWRImmutable(key, k => k)

function Page() {
const { data } = useData()
return <p>{data || 'empty'}</p>
}

const string = renderToString(<Page />)
expect(string).toContain('empty')
})
})