-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(react): add test cases for devMode in production mode at once, m…
…eet coverage 100% (#744) # Overview <!-- A clear and concise description of what this pr is about. --> ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests.
- Loading branch information
Showing
21 changed files
with
657 additions
and
703 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@suspensive/react": patch | ||
--- | ||
|
||
test(react): add test cases for devMode in production mode |
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 |
---|---|---|
|
@@ -37,3 +37,6 @@ graph/ | |
|
||
# lighthouse | ||
.lighthouseci/ | ||
|
||
# test | ||
tsconfig.vitest-temp.json |
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 |
---|---|---|
|
@@ -11,7 +11,6 @@ comment: | |
|
||
ignore: | ||
- '**/*.test-d.*' | ||
- '**/*.setup.*' | ||
|
||
component_management: | ||
individual_components: | ||
|
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
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,8 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { ProductionDevMode } from './contexts/SuspensiveDevModeContext' | ||
|
||
describe('<DevMode/> in production mode', () => { | ||
it('should show nothing if without SuspensiveProvider', () => { | ||
expect(ProductionDevMode()).toBeNull() | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,54 @@ | ||
import { sleep } from '@suspensive/test-utils' | ||
import { waitFor } from '@testing-library/react' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { SuspensiveDevMode, SuspensiveDevModeOnInfoText } from './SuspensiveDevModeContext' | ||
|
||
describe('SuspensiveDevMode', () => { | ||
it('should have field `is` that can be changed by on off', () => { | ||
const devMode = new SuspensiveDevMode() | ||
|
||
expect(devMode.is).toBe(false) | ||
devMode.on() | ||
expect(devMode.is).toBe(true) | ||
devMode.off() | ||
expect(devMode.is).toBe(false) | ||
}) | ||
|
||
it('should inform if DevMode is on by console.info', async () => { | ||
const devMode = new SuspensiveDevMode() | ||
const infoSpy = vi.spyOn(console, 'info') | ||
|
||
expect(devMode.is).toBe(false) | ||
devMode.on() | ||
expect(devMode.is).toBe(true) | ||
await waitFor(() => expect(infoSpy.mock.calls[0][0]).toBe(SuspensiveDevModeOnInfoText)) | ||
await waitFor(() => expect(infoSpy).toBeCalledTimes(1)) | ||
await waitFor(() => expect(infoSpy).toBeCalledTimes(2)) | ||
await waitFor(() => expect(infoSpy).toBeCalledTimes(3)) | ||
await waitFor(() => expect(infoSpy).toBeCalledTimes(4)) | ||
devMode.off() | ||
await sleep(1000) | ||
expect(infoSpy).toBeCalledTimes(4) | ||
await sleep(1000) | ||
expect(infoSpy).toBeCalledTimes(4) | ||
}) | ||
|
||
it('should notify that devMode have changed to subscribers', () => { | ||
const suspensiveDevMode = new SuspensiveDevMode() | ||
|
||
const subscriber = new (class Subscriber { | ||
notifiedCount = 0 | ||
|
||
onChange = () => { | ||
this.notifiedCount = this.notifiedCount + 1 | ||
} | ||
})() | ||
|
||
suspensiveDevMode.subscribe(subscriber.onChange) | ||
expect(subscriber.notifiedCount).toBe(0) | ||
suspensiveDevMode.on() | ||
expect(subscriber.notifiedCount).toBe(1) | ||
suspensiveDevMode.off() | ||
expect(subscriber.notifiedCount).toBe(2) | ||
}) | ||
}) |
34 changes: 0 additions & 34 deletions
34
packages/react/src/contexts/SuspensiveDevModeContext.production.spec.tsx
This file was deleted.
Oops, something went wrong.
107 changes: 12 additions & 95 deletions
107
packages/react/src/contexts/SuspensiveDevModeContext.spec.tsx
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,102 +1,19 @@ | ||
import { TEXT, sleep } from '@suspensive/test-utils' | ||
import { render, renderHook, screen, waitFor } from '@testing-library/react' | ||
import { userEvent } from '@testing-library/user-event' | ||
import { createElement, useContext } from 'react' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { DevMode } from '../DevMode' | ||
import { renderHook } from '@testing-library/react' | ||
import { useContext } from 'react' | ||
import { describe, expect, it } from 'vitest' | ||
import { Suspensive, SuspensiveProvider } from '../Suspensive' | ||
import { DevModeContext, SuspensiveDevMode, SuspensiveDevModeOnInfoText, syncDevMode } from './SuspensiveDevModeContext' | ||
import { DevModeContext, SuspensiveDevMode } from './SuspensiveDevModeContext' | ||
|
||
describe('SuspensiveDevMode (process.env.NODE_ENV: development)', () => { | ||
it('should have field `is` that can be changed by on off', () => { | ||
const devMode = new SuspensiveDevMode() | ||
|
||
expect(devMode.is).toBe(false) | ||
devMode.on() | ||
expect(devMode.is).toBe(true) | ||
devMode.off() | ||
expect(devMode.is).toBe(false) | ||
}) | ||
|
||
it('should inform if DevMode is on by console.info', async () => { | ||
const devMode = new SuspensiveDevMode() | ||
const infoSpy = vi.spyOn(console, 'info') | ||
|
||
expect(devMode.is).toBe(false) | ||
devMode.on() | ||
expect(devMode.is).toBe(true) | ||
await waitFor(() => expect(infoSpy.mock.calls[0][0]).toBe(SuspensiveDevModeOnInfoText)) | ||
await waitFor(() => expect(infoSpy).toBeCalledTimes(1)) | ||
await waitFor(() => expect(infoSpy).toBeCalledTimes(2)) | ||
await waitFor(() => expect(infoSpy).toBeCalledTimes(3)) | ||
await waitFor(() => expect(infoSpy).toBeCalledTimes(4)) | ||
devMode.off() | ||
await sleep(1000) | ||
expect(infoSpy).toBeCalledTimes(4) | ||
await sleep(1000) | ||
expect(infoSpy).toBeCalledTimes(4) | ||
}) | ||
|
||
it('should notify that devMode have changed to subscribers', () => { | ||
const suspensiveDevMode = new SuspensiveDevMode() | ||
|
||
const subscriber = new (class Subscriber { | ||
notifiedCount = 0 | ||
|
||
onChange = () => { | ||
this.notifiedCount = this.notifiedCount + 1 | ||
} | ||
})() | ||
|
||
suspensiveDevMode.subscribe(subscriber.onChange) | ||
expect(subscriber.notifiedCount).toBe(0) | ||
suspensiveDevMode.on() | ||
expect(subscriber.notifiedCount).toBe(1) | ||
suspensiveDevMode.off() | ||
expect(subscriber.notifiedCount).toBe(2) | ||
}) | ||
|
||
describe('DevModeContext', () => { | ||
it('returns null when no SuspensiveProvider is present', () => { | ||
const { result } = renderHook(() => useContext(DevModeContext)) | ||
expect(result.current).toBeNull() | ||
}) | ||
|
||
it('returns an instance of SuspensiveDevMode when wrapped with SuspensiveProvider', () => { | ||
const { result } = renderHook(() => useContext(DevModeContext), { | ||
wrapper: (props) => <SuspensiveProvider {...props} value={new Suspensive()} />, | ||
}) | ||
expect(result.current).toBeInstanceOf(SuspensiveDevMode) | ||
}) | ||
describe('DevModeContext', () => { | ||
it('returns null when no SuspensiveProvider is present', () => { | ||
const { result } = renderHook(() => useContext(DevModeContext)) | ||
expect(result.current).toBeNull() | ||
}) | ||
|
||
describe('syncDevMode (process.env.NODE_ENV: development)', () => { | ||
it('should make component synced with DevMode at development', async () => { | ||
const user = userEvent.setup() | ||
const logSpy = vi.spyOn(console, 'log') | ||
render( | ||
createElement( | ||
syncDevMode(({ devMode }) => { | ||
console.log(devMode.is) | ||
return <>{TEXT}</> | ||
}) | ||
), | ||
{ | ||
wrapper: ({ children }) => ( | ||
<SuspensiveProvider value={new Suspensive()}> | ||
{children} | ||
<DevMode /> | ||
</SuspensiveProvider> | ||
), | ||
} | ||
) | ||
// expect(screen.queryByText(TEXT)).toBeInTheDocument() | ||
expect(logSpy.mock.calls[0][0]).toBe(false) | ||
user.click(screen.getByRole('Suspensive.DevMode-off')) | ||
await waitFor(() => expect(logSpy.mock.calls[1][0]).toBe(true)) | ||
user.click(screen.getByRole('Suspensive.DevMode-on')) | ||
await waitFor(() => expect(logSpy.mock.calls[2][0]).toBe(false)) | ||
await waitFor(() => expect(logSpy.mock.calls[3]).toBe(undefined)) | ||
it('returns an instance of SuspensiveDevMode when wrapped with SuspensiveProvider', () => { | ||
const { result } = renderHook(() => useContext(DevModeContext), { | ||
wrapper: (props) => <SuspensiveProvider {...props} value={new Suspensive()} />, | ||
}) | ||
expect(result.current).toBeInstanceOf(SuspensiveDevMode) | ||
}) | ||
}) |
Oops, something went wrong.