Skip to content
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

feat: add DevTools hook #2016

Merged
merged 11 commits into from
Jun 28, 2022
3 changes: 3 additions & 0 deletions _internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { SWRConfig, revalidateEvents }

export { initCache } from './utils/cache'
export { defaultConfig, cache, mutate, compare } from './utils/config'
import { setupDevTools } from './utils/devtools'
export * from './utils/env'
export { SWRGlobalState } from './utils/global-state'
export { stableHash } from './utils/hash'
Expand All @@ -23,3 +24,5 @@ export { withMiddleware } from './utils/with-middleware'
export { preload } from './utils/preload'

export * from './types'

setupDevTools()
17 changes: 17 additions & 0 deletions _internal/utils/devtools.ts
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
}
}
3 changes: 2 additions & 1 deletion _internal/utils/middleware-preset.ts
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)
31 changes: 31 additions & 0 deletions test/use-swr-devtools.test.tsx
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)
})
})