forked from Uniswap/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: set up component snapshot testing (Uniswap#2102)
* set up snapshot testing * improvements * add tests for TextInput as an example * Fix code style issues with ESLint * add comment to custom-test-env file * only set up needed providers * include style rules in snapshots * disable redux storage warning * added setupTests to avoid boilerplate Co-authored-by: Lint Action <[email protected]>
- Loading branch information
1 parent
718003b
commit 8dfd143
Showing
8 changed files
with
384 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Custom test environment to provide `TextEncoder`/`TextDecoder` | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const Environment = require('jest-environment-jsdom') | ||
|
||
module.exports = class CustomTestEnvironment extends Environment { | ||
async setup() { | ||
await super.setup() | ||
if (typeof this.global.TextEncoder === 'undefined') { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { TextEncoder, TextDecoder } = require('util') | ||
this.global.TextEncoder = TextEncoder | ||
this.global.TextDecoder = TextDecoder | ||
} | ||
} | ||
} |
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
130 changes: 130 additions & 0 deletions
130
src/components/TextInput/__snapshots__/index.test.tsx.snap
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,130 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ResizableTextArea renders correctly 1`] = ` | ||
<DocumentFragment> | ||
.c0 { | ||
font-size: 12; | ||
outline: none; | ||
border: none; | ||
-webkit-flex: 1 1 auto; | ||
-ms-flex: 1 1 auto; | ||
flex: 1 1 auto; | ||
width: 0; | ||
resize: none; | ||
background-color: #F7F8FA; | ||
-webkit-transition: color 300ms step-start; | ||
transition: color 300ms step-start; | ||
color: #000000; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
font-weight: 500; | ||
width: 100%; | ||
line-height: 1.2; | ||
padding: 0px; | ||
-webkit-appearance: textfield; | ||
} | ||
.c0::-webkit-search-decoration { | ||
-webkit-appearance: none; | ||
} | ||
.c0::-webkit-outer-spin-button, | ||
.c0::-webkit-inner-spin-button { | ||
-webkit-appearance: none; | ||
} | ||
.c0::-webkit-input-placeholder { | ||
color: #C3C5CB; | ||
} | ||
.c0::-moz-placeholder { | ||
color: #C3C5CB; | ||
} | ||
.c0:-ms-input-placeholder { | ||
color: #C3C5CB; | ||
} | ||
.c0::placeholder { | ||
color: #C3C5CB; | ||
} | ||
<textarea | ||
autocapitalize="off" | ||
autocomplete="off" | ||
autocorrect="off" | ||
class="c0 testing" | ||
font-size="12" | ||
placeholder="Test Placeholder" | ||
spellcheck="false" | ||
style="height: auto; min-height: 500px;" | ||
> | ||
My test input | ||
</textarea> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`TextInput renders correctly 1`] = ` | ||
<DocumentFragment> | ||
.c0 { | ||
font-size: 12; | ||
outline: none; | ||
border: none; | ||
-webkit-flex: 1 1 auto; | ||
-ms-flex: 1 1 auto; | ||
flex: 1 1 auto; | ||
width: 0; | ||
background-color: #F7F8FA; | ||
-webkit-transition: color 300ms step-start; | ||
transition: color 300ms step-start; | ||
color: #000000; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
font-weight: 500; | ||
width: 100%; | ||
padding: 0px; | ||
-webkit-appearance: textfield; | ||
} | ||
.c0::-webkit-search-decoration { | ||
-webkit-appearance: none; | ||
} | ||
.c0::-webkit-outer-spin-button, | ||
.c0::-webkit-inner-spin-button { | ||
-webkit-appearance: none; | ||
} | ||
.c0::-webkit-input-placeholder { | ||
color: #C3C5CB; | ||
} | ||
.c0::-moz-placeholder { | ||
color: #C3C5CB; | ||
} | ||
.c0:-ms-input-placeholder { | ||
color: #C3C5CB; | ||
} | ||
.c0::placeholder { | ||
color: #C3C5CB; | ||
} | ||
<div | ||
class="testing" | ||
> | ||
<input | ||
autocapitalize="off" | ||
autocomplete="off" | ||
autocorrect="off" | ||
class="c0" | ||
font-size="12" | ||
placeholder="Test Placeholder" | ||
spellcheck="false" | ||
type="text" | ||
value="My test input" | ||
/> | ||
</div> | ||
</DocumentFragment> | ||
`; |
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,68 @@ | ||
import { TextInput, ResizingTextArea } from './' | ||
import { render, screen, fireEvent } from 'test-utils' | ||
|
||
describe('TextInput', () => { | ||
it('renders correctly', () => { | ||
const { asFragment } = render( | ||
<TextInput | ||
className="testing" | ||
value="My test input" | ||
onUserInput={() => null} | ||
placeholder="Test Placeholder" | ||
fontSize="12" | ||
/> | ||
) | ||
expect(asFragment()).toMatchSnapshot() | ||
}) | ||
|
||
it('calls the handler on user input', () => { | ||
const onUserInputSpy = jest.fn() | ||
render( | ||
<TextInput | ||
className="testing" | ||
value="" | ||
onUserInput={onUserInputSpy} | ||
placeholder="Test Placeholder" | ||
fontSize="12" | ||
/> | ||
) | ||
|
||
fireEvent.change(screen.getByPlaceholderText('Test Placeholder'), { target: { value: 'New value' } }) | ||
|
||
expect(onUserInputSpy).toHaveBeenCalledWith('New value') | ||
expect(onUserInputSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
|
||
describe('ResizableTextArea', () => { | ||
it('renders correctly', () => { | ||
const { asFragment } = render( | ||
<ResizingTextArea | ||
className="testing" | ||
value="My test input" | ||
onUserInput={() => null} | ||
placeholder="Test Placeholder" | ||
fontSize="12" | ||
/> | ||
) | ||
expect(asFragment()).toMatchSnapshot() | ||
}) | ||
|
||
it('calls the handler on user input', () => { | ||
const onUserInputSpy = jest.fn() | ||
render( | ||
<ResizingTextArea | ||
className="testing" | ||
value="" | ||
onUserInput={onUserInputSpy} | ||
placeholder="Test Placeholder" | ||
fontSize="12" | ||
/> | ||
) | ||
|
||
fireEvent.change(screen.getByPlaceholderText('Test Placeholder'), { target: { value: 'New value' } }) | ||
|
||
expect(onUserInputSpy).toHaveBeenCalledWith('New value') | ||
expect(onUserInputSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
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 @@ | ||
// jest custom assertions | ||
import '@testing-library/jest-dom' | ||
|
||
// include style rules in snapshots | ||
import 'jest-styled-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React, { FC, ReactElement, ReactNode } from 'react' | ||
import { render, RenderOptions } from '@testing-library/react' | ||
import ThemeProvider from 'theme' | ||
import store from 'state' | ||
import { Provider } from 'react-redux' | ||
|
||
const WithProviders: FC = ({ children }: { children?: ReactNode }) => { | ||
return ( | ||
<Provider store={store}> | ||
<ThemeProvider>{children}</ThemeProvider> | ||
</Provider> | ||
) | ||
} | ||
|
||
const customRender = (ui: ReactElement, options?: Omit<RenderOptions, 'wrapper'>) => | ||
render(ui, { wrapper: WithProviders, ...options }) | ||
|
||
export * from '@testing-library/react' | ||
export { customRender as render } |
Oops, something went wrong.