Skip to content

Commit

Permalink
Add basic test for node env (#1378)
Browse files Browse the repository at this point in the history
* Add test for node env

* use immutable

* add is server flag checking
  • Loading branch information
huozhi authored Aug 24, 2021
1 parent 7ea63ef commit b02d743
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
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')
})
})

0 comments on commit b02d743

Please sign in to comment.