react-native-integrate
Version:
Automate integration of additional code into React Native projects
470 lines (469 loc) • 17.7 kB
JavaScript
"use strict";
/* eslint-disable @typescript-eslint/no-unsafe-call */
Object.defineProperty(exports, "__esModule", { value: true });
const { mockFs, mockPrompter } = require('../../mocks/mockAll');
const mock = jest.spyOn(require('../../../utils/stringSplice'), 'stringSplice');
const notificationViewControllerTask_1 = require("../../../tasks/notificationViewControllerTask");
const mockAll_1 = require("../../mocks/mockAll");
const notificationViewControllerM_1 = require("../../../scaffold/notification-content/notificationViewControllerM");
describe('notificationViewControllerTask', () => {
it('should skip insert when ifNotPresent exists', async () => {
mockPrompter.log.message.mockReset();
const content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
ifNotPresent: 'super viewDidLoad]',
before: { regex: 'return' },
prepend: '[FIRApp configure];',
append: '[FIRApp configure];',
},
],
};
await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
});
expect(mockPrompter.log.message).toHaveBeenCalledWith(expect.stringContaining('found existing '));
});
it('should prepend text into didLaunchWithOptions', async () => {
let content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'didReceiveNotificationResponse',
prepend: '[FIRApp configure];',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
});
// @ts-ignore
expect(content).toContain(task.actions[1].prepend);
});
it('should append text into didLaunchWithOptions', async () => {
let content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'didReceiveNotificationResponse',
append: '[FIRApp configure];',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
});
// @ts-ignore
expect(content).toContain(task.actions[1].append);
});
it('should insert text after point into didLaunchWithOptions', async () => {
let content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
after: { regex: 'super viewDidLoad' },
prepend: '[FIRApp configure];',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
});
expect(content).toContain(`[super viewDidLoad];
[FIRApp configure];`);
});
it('should insert text before point into didLaunchWithOptions', async () => {
let content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
before: { regex: 'super viewDidLoad' },
append: '[FIRApp configure];',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
});
expect(content).toContain(`
[FIRApp configure];
[super viewDidLoad];
`);
});
it('should skip if condition not met', async () => {
const content = '';
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
when: { test: 'random' },
prepend: '#import <Firebase.h>',
},
{
when: { test: 'random' },
block: 'didReceiveNotification',
prepend: '[FIRApp configure];',
},
],
};
await expect((0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
})).resolves.not.toThrow();
});
it('should throw when insertion point not found', async () => {
const content = notificationViewControllerM_1.notificationViewControllerM;
const taskInsertBefore = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
search: 'random',
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
before: 'random',
append: '[FIRApp configure];',
strict: true,
},
],
};
await expect((0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: taskInsertBefore,
content,
packageName: 'test-package',
})).rejects.toThrowError('insertion point');
const taskInsertAfter = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
after: 'random',
prepend: '[FIRApp configure];',
strict: true,
},
],
};
await expect((0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: taskInsertAfter,
content,
packageName: 'test-package',
})).rejects.toThrowError('insertion point');
});
it('should throw when NotificationContent implementation not found', async () => {
const content = '';
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
prepend: '[FIRApp configure];',
},
],
};
await expect((0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
})).rejects.toThrowError('@implementation NotificationViewController');
const task2 = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
block: 'didReceiveNotification',
prepend: '[FIRApp configure];',
},
],
};
await expect((0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task2,
content,
packageName: 'test-package',
})).rejects.toThrowError('@implementation NotificationViewController');
});
it('should throw for invalid method', async () => {
const content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'invalidMethod',
prepend: '[FIRApp configure];',
},
],
};
await expect((0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task,
content,
packageName: 'test-package',
})).rejects.toThrowError('Invalid block');
});
it('should append text into non existing viewDidLoad', async () => {
let content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
append: 'appended code',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task,
content,
packageName: 'test-package',
});
expect(content).toContain('viewDidLoad');
// @ts-ignore
expect(content).toContain(task.actions[1].append);
});
it('should insert text before point into non existing viewDidLoad', async () => {
let content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
before: 'random',
append: 'inserted code',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task,
content,
packageName: 'test-package',
});
expect(content).toContain('viewDidLoad');
// @ts-ignore
expect(content).toContain(task.actions[1].append);
});
it('should insert text after point into non existing viewDidLoad', async () => {
let content = notificationViewControllerM_1.notificationViewControllerM;
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'viewDidLoad',
after: 'random',
prepend: 'inserted code',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task,
content,
packageName: 'test-package',
});
expect(content).toContain('viewDidLoad');
// @ts-ignore
expect(content).toContain(task.actions[1].prepend);
});
it('should append/prepend/insert after/before text into non existing blocks}', async () => {
for (const block of [
'viewDidLoad',
'viewWillAppear',
'viewDidAppear',
'viewWillDisappear',
'dealloc',
'didReceiveNotification',
'didReceiveNotificationResponse',
]) {
let content = notificationViewControllerM_1.notificationViewControllerM.replace(/(didReceiveNotificationRequest|viewDidLoad)/, 'foo');
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
block,
prepend: 'prepended code',
append: 'appended code',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task,
content,
packageName: 'test-package',
});
expect(content).toContain(block);
// @ts-ignore
expect(content).toContain(task.actions[0].prepend);
// @ts-ignore
expect(content).toContain(task.actions[0].append);
// second append on existing block
mockPrompter.log.message.mockReset();
const task2 = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
block,
prepend: 'prepended code',
append: 'appended code',
},
],
};
content = await (0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task: task2,
content,
packageName: 'test-package',
});
// @ts-ignore
expect(content).toContain(task2.actions[0].prepend);
// @ts-ignore
expect(content).toContain(task2.actions[0].append);
expect(mockPrompter.log.message).toHaveBeenCalledWith(expect.stringContaining('code already exists'));
}
});
it('should throw when block could not be added', async () => {
const content = notificationViewControllerM_1.notificationViewControllerM.replace(/viewDidLoad/, 'foo');
mock.mockImplementationOnce(content => content);
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
block: 'didReceiveNotificationResponse',
prepend: 'random',
},
],
};
await expect((0, notificationViewControllerTask_1.notificationViewControllerTask)({
configPath: 'path/to/config',
task,
content,
packageName: 'test-package',
})).rejects.toThrowError('block could not be inserted');
});
describe('runTask', () => {
it('should read and write app delegate file', async () => {
const notificationContentPath = (0, mockAll_1.writeMockNotificationContent)();
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'didReceiveNotificationResponse',
prepend: '[FIRApp configure];',
},
],
};
await (0, notificationViewControllerTask_1.runTask)({
configPath: 'path/to/config',
task: task,
packageName: 'test-package',
});
const content = mockFs.readFileSync(notificationContentPath);
// @ts-ignore
expect(content).toContain(task.actions[1].prepend);
});
it('should throw when app delegate does not exist', async () => {
const task = {
task: 'notification_view_controller',
target: 'test',
actions: [
{
prepend: '#import <Firebase.h>',
},
{
block: 'didReceiveNotificationResponse',
prepend: '[FIRApp configure];',
},
],
};
await expect((0, notificationViewControllerTask_1.runTask)({
configPath: 'path/to/config',
task: task,
packageName: 'test-package',
})).rejects.toThrowError('NotificationContent file not found');
});
});
});