-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add test for node env * use immutable * add is server flag checking
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |