Skip to content

Commit

Permalink
Merge pull request #453 from divyansh0908/notification/local_notify
Browse files Browse the repository at this point in the history
Notification/local notify
  • Loading branch information
shreya-mishra authored Oct 8, 2024
2 parents e0a78d3 + 9150ce9 commit 02ad0ff
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 182 deletions.
9 changes: 9 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BASE_UTL=
RDS_SESSION=
FIREBASE_PROJECT_ID=
FIREBASE_MESSAGING_SENDER_ID=
FIREBASE_STORAGE_BUCKET=
MOBILE_SDK_APP_ID=
OAUTH_CLIENT_ID=
FIREBASE_CURRENT_API_KEY=
OTHER_PLATFORM_OAUTH_CLIENT_ID=
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ buck-out/
# CocoaPods
/ios/Pods/

# ignore google-services
/android/app/google-services.json



#env
Expand Down
130 changes: 130 additions & 0 deletions __tests__/Goals/components/NotificationForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import NotifyForm from '../../../src/components/Notify/NotifyForm';
import { AuthContext } from '../../../src/context/AuthContext';
import {
postFcmToken,
getAllUsers,
sendNotification,
} from '../../../src/screens/AuthScreen/Util';

// Mock the functions used in the component
jest.mock('../../../src/screens/AuthScreen/Util', () => ({
postFcmToken: jest.fn(),
sendNotification: jest.fn(),
getAllUsers: jest.fn(() => Promise.resolve([])), // Mock getAllUsers with an empty array
}));

jest.mock('@react-native-firebase/messaging', () => ({
firebase: {
messaging: jest.fn(() => ({
getToken: jest.fn(() => Promise.resolve('mocked-fcm-token')),
hasPermission: jest.fn(() => Promise.resolve(1)), // Mock permission granted
requestPermission: jest.fn(() => Promise.resolve()), // Mock permission request
})),
},
}));

describe('NotifyForm', () => {
const loggedInUserData = { token: 'user-token' };

const renderComponent = () => {
return render(
<AuthContext.Provider value={{ loggedInUserData }}>
<NotifyForm />
</AuthContext.Provider>,
);
};

beforeEach(() => {
jest.clearAllMocks();
});

it('renders the form with title, description, and Notify button', () => {
const { getByText, getByPlaceholderText } = renderComponent();

expect(getByText('Title:')).toBeTruthy();
expect(getByText('Description:')).toBeTruthy();
expect(getByText('Notify To:')).toBeTruthy();
expect(getByPlaceholderText('Enter title')).toBeTruthy();
expect(getByPlaceholderText('Enter description')).toBeTruthy();
expect(getByText('Notify')).toBeTruthy();
});

it('Calls postFcmToken', async () => {
renderComponent();

await waitFor(() => {
expect(postFcmToken).toHaveBeenCalledWith(
'mocked-fcm-token',
'user-token',
);
});
});

it('fetches users and updates the dropdown', async () => {
const mockUsers = [
{ id: '1', username: 'john_doe', first_name: 'John', last_name: 'Doe' },
{ id: '2', username: 'jane_doe', first_name: 'Jane', last_name: 'Doe' },
];

getAllUsers.mockResolvedValue(mockUsers); // Mock resolved users

const { getByTestId, getByText } = renderComponent();

// Wait for users to load
await waitFor(() => {
expect(getAllUsers).toHaveBeenCalledWith('user-token');
});

const dropdown = getByTestId('dropdown');
fireEvent.press(dropdown); // Simulate dropdown press to show user list

await waitFor(() => {
expect(getByText('john_doe')).toBeTruthy();
expect(getByText('jane_doe')).toBeTruthy();
});
});

it('selects a user from the dropdown and sends a notification', async () => {
const mockUsers = [
{ id: '1', username: 'john_doe', first_name: 'John', last_name: 'Doe' },
];

getAllUsers.mockResolvedValue(mockUsers);

const { getByTestId, getByPlaceholderText, getByText } = renderComponent();

// Wait for users to load
await waitFor(() => {
expect(getAllUsers).toHaveBeenCalledWith('user-token');
});

const dropdown = getByTestId('dropdown');
fireEvent.press(dropdown); // Open dropdown

// Select a user from the dropdown
await waitFor(() => {
fireEvent.press(getByText('john_doe'));
});

// Fill in title and description
fireEvent.changeText(getByPlaceholderText('Enter title'), 'Test Title');
fireEvent.changeText(
getByPlaceholderText('Enter description'),
'Test Description',
);

// Press Notify button
fireEvent.press(getByText('Notify'));

await waitFor(() => {
expect(sendNotification).toHaveBeenCalledWith(
'Test Title',
'Test Description',
'1',
'user-token',
);
});
});
});
26 changes: 10 additions & 16 deletions __tests__/screens/NotifyScreen.test.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react-native';
import { render } from '@testing-library/react-native';
import NotifyScreen from '../../src/screens/NotifyScreen/NotifyScreen';
import { postFcmToken } from '../../src/screens/AuthScreen/Util';
import { firebase } from '@react-native-firebase/messaging';

jest.mock('../../src/screens/AuthScreen/Util', () => ({
postFcmToken: jest.fn(),
sendNotification: jest.fn(),
getAllUsers: jest.fn(() => Promise.resolve([])), // Mock getAllUsers with an empty array
}));

jest.mock('@react-native-firebase/messaging', () => ({
firebase: {
messaging: jest.fn(() => ({
getToken: jest.fn(() => Promise.resolve('mocked-fcm-token')),
hasPermission: jest.fn(() => Promise.resolve(1)), // Mock permission granted
requestPermission: jest.fn(() => Promise.resolve()), // Mock permission request
})),
},
}));
jest.mock('../../src/screens/AuthScreen/Util', () => ({
postFcmToken: jest.fn(),
}));
describe('NotifyScreen', () => {
it('should render correctly with title and NotifyForm', async () => {
const { getByText } = render(<NotifyScreen />);

expect(getByText('Event Notifications')).toBeTruthy();

await waitFor(() => {
expect(firebase.messaging().getToken).toHaveBeenCalled();
});
await waitFor(() =>
expect(postFcmToken).toHaveBeenCalledWith(
'mocked-fcm-token',
'user-token',
),
);
// Wait for the getToken to be called
});
});
130 changes: 0 additions & 130 deletions android/app/google-services.json

This file was deleted.

12 changes: 12 additions & 0 deletions bin/postInstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!usr/bin/env node
run('node ./src/service/googleServiceTemplate.js');

function run(command) {
console.info(`./bin/postInstall scripit running: ${command}`);
try {
require('child_process').execSync(command, { stdio: 'inherit' });
} catch (err) {
console.error(`./bin/postInstall failed on command ${command}`);
process.exit(err.status);
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
"build-assets-folder": "cd android/app/src/main && if [ -d 'assets' ]; then rm -r assets; fi",
"build": "mkdir -p android/app/src/main/assets && npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && cd android && ./gradlew assembleDebug",
"build-release": "mkdir -p android/app/src/main/assets && npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/build/intermediates/res/merged/release/ && rm -rf android/app/src/main/res/drawable-* && rm -rf android/app/src/main/res/raw/* && cd android && ./gradlew bundleRelease"
"postinstall": "node ./bin/postInstall"
},
"dependencies": {
"@babel/helper-hoist-variables": "^7.24.7",
"@react-native-async-storage/async-storage": "^1.15.16",
"@react-native-community/netinfo": "^5.9.9",
"@react-native-community/push-notification-ios": "^1.11.0",
"@react-native-community/slider": "^4.4.3",
"@react-native-firebase/app": "^20.4.0",
"@react-native-firebase/messaging": "^20.4.0",
Expand All @@ -31,6 +33,7 @@
"@react-navigation/native-stack": "^6.9.12",
"@react-navigation/stack": "^6.2.0",
"axios": "^0.26.0",
"dotenv": "^16.4.5",
"eslint-plugin-prettier": "^4.1.0",
"moment": "^2.29.4",
"react": "18.2.0",
Expand Down
Loading

0 comments on commit 02ad0ff

Please sign in to comment.