Skip to content

Commit

Permalink
docs: basic cookbook docs and app (#1622)
Browse files Browse the repository at this point in the history
* docs: basic cookbook docs and app

* chore: add tests

* chore: enable example apps

* chore: fix typecheck

* chore: add lint & fix issues

* docs: tweaks

* docs: tweaks

* docs: tweaks

* chore: tweaks
  • Loading branch information
mdjastrzebski authored May 28, 2024
1 parent e418b6b commit 723c8e2
Show file tree
Hide file tree
Showing 28 changed files with 11,012 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/example-apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
test-example:
strategy:
matrix:
example: [basic]
example: [basic, cookbook]

name: Test Example
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions examples/cookbook/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test-utils.*
3 changes: 3 additions & 0 deletions examples/cookbook/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@callstack"
}
4 changes: 4 additions & 0 deletions examples/cookbook/.expo-shared/assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"12bb71342c6255bbf50437ec8f4441c083f47cdb74bd89160c15e4f43e52a1cb": true,
"40b842e832070c58deac6aa9e08fa459302ee3f9da492c7e77d93d2fbf4a56fd": true
}
25 changes: 25 additions & 0 deletions examples/cookbook/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# General Node.js
node_modules/
.expo/
dist/
npm-debug.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision
*.orig.*
web-build/

# Yarn 4.x
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# macOS
.DS_Store

8 changes: 8 additions & 0 deletions examples/cookbook/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as React from 'react';
import { View } from 'react-native';

const App = () => {
return <View />;
};

export default App;
8 changes: 8 additions & 0 deletions examples/cookbook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# RNTL Cookbook

This example app gathers recipes from the [RNTL Cookbook](https://callstack.github.io/react-native-testing-library/cookbook).

Each recipe described in the Cookbook should have a corresponding code example in this repo.

Note:
Since examples will showcase usage of different dependencies, the dependencies in `package.json` fill will grow much larger that in an normal React Native. This is fine 🐶☕️🔥.
31 changes: 31 additions & 0 deletions examples/cookbook/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"expo": {
"name": "RNTL Cookbook App",
"slug": "rntl-cookbook",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"userInterfaceStyle": "light",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#FFFFFF"
}
},
"web": {
"favicon": "./assets/favicon.png"
}
}
}
Binary file added examples/cookbook/assets/adaptive-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/cookbook/assets/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/cookbook/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/cookbook/assets/splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions examples/cookbook/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function (api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};
24 changes: 24 additions & 0 deletions examples/cookbook/custom-render/WelcomeScreen.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as React from 'react';
import { screen } from '@testing-library/react-native';
import { renderWithProviders } from './test-utils';
import { WelcomeScreen } from './WelcomeScreen';

test('renders WelcomeScreen in light theme', () => {
renderWithProviders(<WelcomeScreen />, { theme: 'light' });
expect(screen.getByText('Theme: light')).toBeOnTheScreen();
});

test('renders WelcomeScreen in dark theme', () => {
renderWithProviders(<WelcomeScreen />, { theme: 'dark' });
expect(screen.getByText('Theme: dark')).toBeOnTheScreen();
});

test('renders WelcomeScreen with user', () => {
renderWithProviders(<WelcomeScreen />, { user: { name: 'Jar-Jar' } });
expect(screen.getByText(/hello Jar-Jar/i)).toBeOnTheScreen();
});

test('renders WelcomeScreen without user', () => {
renderWithProviders(<WelcomeScreen />, { user: null });
expect(screen.getByText(/hello stranger/i)).toBeOnTheScreen();
});
16 changes: 16 additions & 0 deletions examples/cookbook/custom-render/WelcomeScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from 'react';
import { View, Text } from 'react-native';
import { useUser } from './providers/user-provider';
import { useTheme } from './providers/theme-provider';

export function WelcomeScreen() {
const theme = useTheme();
const user = useUser();

return (
<View>
<Text>Hello {user ? user.name : 'Stranger'}</Text>
<Text>Theme: {theme}</Text>
</View>
);
}
13 changes: 13 additions & 0 deletions examples/cookbook/custom-render/providers/theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as React from 'react';

export type Theme = 'light' | 'dark';
export const ThemeProvider = React.createContext<Theme | undefined>(undefined);

export function useTheme() {
const theme = React.useContext(ThemeProvider);
if (theme === undefined) {
throw new Error('useTheme must be used within a ThemeProvider');
}

return theme;
}
8 changes: 8 additions & 0 deletions examples/cookbook/custom-render/providers/user-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as React from 'react';

export type User = { name: string };
export const UserProvider = React.createContext<User | null>(null);

export function useUser() {
return React.useContext(UserProvider);
}
20 changes: 20 additions & 0 deletions examples/cookbook/custom-render/test-utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';
import { render } from '@testing-library/react-native';
import { User, UserProvider } from './providers/user-provider';
import { Theme, ThemeProvider } from './providers/theme-provider';

interface RenderWithProvidersProps {
user?: User | null;
theme?: Theme;
}

export function renderWithProviders<T>(
ui: React.ReactElement<T>,
options?: RenderWithProvidersProps,
) {
return render(
<UserProvider.Provider value={options?.user ?? null}>
<ThemeProvider.Provider value={options?.theme ?? 'light'}>{ui}</ThemeProvider.Provider>
</UserProvider.Provider>,
);
}
7 changes: 7 additions & 0 deletions examples/cookbook/jest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable no-undef, import/no-extraneous-dependencies */

// Import built-in Jest matchers
import '@testing-library/react-native/extend-expect';

// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
5 changes: 5 additions & 0 deletions examples/cookbook/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: '@testing-library/react-native',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
setupFilesAfterEnv: ['./jest-setup.ts'],
};
34 changes: 34 additions & 0 deletions examples/cookbook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject",
"test": "jest",
"lint": "eslint .",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"expo": "^50.0.4",
"expo-status-bar": "~1.11.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.73.2",
"react-native-web": "~0.19.6"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@testing-library/react-native": "^12.4.0",
"@types/eslint": "^8.56.10",
"@types/jest": "^29.5.12",
"@types/react": "~18.2.45",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"react-test-renderer": "18.2.0",
"typescript": "^5.3.0"
},
"private": true,
"packageManager": "[email protected]"
}
7 changes: 7 additions & 0 deletions examples/cookbook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"strict": true,
"allowJs": false
}
}
Loading

0 comments on commit 723c8e2

Please sign in to comment.