-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests for react-* headless components (#347)
Basic testing to verify the components in `Authenticator`, `Uploader`, and `UploadsList` call the expected API functions from their respective providers.
- Loading branch information
Showing
7 changed files
with
205 additions
and
4 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,63 @@ | ||
import React from 'react' | ||
import 'fake-indexeddb/auto' | ||
import { test, expect, vi } from 'vitest' | ||
import user from '@testing-library/user-event' | ||
import { render, screen } from '@testing-library/react' | ||
|
||
import { KeyringContext, keyringContextDefaultValue, KeyringContextValue } from '../src/providers/Keyring' | ||
import { Authenticator, CancelButton, Form, EmailInput } from '../src/index' | ||
|
||
test('CancelButton', async () => { | ||
const cancelRegisterSpace = vi.fn() | ||
const contextValue: KeyringContextValue = [ | ||
keyringContextDefaultValue[0], | ||
{ ...keyringContextDefaultValue[1], cancelRegisterSpace } | ||
] | ||
render( | ||
<KeyringContext.Provider value={contextValue}> | ||
<Authenticator> | ||
<CancelButton>Cancel</CancelButton> | ||
</Authenticator> | ||
</KeyringContext.Provider> | ||
) | ||
|
||
const cancelButton = screen.getByText('Cancel') | ||
await user.click(cancelButton) | ||
|
||
expect(cancelRegisterSpace).toHaveBeenCalledOnce() | ||
}) | ||
|
||
test('Form', async () => { | ||
const createSpace = vi.fn() | ||
const registerSpace = vi.fn() | ||
|
||
const contextValue: KeyringContextValue = [ | ||
keyringContextDefaultValue[0], | ||
{ | ||
...keyringContextDefaultValue[1], | ||
createSpace, | ||
registerSpace | ||
} | ||
] | ||
render( | ||
<KeyringContext.Provider value={contextValue}> | ||
<Authenticator> | ||
<Form> | ||
<EmailInput placeholder='Email' /> | ||
<input type='submit' value='Create Space' /> | ||
</Form> | ||
</Authenticator> | ||
</KeyringContext.Provider> | ||
) | ||
|
||
const myEmail = '[email protected]' | ||
const emailInput = screen.getByPlaceholderText('Email') | ||
await user.click(emailInput) | ||
await user.keyboard(myEmail) | ||
|
||
const submitButton = screen.getByText('Create Space') | ||
await user.click(submitButton) | ||
|
||
expect(createSpace).toHaveBeenCalledOnce() | ||
expect(registerSpace).toHaveBeenCalledWith(myEmail) | ||
}) |
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,40 @@ | ||
import React from 'react' | ||
import 'fake-indexeddb/auto' | ||
import { test, expect, vi } from 'vitest' | ||
import user from '@testing-library/user-event' | ||
import { render, screen } from '@testing-library/react' | ||
|
||
import { UploaderContext, uploaderContextDefaultValue, UploaderContextValue } from '../src/providers/Uploader' | ||
import { Uploader, Input, Form } from '../src/index' | ||
|
||
test('Form', async () => { | ||
const uploadFile = vi.fn() | ||
|
||
const contextValue: UploaderContextValue = [ | ||
uploaderContextDefaultValue[0], | ||
{ | ||
...uploaderContextDefaultValue[1], | ||
uploadFile | ||
} | ||
] | ||
render( | ||
<UploaderContext.Provider value={contextValue}> | ||
<Uploader> | ||
<Form> | ||
<Input data-testid='file-upload' /> | ||
<input type='submit' value='Upload' /> | ||
</Form> | ||
</Uploader> | ||
</UploaderContext.Provider> | ||
) | ||
|
||
const file = new File(['hello'], 'hello.txt', { type: 'text/plain' }) | ||
|
||
const fileInput = screen.getByTestId('file-upload') | ||
await user.upload(fileInput, file) | ||
|
||
const submitButton = screen.getByText('Upload') | ||
await user.click(submitButton) | ||
|
||
expect(uploadFile).toHaveBeenCalledWith(file) | ||
}) |
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,49 @@ | ||
import React from 'react' | ||
import 'fake-indexeddb/auto' | ||
import { test, expect, vi } from 'vitest' | ||
import user from '@testing-library/user-event' | ||
import { render, screen } from '@testing-library/react' | ||
|
||
import { UploadsListContext, uploadsListContextDefaultValue, UploadsListContextValue } from '../src/providers/UploadsList' | ||
import { UploadsList, NextButton, ReloadButton } from '../src/index' | ||
|
||
test('Form', async () => { | ||
const reload = vi.fn() | ||
const next = vi.fn() | ||
|
||
const contextValue: UploadsListContextValue = [ | ||
uploadsListContextDefaultValue[0], | ||
{ | ||
...uploadsListContextDefaultValue[1], | ||
reload, | ||
next | ||
} | ||
] | ||
render( | ||
<UploadsListContext.Provider value={contextValue}> | ||
<UploadsList> | ||
{({ uploadsList }) => ( | ||
<div> | ||
<ul> | ||
{uploadsList?.[0].data?.map(({ root }) => ( | ||
<li key={root.toString()}>{root.toString()}</li> | ||
))} | ||
</ul> | ||
<NextButton>Next</NextButton> | ||
<ReloadButton>Reload</ReloadButton> | ||
</div> | ||
)} | ||
</UploadsList> | ||
</UploadsListContext.Provider> | ||
) | ||
|
||
const reloadButton = screen.getByText('Reload') | ||
await user.click(reloadButton) | ||
|
||
expect(reload).toHaveBeenCalledOnce() | ||
|
||
const nextButton = screen.getByText('Next') | ||
await user.click(nextButton) | ||
|
||
expect(next).toHaveBeenCalledOnce() | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.