-
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.
- Loading branch information
Showing
15 changed files
with
266 additions
and
117 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,2 @@ | ||
*.test.js | ||
*.test.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
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,2 +1,3 @@ | ||
node_modules | ||
.next | ||
coverage |
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,55 @@ | ||
const React = require('react'); | ||
const reactI18next = require('react-i18next'); | ||
|
||
const hasChildren = node => node && (node.children || (node.props && node.props.children)); | ||
|
||
const getChildren = node => (node && node.children ? node.children : node.props && node.props.children); | ||
|
||
const renderNodes = reactNodes => { | ||
if (typeof reactNodes === 'string') { | ||
return reactNodes; | ||
} | ||
|
||
return Object.keys(reactNodes).map((key, i) => { | ||
const child = reactNodes[key]; | ||
const isElement = React.isValidElement(child); | ||
|
||
if (typeof child === 'string') { | ||
return child; | ||
} | ||
if (hasChildren(child)) { | ||
const inner = renderNodes(getChildren(child)); | ||
return React.cloneElement(child, { ...child.props, key: i }, inner); | ||
} | ||
if (typeof child === 'object' && !isElement) { | ||
return Object.keys(child).reduce((str, childKey) => `${str}${child[childKey]}`, ''); | ||
} | ||
|
||
return child; | ||
}); | ||
}; | ||
|
||
const useMock = [k => k, {}]; | ||
useMock.t = k => k; | ||
useMock.i18n = {}; | ||
|
||
const output = { | ||
// this mock makes sure any components using the translate HoC receive the t function as a prop | ||
Trans: ({ children }) => renderNodes(children), | ||
Translation: ({ children }) => children(k => k, { i18n: {} }), | ||
useTranslation: () => useMock, | ||
|
||
// mock if needed | ||
I18nextProvider: reactI18next.I18nextProvider, | ||
initReactI18next: reactI18next.initReactI18next, | ||
setDefaults: reactI18next.setDefaults, | ||
getDefaults: reactI18next.getDefaults, | ||
setI18n: reactI18next.setI18n, | ||
getI18n: reactI18next.getI18n, | ||
withTranslation: () => Component => { | ||
Component.defaultProps = { ...Component.defaultProps, t: (t: string) => `t.${t}` }; | ||
return Component; | ||
}, | ||
}; | ||
|
||
module.exports = output; |
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,58 +1,10 @@ | ||
import { | ||
getByLabelText, | ||
getByText, | ||
getByTestId, | ||
queryByTestId, | ||
// Tip: all queries are also exposed on an object | ||
// called "queries" which you could import here as well | ||
waitFor, | ||
} from '@testing-library/dom'; | ||
import React from 'react'; | ||
// adds special assertions like toHaveTextContent | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { render, screen } from '@testing-library/react'; | ||
import Button, { Sizes } from './Button'; | ||
|
||
function getExampleDOM() { | ||
// This is just a raw example of setting up some DOM | ||
// that we can interact with. Swap this with your UI | ||
// framework of choice 😉 | ||
const div = document.createElement('div'); | ||
div.innerHTML = ` | ||
<label for="username">Username</label> | ||
<input id="username" /> | ||
<button>Print Username</button> | ||
`; | ||
const button = div.querySelector('button'); | ||
const input = div.querySelector('input'); | ||
button.addEventListener('click', () => { | ||
// let's pretend this is making a server request, so it's async | ||
// (you'd want to mock this imaginary request in your unit tests)... | ||
setTimeout(() => { | ||
const printedUsernameContainer = document.createElement('div'); | ||
printedUsernameContainer.innerHTML = ` | ||
<div data-testid="printed-username">${input.value}</div> | ||
`; | ||
div.appendChild(printedUsernameContainer); | ||
}, Math.floor(Math.random() * 200)); | ||
}); | ||
return div; | ||
} | ||
|
||
test('examples of some things', async () => { | ||
const famousWomanInHistory = 'Ada Lovelace'; | ||
const container = getExampleDOM(); | ||
|
||
// Get form elements by their label text. | ||
// An error will be thrown if one cannot be found (accessibility FTW!) | ||
const input = getByLabelText(container, 'Username'); | ||
input.value = famousWomanInHistory; | ||
|
||
// Get elements by their text, just like a real user does. | ||
getByText(container, 'Print Username').click(); | ||
|
||
await waitFor(() => expect(queryByTestId(container, 'printed-username')).toBeTruthy()); | ||
|
||
// getByTestId and queryByTestId are an escape hatch to get elements | ||
// by a test id (could also attempt to get this element by its text) | ||
expect(getByTestId(container, 'printed-username')).toHaveTextContent(famousWomanInHistory); | ||
// jest snapshots work great with regular DOM nodes! | ||
expect(container).toMatchSnapshot(); | ||
test('Renders Button Component as Big', async () => { | ||
render(<Button label='Primary' size={Sizes.BIG} />); | ||
expect(screen.queryByText(/Primary/)).toBeTruthy(); | ||
}); |
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,11 @@ | ||
import Axios from 'axios'; | ||
import { GHUser } from '../../models'; | ||
|
||
export default class UserCardServices { | ||
public endpoint = 'https://api.github.com/users/'; | ||
|
||
async getGithubUserByUsername(username: string): Promise<GHUser> { | ||
const response = await Axios.get(`${this.endpoint}${username}`); | ||
return new GHUser(response.data); | ||
} | ||
} |
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,25 @@ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { render, screen } from '@testing-library/react'; | ||
import axios from 'axios'; | ||
import UserCard from './UserCard'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
const GetUserData = (options: Record<string, unknown>) => ({ | ||
id: 'foobar', | ||
avatar_url: 'https://avatars0.githubusercontent.com/u/23042052', | ||
html_url: 'https://github.com/alcmoraes', | ||
name: 'Alexandre', | ||
location: 'Florianópolis - SC | Brazil', | ||
...options, | ||
}); | ||
|
||
jest.mock('axios'); | ||
|
||
test('Renders UserCard', async () => { | ||
const promise = Promise.resolve({ data: GetUserData({ name: 'Rogério' }) }); | ||
(axios.get as jest.Mock).mockImplementationOnce(() => promise); | ||
render(<UserCard username='' />); | ||
await act(() => promise); | ||
expect(screen.queryByText('Rogério')).toHaveClass('MuiTypography-h5'); | ||
}); |
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
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
Oops, something went wrong.