2016-09-12 13:13:27 +00:00
|
|
|
/**
|
2018-02-09 01:10:29 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-09-12 13:13:27 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-11-02 13:14:11 +00:00
|
|
|
*
|
2018-05-11 19:43:49 +00:00
|
|
|
* @format
|
2017-11-02 13:14:11 +00:00
|
|
|
* @emails oncall+javascript_foundation
|
2016-09-12 13:13:27 +00:00
|
|
|
*/
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const log = require('npmlog');
|
|
|
|
const path = require('path');
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('chalk', {grey: str => str});
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
describe('link', () => {
|
|
|
|
beforeEach(() => {
|
2016-09-19 19:30:50 +00:00
|
|
|
jest.resetModules();
|
2016-08-22 15:56:14 +00:00
|
|
|
delete require.cache[require.resolve('../link')];
|
2016-07-30 15:59:16 +00:00
|
|
|
log.level = 'silent';
|
2016-05-20 11:52:08 +00:00
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should reject when run in a folder without package.json', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
const config = {
|
|
|
|
getProjectConfig: () => {
|
|
|
|
throw new Error('No package.json found');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-08-22 15:56:14 +00:00
|
|
|
const link = require('../link').func;
|
2016-07-30 15:59:16 +00:00
|
|
|
link([], config).catch(() => done());
|
2016-05-20 11:52:08 +00:00
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should accept a name of a dependency to link', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
const config = {
|
2018-02-08 17:45:16 +00:00
|
|
|
getPlatformConfig: () => ({ios: {}, android: {}}),
|
2018-05-11 19:43:49 +00:00
|
|
|
getProjectConfig: () => ({assets: []}),
|
|
|
|
getDependencyConfig: sinon.stub().returns({assets: [], commands: {}}),
|
2016-05-20 11:52:08 +00:00
|
|
|
};
|
|
|
|
|
2016-08-22 15:56:14 +00:00
|
|
|
const link = require('../link').func;
|
2016-07-30 15:59:16 +00:00
|
|
|
link(['react-native-gradient'], config).then(() => {
|
2016-05-20 11:52:08 +00:00
|
|
|
expect(
|
2018-05-11 19:43:49 +00:00
|
|
|
config.getDependencyConfig.calledWith('react-native-gradient'),
|
2016-07-30 15:59:16 +00:00
|
|
|
).toBeTruthy();
|
2016-05-20 11:52:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should register native module when android/ios projects are present', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
const registerNativeModule = sinon.stub();
|
|
|
|
const dependencyConfig = {android: {}, ios: {}, assets: [], commands: {}};
|
2018-02-13 12:57:18 +00:00
|
|
|
const androidLinkConfig = require('../android');
|
|
|
|
const iosLinkConfig = require('../ios');
|
2016-05-20 11:52:08 +00:00
|
|
|
const config = {
|
2018-05-11 19:43:49 +00:00
|
|
|
getPlatformConfig: () => ({
|
|
|
|
ios: {linkConfig: iosLinkConfig},
|
|
|
|
android: {linkConfig: androidLinkConfig},
|
|
|
|
}),
|
2016-05-20 11:52:08 +00:00
|
|
|
getProjectConfig: () => ({android: {}, ios: {}, assets: []}),
|
|
|
|
getDependencyConfig: sinon.stub().returns(dependencyConfig),
|
|
|
|
};
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../android/isInstalled.js', sinon.stub().returns(false));
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../android/registerNativeModule.js', registerNativeModule);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(false));
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/registerNativeModule.js', registerNativeModule);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2016-08-22 15:56:14 +00:00
|
|
|
const link = require('../link').func;
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
link(['react-native-blur'], config).then(() => {
|
|
|
|
expect(registerNativeModule.calledTwice).toBeTruthy();
|
2016-05-20 11:52:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should not register modules when they are already installed', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
const registerNativeModule = sinon.stub();
|
|
|
|
const dependencyConfig = {ios: {}, android: {}, assets: [], commands: {}};
|
|
|
|
const config = {
|
2018-02-08 17:45:16 +00:00
|
|
|
getPlatformConfig: () => ({ios: {}, android: {}}),
|
2018-05-11 19:43:49 +00:00
|
|
|
getProjectConfig: () => ({ios: {}, android: {}, assets: []}),
|
2016-05-20 11:52:08 +00:00
|
|
|
getDependencyConfig: sinon.stub().returns(dependencyConfig),
|
|
|
|
};
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(true));
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../android/isInstalled.js', sinon.stub().returns(true));
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/registerNativeModule.js', registerNativeModule);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../android/registerNativeModule.js', registerNativeModule);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2016-08-22 15:56:14 +00:00
|
|
|
const link = require('../link').func;
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
link(['react-native-blur'], config).then(() => {
|
|
|
|
expect(registerNativeModule.callCount).toEqual(0);
|
2016-05-20 11:52:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should register native modules for plugins', done => {
|
2018-02-08 17:45:16 +00:00
|
|
|
const registerNativeModule = sinon.stub();
|
2018-05-11 19:43:49 +00:00
|
|
|
const dependencyConfig = {
|
|
|
|
ios: {},
|
|
|
|
android: {},
|
|
|
|
test: {},
|
|
|
|
assets: [],
|
|
|
|
commands: {},
|
|
|
|
};
|
|
|
|
const linkPluginConfig = {
|
|
|
|
isInstalled: () => false,
|
|
|
|
register: registerNativeModule,
|
|
|
|
};
|
2018-02-08 17:45:16 +00:00
|
|
|
const config = {
|
2018-05-11 19:43:49 +00:00
|
|
|
getPlatformConfig: () => ({
|
|
|
|
ios: {},
|
|
|
|
android: {},
|
|
|
|
test: {linkConfig: () => linkPluginConfig},
|
|
|
|
}),
|
|
|
|
getProjectConfig: () => ({ios: {}, android: {}, test: {}, assets: []}),
|
2018-02-08 17:45:16 +00:00
|
|
|
getDependencyConfig: sinon.stub().returns(dependencyConfig),
|
|
|
|
};
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(true));
|
2018-02-08 17:45:16 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../android/isInstalled.js', sinon.stub().returns(true));
|
2018-02-08 17:45:16 +00:00
|
|
|
|
|
|
|
const link = require('../link').func;
|
|
|
|
|
|
|
|
link(['react-native-blur'], config).then(() => {
|
|
|
|
expect(registerNativeModule.calledOnce).toBeTruthy();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should not register native modules for plugins when already installed', done => {
|
2018-02-08 17:45:16 +00:00
|
|
|
const registerNativeModule = sinon.stub();
|
2018-05-11 19:43:49 +00:00
|
|
|
const dependencyConfig = {
|
|
|
|
ios: {},
|
|
|
|
android: {},
|
|
|
|
test: {},
|
|
|
|
assets: [],
|
|
|
|
commands: {},
|
|
|
|
};
|
|
|
|
const linkPluginConfig = {
|
|
|
|
isInstalled: () => true,
|
|
|
|
register: registerNativeModule,
|
|
|
|
};
|
2018-02-08 17:45:16 +00:00
|
|
|
const config = {
|
2018-05-11 19:43:49 +00:00
|
|
|
getPlatformConfig: () => ({
|
|
|
|
ios: {},
|
|
|
|
android: {},
|
|
|
|
test: {linkConfig: () => linkPluginConfig},
|
|
|
|
}),
|
|
|
|
getProjectConfig: () => ({ios: {}, android: {}, test: {}, assets: []}),
|
2018-02-08 17:45:16 +00:00
|
|
|
getDependencyConfig: sinon.stub().returns(dependencyConfig),
|
|
|
|
};
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(true));
|
2018-02-08 17:45:16 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../android/isInstalled.js', sinon.stub().returns(true));
|
2018-02-08 17:45:16 +00:00
|
|
|
|
|
|
|
const link = require('../link').func;
|
|
|
|
|
|
|
|
link(['react-native-blur'], config).then(() => {
|
|
|
|
expect(registerNativeModule.callCount).toEqual(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should run prelink and postlink commands at the appropriate times', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
const registerNativeModule = sinon.stub();
|
|
|
|
const prelink = sinon.stub().yieldsAsync();
|
|
|
|
const postlink = sinon.stub().yieldsAsync();
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/registerNativeModule.js', registerNativeModule);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/isInstalled.js', sinon.stub().returns(false));
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-02-13 12:57:18 +00:00
|
|
|
const linkConfig = require('../ios');
|
2016-05-20 11:52:08 +00:00
|
|
|
const config = {
|
2018-05-11 19:43:49 +00:00
|
|
|
getPlatformConfig: () => ({ios: {linkConfig: linkConfig}}),
|
|
|
|
getProjectConfig: () => ({ios: {}, assets: []}),
|
2016-05-20 11:52:08 +00:00
|
|
|
getDependencyConfig: sinon.stub().returns({
|
2018-05-11 19:43:49 +00:00
|
|
|
ios: {},
|
|
|
|
assets: [],
|
|
|
|
commands: {prelink, postlink},
|
2016-05-20 11:52:08 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2016-08-22 15:56:14 +00:00
|
|
|
const link = require('../link').func;
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
link(['react-native-blur'], config).then(() => {
|
|
|
|
expect(prelink.calledBefore(registerNativeModule)).toBeTruthy();
|
|
|
|
expect(postlink.calledAfter(registerNativeModule)).toBeTruthy();
|
2016-05-20 11:52:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should copy assets from both project and dependencies projects', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
const dependencyAssets = ['Fonts/Font.ttf'];
|
2018-02-08 17:45:16 +00:00
|
|
|
const dependencyConfig = {assets: dependencyAssets, ios: {}, commands: {}};
|
2016-05-20 11:52:08 +00:00
|
|
|
const projectAssets = ['Fonts/FontC.ttf'];
|
|
|
|
const copyAssets = sinon.stub();
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
jest.setMock('../ios/copyAssets.js', copyAssets);
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-02-13 12:57:18 +00:00
|
|
|
const linkConfig = require('../ios');
|
2016-05-20 11:52:08 +00:00
|
|
|
const config = {
|
2018-05-11 19:43:49 +00:00
|
|
|
getPlatformConfig: () => ({ios: {linkConfig: linkConfig}}),
|
|
|
|
getProjectConfig: () => ({ios: {}, assets: projectAssets}),
|
2016-05-20 11:52:08 +00:00
|
|
|
getDependencyConfig: sinon.stub().returns(dependencyConfig),
|
|
|
|
};
|
|
|
|
|
2016-08-22 15:56:14 +00:00
|
|
|
const link = require('../link').func;
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
link(['react-native-blur'], config).then(() => {
|
|
|
|
expect(copyAssets.calledOnce).toBeTruthy();
|
|
|
|
expect(copyAssets.getCall(0).args[0]).toEqual(
|
2018-05-11 19:43:49 +00:00
|
|
|
projectAssets.concat(dependencyAssets),
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|