Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 1 addition & 77 deletions plugin/__tests__/withAndroidPushNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,64 +218,6 @@ describe('withAndroidPushNotifications', () => {
expect(num).toBe(1);
});

it('should warn the user if the google-services.json file is not found', async () => {
const config = {
...createTestConfig(),
android: {
...createTestConfig().android,
googleServicesFile: undefined,
},
};
const props: ConfigPluginProps = {
autoConfigurePushNotifications: true,
};
withIterable(config, props);
expect(WarningAggregator.addWarningAndroid).toHaveBeenCalledWith(
'@iterable/expo-plugin',
'Path to google-services.json is not defined, so push notifications will not be enabled. To enable push notifications, please specify the `expo.android.googleServicesFile` field in app.json.'
);
});

it('should copy google-services.json when path is defined', async () => {
const config = {
...createTestConfig(),
_internal: { projectRoot },
android: {
...createTestConfig().android,
googleServicesFile: '__mocks__/google-services.json',
},
};
const props: ConfigPluginProps = {
autoConfigurePushNotifications: true,
};
// Create the source file in the project root
const srcPath = path.resolve(
projectRoot,
'__mocks__/google-services.json'
);
fs.mkdirSync(path.dirname(srcPath), { recursive: true });
fs.writeFileSync(srcPath, '{}');
const result = withIterable(config, props) as WithIterableResult;
const dangerousMod = result.mods.android.dangerous as Mod<any>;
await dangerousMod({
...createMockAndroidDangerousModConfig(),
modRequest: {
projectRoot,
platformProjectRoot: path.resolve(projectRoot, 'android'),
modName: 'dangerous',
platform: 'android',
introspect: true,
},
android: {
googleServicesFile: '__mocks__/google-services.json',
},
});
expect(mockCopyFile).toHaveBeenCalledWith(
srcPath,
path.resolve(projectRoot, 'android/app/google-services.json')
);
});

it('should warn when google-services.json path is not defined', async () => {
const config = {
...createTestConfig(),
Expand All @@ -290,28 +232,10 @@ describe('withAndroidPushNotifications', () => {
withIterable(config, props);
expect(WarningAggregator.addWarningAndroid).toHaveBeenCalledWith(
'@iterable/expo-plugin',
'Path to google-services.json is not defined, so push notifications will not be enabled. To enable push notifications, please specify the `expo.android.googleServicesFile` field in app.json.'
'The path to your google-services.json file is not defined, so push notifications may not work. Please add the path to your google-services.json file in the `expo.android.googleServicesFile` field in your app.json.'
);
expect(mockCopyFile).not.toHaveBeenCalled();
});

it('should throw error when google-services.json does not exist', async () => {
const config = createTestConfig();
const props: ConfigPluginProps = {
autoConfigurePushNotifications: true,
};
mockCopyFile.mockRejectedValueOnce(new Error('File not found'));
const result = withIterable(config, props) as WithIterableResult;
const dangerousMod = result.mods.android.dangerous as Mod<any>;
await expect(
dangerousMod({
...createMockAndroidDangerousModConfig(),
android: {
googleServicesFile: './google-services.json',
},
})
).rejects.toThrow('Cannot copy google-services.json');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ export const FIREBASE_BOM_VERSION = '32.8.1';

export const FIREBASE_MESSAGING_CLASS_PATH =
'com.google.firebase:firebase-messaging';

export const DEFAULT_GOOGLE_SERVICES_PATH = 'app/google-services.json';
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import {
WarningAggregator,
withAndroidManifest,
withAppBuildGradle,
withDangerousMod,
withPlugins,
withProjectBuildGradle,
} from 'expo/config-plugins';
import fs from 'fs';
import path from 'path';

import { ConfigPluginPropsWithDefaults } from '../withIterable.types';
import {
DEFAULT_GOOGLE_SERVICES_PATH,
FIREBASE_BOM_CLASS_PATH,
FIREBASE_BOM_VERSION,
FIREBASE_MESSAGING_CLASS_PATH,
Expand Down Expand Up @@ -124,59 +120,19 @@ const withAppPermissions: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
});
};

/**
* Copy `google-services.json`
* TODO: Add this step to the docs
*/
const withCopyAndroidGoogleServices: ConfigPlugin = (config) => {
return withDangerousMod(config, [
'android',
async (newConfig) => {
const srcPath = path.resolve(
newConfig.modRequest.projectRoot,
newConfig.android?.googleServicesFile ?? ''
);
const destPath = path.resolve(
newConfig.modRequest.platformProjectRoot,
DEFAULT_GOOGLE_SERVICES_PATH
);

try {
await fs.promises.copyFile(srcPath, destPath);
} catch {
throw new Error(
`Cannot copy google-services.json, because the file ${srcPath} doesn't exist. Please provide a valid path in \`app.json\`.`
);
}
return newConfig;
},
]);
};

const withGoogleServices: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
config,
props
) => {
export const withAndroidPushNotifications: ConfigPlugin<
ConfigPluginPropsWithDefaults
> = (config, props) => {
if (!config.android?.googleServicesFile) {
WarningAggregator.addWarningAndroid(
'@iterable/expo-plugin',
'Path to google-services.json is not defined, so push notifications will not be enabled. To enable push notifications, please specify the `expo.android.googleServicesFile` field in app.json.'
'The path to your google-services.json file is not defined, so push notifications may not work. Please add the path to your google-services.json file in the `expo.android.googleServicesFile` field in your app.json.'
);
return config;
}

return withPlugins(config, [
[withFirebase, props],
[withCopyAndroidGoogleServices, props],
]);
};

export const withAndroidPushNotifications: ConfigPlugin<
ConfigPluginPropsWithDefaults
> = (config, props) => {
return withPlugins(config, [
[withAppPermissions, props],
[withGoogleServices, props],
[withFirebase, props],
]);
};

Expand Down
Loading