-
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.
* feat: add __SWR_DEVTOOLS_USE__ hook * refactor: define the devtools hook in web-preset * feat: expose the React instance as window.__SWR_DEVTOOLS_REACT__ * refactor: create devtools preset and a setup function * test: fix a failed test for devtools * test: add a test for window.__SWR_DEVTOOLS_REACT__ * refactor: inject the devtools middleware as a built-in middleware * fix: inject __SWR_DEVTOOLS_REACT__ only .__SWR_DEVTOOLS_USE__ is defined * refactor: sort an import statement
- Loading branch information
Showing
4 changed files
with
53 additions
and
1 deletion.
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,17 @@ | ||
import React from 'react' | ||
import { isWindowDefined } from './helper' | ||
|
||
// @ts-expect-error | ||
const enableDevtools = isWindowDefined && window.__SWR_DEVTOOLS_USE__ | ||
|
||
export const use = enableDevtools | ||
? // @ts-expect-error | ||
window.__SWR_DEVTOOLS_USE__ | ||
: [] | ||
|
||
export const setupDevTools = () => { | ||
if (enableDevtools) { | ||
// @ts-expect-error | ||
window.__SWR_DEVTOOLS_REACT__ = React | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { use as devtoolsUse } from './devtools' | ||
import { middleware as preload } from './preload' | ||
|
||
export const BUILT_IN_MIDDLEWARE = [preload] | ||
export const BUILT_IN_MIDDLEWARE = devtoolsUse.concat(preload) |
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,31 @@ | ||
import { screen } from '@testing-library/react' | ||
import React from 'react' | ||
|
||
describe('useSWR - devtools', () => { | ||
let useSWR, createKey, createResponse, renderWithConfig | ||
beforeEach(() => { | ||
const middleware = | ||
useSWRNext => | ||
(...args) => { | ||
const result = useSWRNext(...args) | ||
return { ...result, data: 'middleware' } | ||
} | ||
// @ts-expect-error | ||
window.__SWR_DEVTOOLS_USE__ = [middleware] | ||
;({ createKey, createResponse, renderWithConfig } = require('./utils')) | ||
useSWR = require('swr').default | ||
}) | ||
it('window.__SWR_DEVTOOLS_USE__ should be set as middleware', async () => { | ||
const key = createKey() | ||
function Page() { | ||
const { data } = useSWR(key, () => createResponse('ok')) | ||
return <div>data: {data}</div> | ||
} | ||
renderWithConfig(<Page />) | ||
await screen.findByText('data: middleware') | ||
}) | ||
it('window.__SWR_DEVTOOLS_REACT__ should be the same reference with React', () => { | ||
// @ts-expect-error | ||
expect(window.__SWR_DEVTOOLS_REACT__).toBe(React) | ||
}) | ||
}) |