-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement jest mocks for react-native. (#535)
**Requirements** - [x] I have added test coverage for new or changed functionality - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [x] I have validated my changes against all supported platform versions **Related issues** Provide links to any issues in this repository or elsewhere relating to this pull request. **Describe the solution you've provided** Provide a clear and concise description of what you expect to happen. **Describe alternatives you've considered** Provide a clear and concise description of any alternative solutions or features you've considered. **Additional context** Add any other context about the pull request here. --------- Co-authored-by: “Menelik <“[email protected]”> Co-authored-by: Ryan Lamb <[email protected]>
- Loading branch information
1 parent
008dcf0
commit aee09c8
Showing
24 changed files
with
456 additions
and
10 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
38 changes: 38 additions & 0 deletions
38
packages/tooling/jest/example/react-native-example/.gitignore
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,38 @@ | ||
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files | ||
|
||
# dependencies | ||
node_modules/ | ||
|
||
# Expo | ||
.expo/ | ||
dist/ | ||
web-build/ | ||
|
||
# Native | ||
*.orig.* | ||
*.jks | ||
*.p8 | ||
*.p12 | ||
*.key | ||
*.mobileprovision | ||
|
||
# Metro | ||
.metro-health-check* | ||
|
||
# debug | ||
npm-debug.* | ||
yarn-debug.* | ||
yarn-error.* | ||
|
||
# macOS | ||
.DS_Store | ||
*.pem | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
|
||
# vscode | ||
.vscode |
36 changes: 36 additions & 0 deletions
36
packages/tooling/jest/example/react-native-example/App.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { StyleSheet } from 'react-native'; | ||
import { | ||
AutoEnvAttributes, | ||
LDProvider, | ||
ReactNativeLDClient, | ||
LDOptions, | ||
} from '@launchdarkly/react-native-client-sdk'; | ||
import Welcome from './src/welcome'; | ||
|
||
const options: LDOptions = { | ||
debug: true, | ||
} | ||
//TODO Set MOBILE_KEY in .env file to a mobile key in your project/environment. | ||
const MOBILE_KEY = 'YOUR_MOBILE_KEY'; | ||
const featureClient = new ReactNativeLDClient(MOBILE_KEY, AutoEnvAttributes.Enabled, options); | ||
|
||
const userContext = { kind: 'user', key: '', anonymous: true }; | ||
|
||
export default function App() { | ||
featureClient.identify(userContext).catch((e: any) => console.log(e)); | ||
|
||
return ( | ||
<LDProvider client={featureClient}> | ||
<Welcome /> | ||
</LDProvider> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
backgroundColor: '#fff', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/tooling/jest/example/react-native-example/app.json
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,29 @@ | ||
{ | ||
"expo": { | ||
"name": "react-native-jest-example", | ||
"slug": "react-native-jest-example", | ||
"version": "0.0.1", | ||
"orientation": "portrait", | ||
"icon": "./assets/icon.png", | ||
"userInterfaceStyle": "light", | ||
"splash": { | ||
"image": "./assets/splash.png", | ||
"resizeMode": "contain", | ||
"backgroundColor": "#ffffff" | ||
}, | ||
"ios": { | ||
"supportsTablet": true, | ||
"bundleIdentifier": "com.anonymous.reactnativejestexample" | ||
}, | ||
"android": { | ||
"adaptiveIcon": { | ||
"foregroundImage": "./assets/adaptive-icon.png", | ||
"backgroundColor": "#ffffff" | ||
}, | ||
"package": "com.anonymous.reactnativejestexample" | ||
}, | ||
"web": { | ||
"favicon": "./assets/favicon.png" | ||
} | ||
} | ||
} |
Binary file added
BIN
+17.1 KB
packages/tooling/jest/example/react-native-example/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
BIN
+1.43 KB
packages/tooling/jest/example/react-native-example/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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions
7
packages/tooling/jest/example/react-native-example/babel.config.js
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,7 @@ | ||
module.exports = function (api) { | ||
api.cache(true); | ||
return { | ||
presets: ['babel-preset-expo', '@babel/preset-typescript'], | ||
|
||
}; | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/tooling/jest/example/react-native-example/index.js
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,10 @@ | ||
// We have to use a custom entrypoint for monorepo workspaces to work. | ||
// https://docs.expo.dev/guides/monorepos/#change-default-entrypoint | ||
import { registerRootComponent } from 'expo'; | ||
|
||
import App from './App'; | ||
|
||
// registerRootComponent calls AppRegistry.registerComponent('main', () => App); | ||
// It also ensures that whether you load the app in Expo Go or in a native build, | ||
// the environment is set up appropriately | ||
registerRootComponent(App); |
4 changes: 4 additions & 0 deletions
4
packages/tooling/jest/example/react-native-example/jest.config.js
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,4 @@ | ||
module.exports = { | ||
preset: 'jest-expo', | ||
setupFiles: ['@launchdarkly/jest/react-native'], | ||
}; |
28 changes: 28 additions & 0 deletions
28
packages/tooling/jest/example/react-native-example/metro.config.js
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,28 @@ | ||
// We need to use a custom metro config for monorepo workspaces to work. | ||
// https://docs.expo.dev/guides/monorepos/#modify-the-metro-config | ||
/** | ||
* @type {import('expo/metro-config')} | ||
*/ | ||
const { getDefaultConfig } = require('expo/metro-config'); | ||
const path = require('path'); | ||
|
||
// Find the project and workspace directories | ||
const projectRoot = __dirname; | ||
|
||
const findWorkspaceRoot = require('find-yarn-workspace-root'); | ||
|
||
const workspaceRoot = findWorkspaceRoot(__dirname); // Absolute path or null | ||
|
||
const config = getDefaultConfig(projectRoot); | ||
|
||
// 1. Watch all files within the monorepo | ||
config.watchFolders = [workspaceRoot]; | ||
// 2. Let Metro know where to resolve packages and in what order | ||
config.resolver.nodeModulesPaths = [ | ||
path.resolve(projectRoot, 'node_modules'), | ||
path.resolve(workspaceRoot, 'node_modules'), | ||
]; | ||
// 3. Force Metro to resolve (sub)dependencies only from the `nodeModulesPaths` | ||
config.resolver.disableHierarchicalLookup = true; | ||
|
||
module.exports = config; |
40 changes: 40 additions & 0 deletions
40
packages/tooling/jest/example/react-native-example/package.json
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 @@ | ||
{ | ||
"name": "react-native-jest-example", | ||
"version": "0.0.1", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "expo start", | ||
"android": "expo run:android", | ||
"ios": "expo run:ios", | ||
"test": "jest", | ||
"clean": "rm -rf node_modules && rm -rf package-lock.json && rm -rf yarn.lock" | ||
}, | ||
"dependencies": { | ||
"@launchdarkly/react-native-client-sdk": "^10.9.0", | ||
"expo": "^51.0.26", | ||
"expo-status-bar": "~1.12.1", | ||
"find-yarn-workspace-root": "^2.0.0", | ||
"react": "^18.2.0", | ||
"react-native": "0.74.5" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.20.0", | ||
"@babel/preset-env": "^7.25.4", | ||
"@babel/preset-react": "^7.24.7", | ||
"@babel/runtime": "^7.25.4", | ||
"@launchdarkly/jest": "workspace:^", | ||
"@react-native/babel-preset": "^0.75.2", | ||
"@testing-library/react-native": "^12.6.1", | ||
"@types/jest": "^29.5.12", | ||
"@types/react": "~18.2.45", | ||
"jest": "^29.7.0", | ||
"jest-expo": "^51.0.4", | ||
"react-test-renderer": "^18.2.0", | ||
"typescript": "^5.1.3" | ||
}, | ||
"packageManager": "[email protected]", | ||
"installConfig": { | ||
"hoistingLimits": "workspaces" | ||
}, | ||
"private": true | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/tooling/jest/example/react-native-example/src/welcome.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { mockFlags, resetLDMocks } from '@launchdarkly/jest/react-native'; | ||
import { screen, render } from '@testing-library/react-native'; | ||
import { useLDClient } from '@launchdarkly/react-native-client-sdk'; | ||
import Welcome from './welcome'; | ||
|
||
describe('Welcome component test', () => { | ||
|
||
afterEach(() => { | ||
resetLDMocks(); | ||
}); | ||
|
||
test('mock boolean flag correctly', () => { | ||
mockFlags({ 'my-boolean-flag': true }); | ||
render(<Welcome />); | ||
expect(screen.getByText('Flag value is true')).toBeTruthy(); | ||
}); | ||
|
||
test('mock ldClient correctly', () => { | ||
const current = useLDClient(); | ||
|
||
current?.track('event'); | ||
expect(current.track).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
}); |
25 changes: 25 additions & 0 deletions
25
packages/tooling/jest/example/react-native-example/src/welcome.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { StyleSheet, Text, View } from 'react-native'; | ||
import { useLDClient } from '@launchdarkly/react-native-client-sdk'; | ||
|
||
export default function Welcome() { | ||
|
||
const ldClient = useLDClient(); | ||
|
||
const flagValue = ldClient.boolVariation('my-boolean-flag', false); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<Text>Welcome to LaunchDarkly</Text> | ||
<Text>Flag value is {`${flagValue}`}</Text> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
backgroundColor: '#fff', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/tooling/jest/example/react-native-example/tsconfig.eslint.json
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,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": [ | ||
"/**/*.ts", | ||
"/**/*.tsx", | ||
"/*.js", | ||
"/*.tsx" | ||
], | ||
"exclude": ["node_modules"] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/tooling/jest/example/react-native-example/tsconfig.json
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,8 @@ | ||
{ | ||
"extends": "expo/tsconfig.base", | ||
"compilerOptions": { | ||
"strict": true, | ||
"moduleResolution": "bundler", | ||
"jsx": "react-jsx" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,72 @@ | ||
import { | ||
ldClientMock, | ||
mockFlags, | ||
mockLDProvider, | ||
mockReactNativeLDClient, | ||
mockUseLDClient, | ||
resetLDMocks, | ||
} from '.'; | ||
|
||
describe('react-native', () => { | ||
test.todo('Add react-native tests'); | ||
afterEach(() => { | ||
resetLDMocks(); | ||
}); | ||
|
||
test('reset LD Mocks', () => { | ||
const current = mockUseLDClient(); | ||
|
||
current?.track('event'); | ||
expect(ldClientMock.track).toHaveBeenCalledTimes(1); | ||
|
||
resetLDMocks(); | ||
expect(ldClientMock.track).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('mock boolean flag correctly', () => { | ||
mockFlags({ 'bool-flag': true }); | ||
expect(ldClientMock.boolVariation).toBeDefined(); | ||
}); | ||
|
||
test('mock number flag correctly', () => { | ||
mockFlags({ 'number-flag': 42 }); | ||
expect(ldClientMock.numberVariation).toBeDefined(); | ||
}); | ||
|
||
test('mock string flag correctly', () => { | ||
mockFlags({ 'string-flag': 'hello' }); | ||
expect(ldClientMock.stringVariation).toBeDefined(); | ||
}); | ||
|
||
test('mock json flag correctly', () => { | ||
mockFlags({ 'json-flag': { key: 'value' } }); | ||
expect(ldClientMock.jsonVariation).toBeDefined(); | ||
}); | ||
|
||
test('mock LDProvider correctly', () => { | ||
expect(mockLDProvider).toBeDefined(); | ||
}); | ||
|
||
test('mock ReactNativeLDClient correctly', () => { | ||
expect(mockReactNativeLDClient).toBeDefined(); | ||
}); | ||
|
||
test('mock ldClient correctly', () => { | ||
const current = mockUseLDClient(); | ||
|
||
current?.track('event'); | ||
expect(ldClientMock.track).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('mock ldClient complete set of methods correctly', () => { | ||
expect(ldClientMock.identify).toBeDefined(); | ||
expect(ldClientMock.allFlags.mock).toBeDefined(); | ||
expect(ldClientMock.close.mock).toBeDefined(); | ||
expect(ldClientMock.flush).toBeDefined(); | ||
expect(ldClientMock.getContext.mock).toBeDefined(); | ||
expect(ldClientMock.off.mock).toBeDefined(); | ||
expect(ldClientMock.on.mock).toBeDefined(); | ||
expect(ldClientMock.track.mock).toBeDefined(); | ||
expect(ldClientMock.variation.mock).toBeDefined(); | ||
expect(ldClientMock.variationDetail.mock).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.