Skip to content

Commit 107b511

Browse files
illuminati1911jokerttu
authored andcommitted
test: refactoring tests and added map init test
1 parent 39144b5 commit 107b511

File tree

8 files changed

+218
-84
lines changed

8 files changed

+218
-84
lines changed

example/e2e/initialization.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {
18+
initializeIntegrationTestsPage,
19+
selectTestByName,
20+
waitForTestToFinish,
21+
} from './shared.js';
22+
import { element, by, log } from 'detox';
23+
24+
describe('Initialization tests', () => {
25+
beforeEach(async () => {
26+
await initializeIntegrationTestsPage();
27+
});
28+
29+
it('T01 - initialize map and test default values', async () => {
30+
await selectTestByName('testMapInitialization');
31+
// await waitForStepNumber(1);
32+
// await agreeToTermsAndConditions();
33+
await waitForTestToFinish();
34+
const failureMessageLabel = element(by.id('failure_message_label'));
35+
const attributes = await failureMessageLabel.getAttributes();
36+
log.error(attributes.text);
37+
await expect(element(by.id('failure_message_label'))).toHaveText('');
38+
await expect(element(by.id('test_result_label'))).toHaveText(
39+
'Test result: Success'
40+
);
41+
});
42+
});

example/e2e/navigation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from './shared.js';
2424
import { element, by } from 'detox';
2525

26-
describe('Initialization tests', () => {
26+
describe('Navigation tests', () => {
2727
beforeEach(async () => {
2828
await initializeIntegrationTestsPage();
2929
});

example/e2e/shared.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ export const waitForTestToFinish = async () => {
6161

6262
export const initializeIntegrationTestsPage = async () => {
6363
await device.launchApp({ newInstance: true });
64-
await element(by.text('Integration Tests')).tap();
64+
await element(by.id('integration_tests_button')).tap();
6565
};
6666

6767
export const selectTestByName = async name => {
68-
await element(by.text('Tests')).tap();
69-
await element(by.text(name)).tap();
68+
await element(by.id('tests_menu_button')).tap();
69+
await element(by.id(name)).tap();
7070
};

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"build:android": "cd android && ./gradlew assembleDebug --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a",
1212
"build:ios": "cd ios && xcodebuild -workspace SampleApp.xcworkspace -scheme SampleApp -configuration Debug -sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO",
1313
"test:android": "DETOX_CONFIGURATION=android.emu.debug npx jest --config e2e/jest.config.js",
14-
"test:ios": "DETOX_CONFIGURATION=ios.sim.debug npx jest --config e2e/jest.config.js"
14+
"test:ios": "DETOX_CONFIGURATION=ios.sim.debug npx jest --config e2e/jest.config.js --verbose"
1515
},
1616
"dependencies": {
1717
"@react-navigation/native": "^6.1.18",

example/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const HomeScreen = () => {
6363
<View style={styles.buttonContainer}>
6464
<Button
6565
title="Integration Tests"
66+
testID="integration_tests_button"
6667
onPress={() => isFocused && navigate('Integration tests')}
6768
/>
6869
</View>

example/src/screens/IntegrationTestsScreen.tsx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import {
3333
import styles from '../styles';
3434
import OverlayModal from '../helpers/overlayModal';
3535
import {
36-
runTest2,
36+
testMapInitialization,
3737
runTest3,
3838
testNavigationSessionInitialization,
39-
} from './integration_tests/navigation_test';
39+
} from './integration_tests/integration_test';
4040

4141
// Utility function for showing Snackbar
4242
const showSnackbar = (text: string, duration = Snackbar.LENGTH_SHORT) => {
@@ -68,6 +68,7 @@ const IntegrationTestsScreen = () => {
6868
const { navigationController, addListeners, removeListeners } =
6969
useNavigation();
7070
const [detoxStepNumber, setDetoxStepNumber] = useState(0);
71+
const [failureMessage, setFailuremessage] = useState('');
7172

7273
const onMapReady = useCallback(async () => {
7374
console.log('Map is ready, initializing navigator...');
@@ -113,9 +114,10 @@ const IntegrationTestsScreen = () => {
113114
setTestResult(TestResult.Success);
114115
};
115116

116-
const failTest = () => {
117+
const failTest = (message: string) => {
117118
setTestStatus(TestRunStatus.Finished);
118119
setTestResult(TestResult.Failure);
120+
setFailuremessage(message);
119121
};
120122

121123
const setDetoxStep = (stepNumber: number) => {
@@ -128,16 +130,28 @@ const IntegrationTestsScreen = () => {
128130
setTestResult(TestResult.None);
129131
setTestStatus(TestRunStatus.NotRunning);
130132
setDetoxStepNumber(0);
133+
setFailuremessage('');
134+
};
135+
136+
const expectFalseError = (expectation: string) => {
137+
failTest(`Expected ${expectation} to be false but it was true`);
138+
};
139+
140+
const expectTrueError = (expectation: string) => {
141+
failTest(`Expected ${expectation} to be true but it was false`);
131142
};
132143

133144
const getTestTools = () => {
134145
return {
135146
navigationController,
147+
mapViewController,
136148
addListeners,
137149
removeListeners,
138150
passTest,
139151
failTest,
140152
setDetoxStep,
153+
expectFalseError,
154+
expectTrueError,
141155
};
142156
};
143157

@@ -149,8 +163,8 @@ const IntegrationTestsScreen = () => {
149163
case 'testNavigationSessionInitialization':
150164
await testNavigationSessionInitialization(getTestTools());
151165
break;
152-
case 'test2':
153-
await runTest2();
166+
case 'testMapInitialization':
167+
await testMapInitialization(getTestTools());
154168
break;
155169
case 'test3':
156170
await runTest3();
@@ -180,6 +194,7 @@ const IntegrationTestsScreen = () => {
180194
<Text>Selected testId: {activeTestId}</Text>
181195
<Text testID="test_status_label">Test status: {testStatusString}</Text>
182196
<Text testID="test_result_label">Test result: {testResult}</Text>
197+
<Text testID="failure_message_label">{failureMessage}</Text>
183198
<Button
184199
title="Reset"
185200
onPress={() => {
@@ -190,6 +205,7 @@ const IntegrationTestsScreen = () => {
190205
<View style={styles.controlButtons}>
191206
<Button
192207
title="Tests"
208+
testID="tests_menu_button"
193209
onPress={() => {
194210
setIsOverlayOpen(true);
195211
}}
@@ -203,18 +219,21 @@ const IntegrationTestsScreen = () => {
203219
>
204220
<Button
205221
title="testNavigationSessionInitialization"
222+
testID="testNavigationSessionInitialization"
206223
onPress={() => {
207224
runTest('testNavigationSessionInitialization');
208225
}}
209226
/>
210227
<Button
211-
title="Test2"
228+
title="testMapInitialization"
229+
testID="testMapInitialization"
212230
onPress={() => {
213-
runTest('test2');
231+
runTest('testMapInitialization');
214232
}}
215233
/>
216234
<Button
217235
title="Test3"
236+
testID="Test3"
218237
onPress={() => {
219238
runTest('test3');
220239
}}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import type {
18+
MapViewController,
19+
NavigationCallbacks,
20+
NavigationController,
21+
NavigationInitErrorCode,
22+
} from '@googlemaps/react-native-navigation-sdk';
23+
24+
interface TestTools {
25+
navigationController: NavigationController;
26+
mapViewController: MapViewController | null;
27+
addListeners: (listeners: Partial<NavigationCallbacks>) => void;
28+
removeListeners: (listeners: Partial<NavigationCallbacks>) => void;
29+
passTest: () => void;
30+
failTest: (message: string) => void;
31+
setDetoxStep: (stepNumber: number) => void;
32+
expectFalseError: (expectation: string) => void;
33+
expectTrueError: (expectation: string) => void;
34+
}
35+
36+
export const testNavigationSessionInitialization = async (
37+
testTools: TestTools
38+
) => {
39+
const {
40+
navigationController,
41+
addListeners,
42+
passTest,
43+
failTest,
44+
setDetoxStep,
45+
expectFalseError,
46+
} = testTools;
47+
48+
const checkDefaults = async () => {
49+
if (!(await navigationController.areTermsAccepted())) {
50+
return expectFalseError('navigationController.areTermsAccepted()');
51+
}
52+
passTest();
53+
};
54+
55+
addListeners({
56+
onNavigationReady: () => {
57+
checkDefaults();
58+
},
59+
onNavigationInitError: (errorCode: NavigationInitErrorCode) => {
60+
console.log(errorCode);
61+
failTest('onNavigatonInitError');
62+
},
63+
});
64+
try {
65+
await navigationController.init();
66+
} catch (error) {
67+
console.error('Error initializing navigator', error);
68+
failTest('navigationController.init() exception');
69+
}
70+
// Tell detox to prepare to execute step 1: (confirm t&c dialog)
71+
setDetoxStep(1);
72+
};
73+
74+
const expectFalseMessage = (expectation: string) => {
75+
return `Expected ${expectation} to be false but it was true`;
76+
};
77+
78+
export const testMapInitialization = async (testTools: TestTools) => {
79+
const { mapViewController, passTest, failTest, expectFalseError } = testTools;
80+
if (!mapViewController) {
81+
return failTest('mapViewController was expected to exist');
82+
}
83+
if ((await mapViewController.getUiSettings()).isCompassEnabled) {
84+
return expectFalseError(
85+
'mapViewController.getUiSettings()).isCompassEnabled'
86+
);
87+
}
88+
if ((await mapViewController.getUiSettings()).isMapToolbarEnabled) {
89+
return failTest(
90+
expectFalseMessage(
91+
'mapViewController.getUiSettings()).isMapToolbarEnabled'
92+
)
93+
);
94+
}
95+
if (!(await mapViewController.getUiSettings()).isIndoorLevelPickerEnabled) {
96+
return failTest(
97+
expectFalseMessage(
98+
'mapViewController.getUiSettings()).isIndoorLevelPickerEnabled'
99+
)
100+
);
101+
}
102+
if ((await mapViewController.getUiSettings()).isMapToolbarEnabled) {
103+
return expectFalseError(
104+
'mapViewController.getUiSettings()).isMapToolbarEnabled'
105+
);
106+
}
107+
if (!(await mapViewController.getUiSettings()).isRotateGesturesEnabled) {
108+
return expectFalseError(
109+
'mapViewController.getUiSettings()).isRotateGesturesEnabled'
110+
);
111+
}
112+
if (!(await mapViewController.getUiSettings()).isScrollGesturesEnabled) {
113+
return expectFalseError(
114+
'mapViewController.getUiSettings()).isScrollGesturesEnabled'
115+
);
116+
}
117+
if (
118+
!(await mapViewController.getUiSettings())
119+
.isScrollGesturesEnabledDuringRotateOrZoom
120+
) {
121+
return expectFalseError(
122+
'mapViewController.getUiSettings()).isScrollGesturesEnabledDuringRotateOrZoom'
123+
);
124+
}
125+
if (!(await mapViewController.getUiSettings()).isTiltGesturesEnabled) {
126+
return expectFalseError(
127+
'mapViewController.getUiSettings()).isTiltGesturesEnabled'
128+
);
129+
}
130+
if ((await mapViewController.getUiSettings()).isZoomControlsEnabled) {
131+
return expectFalseError(
132+
'mapViewController.getUiSettings()).isZoomControlsEnabled'
133+
);
134+
}
135+
if (!(await mapViewController.getUiSettings()).isZoomGesturesEnabled) {
136+
return expectFalseError(
137+
'mapViewController.getUiSettings()).isZoomGesturesEnabled'
138+
);
139+
}
140+
141+
passTest();
142+
};
143+
144+
export const runTest3 = async () => {};

0 commit comments

Comments
 (0)