Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 0d0f07e

Browse files
committed
chore(test-app): add TypeScript tests
1 parent 3f1eb3c commit 0d0f07e

File tree

7 files changed

+129
-15
lines changed

7 files changed

+129
-15
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { Analytics } from './analytics'
22

33
export default new Analytics.Client()
4+
export { Analytics }

packages/test-app/e2e/main.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if (CIRCLE_WORKFLOW_ID) {
2020
await element(by.text('Flush')).tap()
2121

2222
await Promise.all(
23-
require('../calls.json').map(([type, ...args]) =>
23+
require('../src/calls.json').map(([type, ...args]) =>
2424
hasMatchingCall(type, ...args)
2525
)
2626
)

packages/test-app/generate.sh

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,27 @@ rm -rf project
1010

1111
yarn react-native init project
1212

13-
cp -r App.js calls.json project/
13+
cp -r src/* project/
1414
cd project
15+
rm App.js
16+
17+
sed -i -e "s/SEGMENT_WRITE_TOKEN/$SEGMENT_WRITE_TOKEN/g" App.tsx
18+
sed -i -e "s/CIRCLE_WORKFLOW_ID/$CIRCLE_WORKFLOW_ID/g" App.tsx
1519

1620
build_dir=$pwd/../integrations/build
21+
counter=0
1722

1823
for integration in `cd $build_dir && echo @segment/*`; do
24+
counter=$((counter+1))
1925
install_command+=" file:$build_dir/$integration"
20-
integrations_require+="require('$integration'),"
26+
integrations_require+="import integration_$counter from '$integration';"
27+
integrations_require+="SEGMENT_INTEGRATIONS.push(integration_$counter);"
2128
done
2229

23-
yarn add $install_command @babel/runtime
30+
echo -e $integrations_require >> App.tsx
2431

25-
cat << EOF >> App.js
26-
buildId = '$CIRCLE_WORKFLOW_ID'
27-
28-
analytics
29-
.setup('$SEGMENT_WRITE_TOKEN', {
30-
using: [$integrations_require],
31-
debug: true
32-
})
33-
.then(() => console.log('Analytics ready'))
34-
.catch(err => console.error('Analytics error', err))
35-
EOF
32+
yarn add $install_command @babel/runtime
33+
yarn add typescript react-native-typescript-transformer @types/{react,react-native} --dev
34+
yarn tsc
3635

3736
../android-workaround.js

packages/test-app/src/App.tsx

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import React, { Component } from 'react'
2+
import { StyleSheet, Text, View, Image, TouchableOpacity } from 'react-native'
3+
import analytics, { Analytics } from '@segment/analytics-react-native'
4+
5+
type Call = ['identify' | 'track', string, {}]
6+
7+
const calls: Call[] = require('./calls.json')
8+
9+
const Button = ({ title, onPress }: { title: string; onPress: () => void }) => (
10+
<TouchableOpacity style={styles.button} onPress={onPress}>
11+
<Text style={styles.text}>{title}</Text>
12+
</TouchableOpacity>
13+
)
14+
15+
const screenHome = () => analytics.screen('Home')
16+
17+
const flush = () => analytics.flush()
18+
19+
const pizzaEaten = () => analytics.track('Pizza Eaten')
20+
21+
const trackOrder = () => {
22+
analytics.track('Order Completed')
23+
analytics.track('Order Cancelled', {
24+
order_id: 323
25+
})
26+
analytics.identify('userIdOnly')
27+
analytics.identify('userId', {
28+
age: 32
29+
})
30+
analytics.alias('newlyAliasedId')
31+
analytics.screen('User Login Screen', {
32+
method: 'google'
33+
})
34+
}
35+
36+
const buildId = 'CIRCLE_WORKFLOW_ID'
37+
38+
const testSuite = () =>
39+
calls.forEach(([call, name, props = {}]) =>
40+
analytics[call](name, {
41+
...props,
42+
buildId
43+
})
44+
)
45+
46+
export default class App extends Component {
47+
render() {
48+
return (
49+
<View style={styles.container}>
50+
<Image
51+
source={{ uri: 'https://i.imgur.com/GrCdId0.png' }}
52+
resizeMode="contain"
53+
style={{
54+
margin: 50,
55+
width: 240,
56+
height: 160
57+
}}
58+
/>
59+
<Button title="Screen: Home" onPress={screenHome} />
60+
<Button title="Track: Order Complete" onPress={trackOrder} />
61+
<Button title="Flush" onPress={flush} />
62+
<Button title="Track: Pizza Eaten" onPress={pizzaEaten} />
63+
<Button title="Launch test suite" onPress={testSuite} />
64+
</View>
65+
)
66+
}
67+
}
68+
69+
const styles = StyleSheet.create({
70+
button: {
71+
margin: 20
72+
},
73+
text: {
74+
color: '#FBFAF9'
75+
},
76+
container: {
77+
flex: 1,
78+
justifyContent: 'flex-start',
79+
alignItems: 'center',
80+
backgroundColor: '#32A75D'
81+
}
82+
})
83+
84+
const SEGMENT_INTEGRATIONS: Analytics.Integration[] = []
85+
86+
setTimeout(() => {
87+
analytics
88+
.setup('SEGMENT_WRITE_TOKEN', {
89+
debug: true,
90+
using: SEGMENT_INTEGRATIONS
91+
})
92+
.then(() => console.log('Analytics ready'))
93+
.catch(err => console.error(err))
94+
})
File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
getTransformModulePath() {
3+
return require.resolve('react-native-typescript-transformer')
4+
},
5+
getSourceExts() {
6+
return ['ts', 'tsx']
7+
}
8+
}

packages/test-app/src/tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../../tsconfig.json",
3+
"compilerOptions": {
4+
"jsx": "react-native",
5+
"allowSyntheticDefaultImports": true,
6+
"esModuleInterop": true,
7+
"outDir": "build",
8+
"lib": ["es2015"],
9+
"typeRoots": ["node_modules/@types"]
10+
},
11+
"include": ["./App.tsx"]
12+
}

0 commit comments

Comments
 (0)