Skip to content

Commit

Permalink
[notifications] Check if icon file exists before removing (#13539)
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzach authored and brentvatne committed Jul 8, 2021
1 parent 73b0dad commit 6eda79f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -52,7 +52,7 @@ const soundPath = path.resolve(__dirname, './fixtures/cat.wav');
const projectRoot = '/app';

describe('Android notifications configuration', () => {
beforeAll(async () => {
beforeEach(async () => {
const icon = fsReal.readFileSync(iconPath);
const sound = fsReal.readFileSync(soundPath);
vol.fromJSON(
Expand All @@ -65,10 +65,13 @@ describe('Android notifications configuration', () => {
vol.writeFileSync('/app/assets/notificationSound.wav', sound);
});

afterEach(() => {
vol.reset();
});

afterAll(() => {
jest.unmock('@expo/image-utils');
jest.unmock('fs');
vol.reset();
});

it(`returns null if no config provided`, () => {
Expand All @@ -92,6 +95,24 @@ describe('Android notifications configuration', () => {
const after = getDirFromFS(vol.toJSON(), projectRoot);
expect(Object.keys(after).sort()).toEqual(LIST_OF_GENERATED_NOTIFICATION_FILES.sort());
});

it('Safely remove icon if it exists, and ignore if it doesnt', async () => {
const before = getDirFromFS(vol.toJSON(), projectRoot);
// first set the icon
await setNotificationIconAsync(projectRoot, '/app/assets/notificationIcon.png');

// now remove
await setNotificationIconAsync(projectRoot, null);

const after = getDirFromFS(vol.toJSON(), projectRoot);
expect(before).toMatchObject(after);

// now remove again to make sure we don't throw in that case
await setNotificationIconAsync(projectRoot, null);

const final = getDirFromFS(vol.toJSON(), projectRoot);
expect(before).toMatchObject(final);
});
});

function setUpDrawableDirectories() {
Expand Down
Expand Up @@ -181,7 +181,10 @@ function removeNotificationIconImageFiles(projectRoot: string) {
Object.values(dpiValues).forEach(async ({ folderName }) => {
const drawableFolderName = folderName.replace('mipmap', 'drawable');
const dpiFolderPath = resolve(projectRoot, ANDROID_RES_PATH, drawableFolderName);
unlinkSync(resolve(dpiFolderPath, NOTIFICATION_ICON + '.png'));
const iconFile = resolve(dpiFolderPath, NOTIFICATION_ICON + '.png');
if (existsSync(iconFile)) {
unlinkSync(iconFile);
}
});
}

Expand Down

0 comments on commit 6eda79f

Please sign in to comment.