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

test: add missing tests for React hooks #225

Merged
merged 14 commits into from
Sep 27, 2024

Conversation

cristianoventura
Copy link
Collaborator

Description

This PR adds missing tests for the React hooks present in ./src/hooks/

How to Test

Checklist

  • I have performed a self-review of my code.
  • I have added tests for my changes.
  • I have run linter locally (npm run lint) and all checks pass.
  • I have run tests locally (npm test) and all tests pass.
  • I have commented on my code, particularly in hard-to-understand areas.

Screenshots (if appropriate):

N/A

Related PR(s)/Issue(s)

N/A

@cristianoventura cristianoventura self-assigned this Sep 25, 2024
@cristianoventura cristianoventura marked this pull request as ready for review September 25, 2024 22:44
@cristianoventura cristianoventura requested a review from a team as a code owner September 25, 2024 22:44
@cristianoventura cristianoventura requested review from e-fisher, Llandy3d and going-confetti and removed request for a team September 25, 2024 22:44
Copy link
Collaborator

@e-fisher e-fisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌
Overall looks good, left a few comments about casting as Mock and beforeAll/beforeEach placement - they apply to multiple files, but I didn't repeat comments to reduce noise.

Also, the useSetWindowTitle is currently not used anywhere, perhaps we keep it in case we use it later since it's well tested now. 😄 But for the next tests, I would suggest focusing on discovering and testing "fragile" and critical parts of the app, instead of striving for 100% coverage on specific type of components.

Comment on lines 1 to 26
import { renderHook } from '@testing-library/react'
import { useCloseSplashScreen } from './useCloseSplashScreen'
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'

const closeSplashscreen = vi.fn()

beforeAll(() => {
window.studio = {
...window.studio,
app: {
closeSplashscreen,
},
}
})

beforeEach(() => {
vi.clearAllMocks()
})

describe('useCloseSplashScreen', () => {
it('should call closeSplashscreen on mount', () => {
renderHook(() => useCloseSplashScreen())

expect(closeSplashscreen).toHaveBeenCalled()
})
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's always this line between not enough tests and testing too much - I think there are some hooks that deserve test coverage, like useListenProxyData or useProxyDataGroups, which have some logic covering cases not immediately obvious when just looking at code. But testing hooks like useCloseSplashScreen feels an overkill, it becomes more of a framework testing - verifying useEffect behaves as expected and executes on mount, which is likely already tested in react codebase.

Comment on lines 42 to 44
;(createNewGeneratorFile as Mock).mockReturnValue(newGenerator)
;(generateFileNameWithTimestamp as Mock).mockReturnValue(fileName)
;(getRoutePath as Mock).mockReturnValue(routePath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By casting as Mock you lose type safety and mockReturnValue accepts any.
There's a helper vi.mocked which allows removing cast (as well as this ugly syntax 😅) and preserve type safety:

Suggested change
;(createNewGeneratorFile as Mock).mockReturnValue(newGenerator)
;(generateFileNameWithTimestamp as Mock).mockReturnValue(fileName)
;(getRoutePath as Mock).mockReturnValue(routePath)
vi.mocked(createNewGeneratorFile).mockReturnValue(newGenerator)
vi.mocked(generateFileNameWithTimestamp).mockReturnValue(fileName)
vi.mocked(getRoutePath).mockReturnValue(routePath)

You'll get a typescript error on the first line because { id: 'test' } is not a valid GeneratorFileData, but that's expected, and a fixture should be created

Comment on lines +114 to +123
const proxyDataWithResponse = createProxyData()
const proxyDataWithoutResponse = createProxyDataWithoutResponse()
const responseWith304StatusCode = createResponse({
statusCode: 304,
content: '',
headers: [],
cookies: [],
query: [],
scheme: 'http',
host: 'example.com',
method: 'GET',
path: '/api/v1/users',
content: null,
timestampStart: 0,
timestampEnd: 0,
contentLength: 0,
httpVersion: '1.1',
url: 'http://example.com',
}

const response: ProxyData['response'] = {
statusCode: 200,
headers: [['content-type', 'application/json']],
cookies: [],
reason: 'OK',
content: '{"hello":"world"}',
path: '/api/v1/users',
httpVersion: '1.1',
timestampStart: 0,
contentLength: 0,
}

const proxyDataWithoutResponse: ProxyData = {
id: '1',
request,
}

const proxyDataWithResponse: ProxyData = {
id: '1',
request,
response,
}

const proxyDataWith304Response: ProxyData = {
...proxyDataWithResponse,
request,
response: {
...response,
statusCode: 304,
content: '',
headers: [],
},
}
})
const proxyDataWith304Response = createProxyData({
response: responseWith304StatusCode,
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

Comment on lines 21 to 23
beforeEach(() => {
vi.clearAllMocks()
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be inside describe?

Comment on lines 11 to 18
window.studio = {
...window.studio,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
script: {
onScriptCheck,
},
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly shorter and won't require eslint/ts comments

Suggested change
window.studio = {
...window.studio,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
script: {
onScriptCheck,
},
}
vi.stubGlobal('studio', {
script: {
onScriptCheck,
},
})

Copy link
Collaborator

@e-fisher e-fisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🙇

@cristianoventura cristianoventura merged commit 3e05015 into main Sep 27, 2024
2 checks passed
@cristianoventura cristianoventura deleted the test/missing-hooks-tests branch September 27, 2024 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants