2023-07-18 15:58:06 +02:00
|
|
|
/* eslint-disable jest/require-top-level-describe */
|
|
|
|
|
|
|
|
const { afterEachCleanUp, afterAllCleanUp } = require('@joplin/lib/testing/test-utils.js');
|
2024-07-18 10:44:13 +02:00
|
|
|
const shim = require('@joplin/lib/shim').default;
|
2023-07-18 15:58:06 +02:00
|
|
|
const { shimInit } = require('@joplin/lib/shim-init-node.js');
|
2024-07-18 10:44:13 +02:00
|
|
|
const injectedJs = require('./utils/injectedJs.js').default;
|
2023-07-18 15:58:06 +02:00
|
|
|
const { mkdir, rm } = require('fs-extra');
|
|
|
|
const path = require('path');
|
2024-07-18 10:44:13 +02:00
|
|
|
const sharp = require('sharp');
|
2023-07-18 15:58:06 +02:00
|
|
|
const { tmpdir } = require('os');
|
|
|
|
const uuid = require('@joplin/lib/uuid').default;
|
|
|
|
const sqlite3 = require('sqlite3');
|
2024-03-11 17:02:15 +02:00
|
|
|
const React = require('react');
|
2023-12-15 20:34:21 +02:00
|
|
|
require('../../jest.base-setup.js')();
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2023-07-29 17:33:39 +02:00
|
|
|
import { setImmediate } from 'timers';
|
|
|
|
|
|
|
|
// Required by some libraries (setImmediate is not supported in most browsers,
|
|
|
|
// so is removed by jsdom).
|
|
|
|
window.setImmediate = setImmediate;
|
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
shimInit({
|
|
|
|
nodeSqlite: sqlite3,
|
2024-08-02 15:51:49 +02:00
|
|
|
appVersion: () => require('./package.json').version,
|
2024-03-11 17:02:15 +02:00
|
|
|
React,
|
2024-07-18 10:44:13 +02:00
|
|
|
sharp,
|
2024-03-11 17:02:15 +02:00
|
|
|
});
|
2024-07-18 10:44:13 +02:00
|
|
|
shim.injectedJs = (name) => {
|
|
|
|
if (!(name in injectedJs)) {
|
|
|
|
throw new Error(`Cannot find injected JS with ID ${name}`);
|
|
|
|
}
|
|
|
|
return injectedJs[name];
|
|
|
|
};
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2023-08-18 10:45:04 +02:00
|
|
|
// This library has the following error when running within Jest:
|
|
|
|
// Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.
|
|
|
|
jest.mock('react-native-device-info', () => {
|
|
|
|
return {
|
|
|
|
hasNotch: () => false,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-06-11 08:40:36 +02:00
|
|
|
// react-native-version-info doesn't work (returns undefined for .version) when
|
|
|
|
// running in a testing environment.
|
|
|
|
jest.doMock('react-native-version-info', () => {
|
|
|
|
return {
|
|
|
|
default: {
|
|
|
|
appVersion: require('./package.json').version,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-08-18 10:45:04 +02:00
|
|
|
// react-native-webview expects native iOS/Android code so needs to be mocked.
|
2024-07-18 10:44:13 +02:00
|
|
|
jest.mock('./components/ExtendedWebView', () => {
|
|
|
|
return require('./components/ExtendedWebView/index.jest.js');
|
2023-08-18 10:45:04 +02:00
|
|
|
});
|
2023-07-18 15:58:06 +02:00
|
|
|
|
2024-10-30 23:12:27 +02:00
|
|
|
jest.mock('./components/CameraView/Camera', () => {
|
|
|
|
return require('./components/CameraView/Camera/index.jest');
|
|
|
|
});
|
|
|
|
|
2024-06-11 08:40:36 +02:00
|
|
|
jest.mock('@react-native-clipboard/clipboard', () => {
|
|
|
|
return { default: { getString: jest.fn(), setString: jest.fn() } };
|
|
|
|
});
|
|
|
|
|
2024-07-26 13:35:50 +02:00
|
|
|
const emptyMockPackages = [
|
|
|
|
'react-native-share',
|
|
|
|
'react-native-file-viewer',
|
|
|
|
'react-native-image-picker',
|
|
|
|
'react-native-document-picker',
|
|
|
|
'@joplin/react-native-saf-x',
|
|
|
|
];
|
|
|
|
for (const packageName of emptyMockPackages) {
|
|
|
|
jest.doMock(packageName, () => {
|
|
|
|
return { default: { } };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
jest.mock('react-native-file-viewer', () => {
|
2024-07-18 10:44:13 +02:00
|
|
|
return { default: { } };
|
|
|
|
});
|
|
|
|
|
2024-07-26 13:35:50 +02:00
|
|
|
jest.mock('react-native-image-picker', () => {
|
|
|
|
return { default: { } };
|
|
|
|
});
|
|
|
|
|
2024-10-26 22:00:56 +02:00
|
|
|
jest.mock('react-native-zip-archive', () => {
|
|
|
|
return { default: { } };
|
|
|
|
});
|
|
|
|
|
2024-07-26 13:35:50 +02:00
|
|
|
jest.mock('react-native-document-picker', () => ({ default: { } }));
|
|
|
|
|
2024-07-18 10:44:13 +02:00
|
|
|
// Used by the renderer
|
|
|
|
jest.doMock('react-native-vector-icons/Ionicons', () => {
|
|
|
|
return {
|
|
|
|
default: class extends require('react-native').View {
|
|
|
|
static getImageSourceSync = () => ({ uri: '' });
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-07-18 15:58:06 +02:00
|
|
|
// react-native-fs's CachesDirectoryPath export doesn't work in a testing environment.
|
|
|
|
// Use a temporary folder instead.
|
|
|
|
const tempDirectoryPath = path.join(tmpdir(), `appmobile-test-${uuid.createNano()}`);
|
|
|
|
|
|
|
|
jest.doMock('react-native-fs', () => {
|
|
|
|
return {
|
|
|
|
CachesDirectoryPath: tempDirectoryPath,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-08-02 15:51:49 +02:00
|
|
|
shim.fsDriver().getCacheDirectoryPath = () => {
|
|
|
|
return tempDirectoryPath;
|
|
|
|
};
|
|
|
|
|
2023-07-18 15:58:06 +02:00
|
|
|
beforeAll(async () => {
|
|
|
|
await mkdir(tempDirectoryPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await afterEachCleanUp();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await afterAllCleanUp();
|
|
|
|
await rm(tempDirectoryPath, { recursive: true });
|
|
|
|
});
|