Skip to content

Commit

Permalink
fix test cases and build issue
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshaR1642 committed Aug 21, 2024
1 parent ef8fe56 commit fab963e
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 164 deletions.
3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.6.1.cjs
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import com.android.Version

buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["ReactNativeGeofencing_kotlinVersion"]
def kotlin_version = project.properties["ReactNativeGeofencing_kotlinVersion"]

repositories {
google()
Expand Down
6 changes: 3 additions & 3 deletions example/index.ts → example/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AppRegistry } from 'react-native';
import { App } from './src/App';
import { name as appName } from './app.json';
import { Geofence, Events } from '@rn-bridge/react-native-geofencing';
import Geofencing, { Events } from '@rn-bridge/react-native-geofencing';

Geofence.onEnter((ids: string[]) => {
Geofencing.onEnter((ids) => {
console.log(Events.Enter, ids);
});

Geofence.onExit((ids: string[]) => {
Geofencing.onExit((ids) => {
console.log(Events.Exit, ids);
});

Expand Down
23 changes: 8 additions & 15 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { StyleSheet, View, TouchableOpacity, Text, Alert } from 'react-native';
import {
addGeofence,
removeGeofence,
getRegisteredGeofences,
requestLocation,
getLocationAuthorizationStatus,
getCurrentLocation
} from '@rn-bridge/react-native-geofencing';
import Geofencing from '@rn-bridge/react-native-geofencing';

const Button = ({
title,
Expand Down Expand Up @@ -34,7 +27,7 @@ export const App = () => {
style={styles.button}
title="Request Location While Using"
onPress={async () => {
const response = await requestLocation({ allowWhileUsing: true });
const response = await Geofencing.requestLocation({ allowWhileUsing: true });
console.log('requestLocation:', response);
Alert.alert('', `Location: ${response.location}`);
}}
Expand All @@ -45,7 +38,7 @@ export const App = () => {
style={styles.button}
title="Request Location Always"
onPress={async () => {
const response = await requestLocation({ allowAlways: true });
const response = await Geofencing.requestLocation({ allowAlways: true });
console.log('requestLocation:', response);
Alert.alert('', `Location: ${response.location}`);
}}
Expand All @@ -56,7 +49,7 @@ export const App = () => {
style={styles.button}
title="Get Current Location"
onPress={async () => {
const response = await getCurrentLocation();
const response = await Geofencing.getCurrentLocation();
console.log('getCurrentLocation:', response);
Alert.alert(
'',
Expand All @@ -70,7 +63,7 @@ export const App = () => {
style={styles.button}
title="Add Geo Fence"
onPress={async () => {
const response = await addGeofence({
const response = await Geofencing.addGeofence({
id: 'a9957259-8036-4dcb-974c-34eae9b44bdb',
latitude: 10.9314,
longitude: 76.9781,
Expand All @@ -92,7 +85,7 @@ export const App = () => {
style={styles.button}
title="Remove Geo Fence"
onPress={async () => {
const response = await removeGeofence(
const response = await Geofencing.removeGeofence(
'a9957259-8036-4dcb-974c-34eae9b44bdb'
);

Expand All @@ -111,7 +104,7 @@ export const App = () => {
style={styles.button}
title="Get Registered Geo Fences"
onPress={async () => {
const response = await getRegisteredGeofences();
const response = await Geofencing.getRegisteredGeofences();
console.log('getRegisteredGeofences:', response);
const ids = response.map((id: string, index: number) => `id${index+1}: ${id}`).join("\n")
const message = response.length == 0 ? "No geofence registered" : ids
Expand All @@ -124,7 +117,7 @@ export const App = () => {
style={styles.button}
title="Get Authorization Status"
onPress={async () => {
const response = await getLocationAuthorizationStatus();
const response = await Geofencing.getLocationAuthorizationStatus();
console.log('getLocationAuthorizationStatus:', response);
Alert.alert('', `Location: ${response}`);
}}
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@rn-bridge/react-native-geofencing",
"version": "0.7.0",
"version": "0.8.0",
"description": "Native modules to determine if a location is within defined geographical boundaries",
"source": "./src/index.tsx",
"main": "./lib/commonjs/index.js",
"main": "./src/index.tsx",
"module": "./lib/module/index.js",
"types": "./src/types/index.d.ts",
"files": [
Expand Down Expand Up @@ -114,6 +114,13 @@
{
"esm": true
}
],
[
"typescript",
{
"project": "tsconfig.build.json",
"esm": true
}
]
]
}
Expand Down
74 changes: 17 additions & 57 deletions src/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { NativeEventEmitter, NativeModules } from 'react-native';

const mockedCallbacks: {
onEnter: ((data: string[]) => void)[];
onExit: ((data: string[]) => void)[];
} = {
onEnter: [],
onExit: []
};
import { NativeModules } from 'react-native';
import Geofencing, { Events } from "../"

jest.mock('react-native', () => {
return {
Expand All @@ -19,39 +12,13 @@ jest.mock('react-native', () => {
getRegisteredGeofences: jest.fn(),
removeAllGeofence: jest.fn(),
requestLocation: jest.fn(),
Geofence: {
onEnter: (callback: (data: string[]) => void) => {
mockedCallbacks.onEnter.push(callback);
},
onExit: (callback: (data: string[]) => void) => {
mockedCallbacks.onExit.push(callback);
},
removeOnEnterListener: jest.fn(),
removeOnExitListener: jest.fn()
}
}
},
NativeEventEmitter: jest.fn().mockImplementation(() => {
return {
addListener: jest.fn(),
removeAllListeners: jest.fn(),
emit: (event: string, data: string[]) => {
if (event === 'onEnter') {
mockedCallbacks.onEnter.map(
(callback: (data: string[]) => void) => {
callback(data);
}
);

mockedCallbacks.onEnter = [];
} else if (event === 'onExit') {
mockedCallbacks.onExit.map((callback: (data: string[]) => void) => {
callback(data);
});

mockedCallbacks.onExit = [];
}
},
emit: jest.fn(),
listenerCount: jest.fn().mockReturnValue(1)
};
}),
Expand All @@ -69,7 +36,7 @@ jest.mock('react-native', () => {
},
Platform: {
select: jest.fn(),
OS: 'android' // You can switch to 'ios' for testing iOS specific code
OS: 'android'
},
AppRegistry: {
registerHeadlessTask: jest.fn()
Expand All @@ -78,7 +45,6 @@ jest.mock('react-native', () => {
});

describe('Geofencing Module', () => {
const geofencingEventEmitter = new NativeEventEmitter();

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -100,14 +66,14 @@ describe('Geofencing Module', () => {
NativeModules.Geofencing.getCurrentLocation.mockResolvedValueOnce(
mockLocation
);
const location = await NativeModules.Geofencing.getCurrentLocation();
const location = await Geofencing.getCurrentLocation();
expect(location).toEqual(mockLocation);
});

it('should add a geofence', async () => {
const mockResponse = { success: true, id: '1', error: '' };
NativeModules.Geofencing.addGeofence.mockResolvedValueOnce(mockResponse);
const response = await NativeModules.Geofencing.addGeofence({
const response = await Geofencing.addGeofence({
id: '1',
latitude: 10.0,
longitude: 20.0,
Expand All @@ -125,7 +91,7 @@ describe('Geofencing Module', () => {
it('should remove a geofence', async () => {
const mockResponse = { success: true, id: '1', error: '' };
NativeModules.Geofencing.removeGeofence.mockResolvedValueOnce(mockResponse);
const response = await NativeModules.Geofencing.removeGeofence('1');
const response = await Geofencing.removeGeofence('1');
expect(response).toEqual(mockResponse);
expect(NativeModules.Geofencing.removeGeofence).toHaveBeenCalledWith('1');
});
Expand All @@ -135,7 +101,7 @@ describe('Geofencing Module', () => {
NativeModules.Geofencing.getRegisteredGeofences.mockResolvedValueOnce(
mockGeofences
);
const geofences = await NativeModules.Geofencing.getRegisteredGeofences();
const geofences = await Geofencing.getRegisteredGeofences();
expect(geofences).toEqual(mockGeofences);
});

Expand All @@ -144,35 +110,29 @@ describe('Geofencing Module', () => {
NativeModules.Geofencing.removeAllGeofence.mockResolvedValueOnce(
mockResponse
);
const response = await NativeModules.Geofencing.removeAllGeofence();
const response = await Geofencing.removeAllGeofence();
expect(response).toEqual(mockResponse);
});

it('should handle geofence enter event', () => {
const callback = jest.fn();
NativeModules.Geofencing.Geofence.onEnter(callback);
geofencingEventEmitter.emit('onEnter', ['1', '2']);
expect(callback).toHaveBeenCalledWith(['1', '2']);
Geofencing.onEnter(callback);
expect(Geofencing.geofencingEventEmitter.addListener).toHaveBeenCalledWith(Events.Enter, callback);
});

it('should handle geofence exit event', () => {
const callback = jest.fn();
NativeModules.Geofencing.Geofence.onExit(callback);
geofencingEventEmitter.emit('onExit', ['3', '4']);
expect(callback).toHaveBeenCalledWith(['3', '4']);
Geofencing.onExit(callback);
expect(Geofencing.geofencingEventEmitter.addListener).toHaveBeenCalledWith(Events.Exit, callback);
});

it('should remove geofence enter listener', () => {
NativeModules.Geofencing.Geofence.removeOnEnterListener();
expect(
NativeModules.Geofencing.Geofence.removeOnEnterListener
).toHaveBeenCalled();
Geofencing.removeOnEnterListener();
expect(Geofencing.geofencingEventEmitter.removeAllListeners).toHaveBeenCalledWith(Events.Enter);
});

it('should remove geofence exit listener', () => {
NativeModules.Geofencing.Geofence.removeOnExitListener();
expect(
NativeModules.Geofencing.Geofence.removeOnExitListener
).toHaveBeenCalled();
Geofencing.removeOnExitListener();
expect(Geofencing.geofencingEventEmitter.removeAllListeners).toHaveBeenCalledWith(Events.Exit);
});
});
Loading

0 comments on commit fab963e

Please sign in to comment.