-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This follows the template that [we previously coded in react-native-templates](https://github.com/thoughtbot/react-native-templates/pull/1/files#diff-fc130cd2eebfc7cbeb229f509cb090c6fb5837a169b9270709cfa73ff46c9a56). * Installs React Query * Installs testing/mocking utility, MSW * Creates a mocking and testing strategy * Adds an example API call and mock and test For now, the "create" command automatically uses React Query, but we will likely decide to prompt the user if they'd like to use this or Apollo (for GraphQL) in the future. Co-authored-by: Frida Casas <frida@thoughtbot.com>
- Loading branch information
Showing
20 changed files
with
448 additions
and
61 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
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,22 @@ | ||
import addDependency from '../util/addDependency'; | ||
import appendToFile from '../util/appendToFile'; | ||
import copyTemplateDirectory from '../util/copyTemplateDirectory'; | ||
import prependToFile from '../util/prependToFile'; | ||
|
||
export default async function addReactQuery() { | ||
await addDependency('@tanstack/react-query axios msw'); | ||
await copyTemplateDirectory({ templateDir: 'reactQuery' }); | ||
await prependToFile('jest.setup.js', "import server from 'src/test/server';"); | ||
await appendToFile( | ||
'jest.setup.js', | ||
` | ||
// listen with MSW server. Individual tests can pass mocks to 'render' function | ||
beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); | ||
afterAll(() => server.close()); | ||
beforeEach(() => { | ||
server.resetHandlers() | ||
}); | ||
`, | ||
); | ||
} |
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,17 @@ | ||
import { fs, vol } from 'memfs'; | ||
import { expect, test } from 'vitest'; | ||
import prependToFile from '../prependToFile'; | ||
|
||
test('prepends contents', async () => { | ||
const json = { | ||
'package.json': '1', | ||
'src/myFile.txt': 'hello world', | ||
}; | ||
|
||
vol.fromJSON(json, './'); | ||
|
||
await prependToFile('src/myFile.txt', 'prepended\ntwo'); | ||
expect(fs.readFileSync('src/myFile.txt', 'utf8')).toMatch( | ||
'prepended\ntwo\nhello world', | ||
); | ||
}); |
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,12 +1,8 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import getProjectDir from './getProjectDir'; | ||
import appendToFile from './appendToFile'; | ||
|
||
/** | ||
* lines should be separated by newlines | ||
*/ | ||
export default async function addToGitignore(lines: string) { | ||
return fs.appendFile( | ||
path.join(await getProjectDir(), '.gitignore'), | ||
`\n${lines}`, | ||
); | ||
return appendToFile('.gitignore', lines); | ||
} |
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,12 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import getProjectDir from './getProjectDir'; | ||
/** | ||
* lines should be separated by newlines | ||
*/ | ||
export default async function appendToFile(filename: string, lines: string) { | ||
return fs.appendFile( | ||
path.join(await getProjectDir(), filename), | ||
`\n${lines}`, | ||
); | ||
} |
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,12 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import getProjectDir from './getProjectDir'; | ||
|
||
/** | ||
* lines should be separated by newlines | ||
*/ | ||
export default async function prependToFile(filename: string, lines: string) { | ||
const fullFilename = path.join(await getProjectDir(), filename); | ||
const contents = await fs.readFile(fullFilename, 'utf8'); | ||
return fs.writeFile(fullFilename, `${lines}\n${contents}`); | ||
} |
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,8 +1,23 @@ | ||
import { screen } from '@testing-library/react-native'; | ||
import RootNavigator from 'src/navigators/RootNavigator'; | ||
import render from 'src/test/render'; | ||
import mock from 'src/test/mock'; | ||
import { renderApplication } from 'src/test/render'; | ||
|
||
test('renders', async () => { | ||
render(<RootNavigator />); | ||
expect(await screen.findByText(/Open up App.tsx/)).toBeDefined(); | ||
const mocks = [mockCoffees()]; | ||
|
||
renderApplication({ mocks }); | ||
|
||
expect(await screen.findByRole('header', { name: 'Mocha' })).toBeDefined(); | ||
}); | ||
|
||
function mockCoffees() { | ||
return mock.get('coffee/hot', { | ||
response: [ | ||
{ | ||
id: 1, | ||
title: 'Mocha', | ||
image: 'htps://placehold.it/200x200', | ||
}, | ||
], | ||
}); | ||
} |
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,44 @@ | ||
import { NavigationContainer } from '@react-navigation/native'; | ||
import { QueryClientProvider } from '@tanstack/react-query'; | ||
import { | ||
RenderAPI, | ||
// eslint-disable-next-line no-restricted-imports | ||
render as TestingLibraryRender, | ||
} from '@testing-library/react-native'; | ||
import { RequestHandler } from 'msw'; | ||
import { ReactElement } from 'react'; | ||
import Providers, { Provider } from 'src/components/Providers'; | ||
import RootNavigator from 'src/navigators/RootNavigator'; | ||
import queryClient from 'src/util/api/queryClient'; | ||
import server from './server'; | ||
|
||
export type RenderOptions = { | ||
mocks?: Array<RequestHandler>; | ||
}; | ||
|
||
// TODO: this will become customized as the codebase progresses, so our | ||
// tests can be wrapped with appropriate providers, mocks can be supplied, etc | ||
export default function render( | ||
element: ReactElement, | ||
{ mocks }: RenderOptions = {}, | ||
): RenderAPI { | ||
if (mocks) { | ||
server.use(...mocks); | ||
} | ||
|
||
const providers: Provider[] = [ | ||
(children) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
), | ||
(children) => <NavigationContainer>{children}</NavigationContainer>, | ||
// CODEGEN:BELT:PROVIDERS - do not remove | ||
]; | ||
|
||
return TestingLibraryRender( | ||
<Providers providers={providers}>{element}</Providers>, | ||
); | ||
} | ||
|
||
export function renderApplication(options: RenderOptions = {}) { | ||
return render(<RootNavigator />, options); | ||
} |
Oops, something went wrong.