Fix unit test setup

This commit is contained in:
Elias Nahum
2021-06-09 15:09:23 -04:00
parent 48c7eaecf5
commit ff8a09c9e1
6 changed files with 91 additions and 167 deletions

View File

@@ -716,7 +716,7 @@ SPEC CHECKSUMS:
EXConstants: c00cd53a17a65b2e53ddb3890e4e74d3418e406e
EXFileSystem: 35769beb727d5341d1276fd222710f9704f7164e
FBLazyVector: 49cbe4b43e445b06bf29199b6ad2057649e4c8f5
FBReactNativeSpec: ebaa990b13e6f0496fd41894a824c585c4afab46
FBReactNativeSpec: a804c9d6c798f94831713302354003ee54ea18cb
glog: 73c2498ac6884b13ede40eda8228cb1eee9d9d62
jail-monkey: 80c9e34da2cd54023e5ad08bf7051ec75bd43d5b
libwebp: 946cb3063cea9236285f7e9a8505d806d30e07f3

12
package-lock.json generated
View File

@@ -3415,13 +3415,6 @@
"rambdax": "2.15.0",
"rxjs": "^6.5.3",
"sql-escape-string": "^1.1.0"
},
"dependencies": {
"lokijs": {
"version": "npm:@nozbe/lokijs@1.5.12-wmelon",
"resolved": "https://registry.npmjs.org/@nozbe/lokijs/-/lokijs-1.5.12-wmelon.tgz",
"integrity": "sha512-7xQUn80pzPBB9VcwvB/W2V9/60xIfuk+3IDIvS9cU7W29jJx4QBXe5dBWTaARmxD9hXozPCcPWh2wfd7m4dbTA=="
}
}
},
"@nozbe/with-observables": {
@@ -16601,6 +16594,11 @@
}
}
},
"lokijs": {
"version": "npm:@nozbe/lokijs@1.5.12-wmelon",
"resolved": "https://registry.npmjs.org/@nozbe/lokijs/-/lokijs-1.5.12-wmelon.tgz",
"integrity": "sha512-7xQUn80pzPBB9VcwvB/W2V9/60xIfuk+3IDIvS9cU7W29jJx4QBXe5dBWTaARmxD9hXozPCcPWh2wfd7m4dbTA=="
},
"loose-envify": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz",

View File

@@ -1,33 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {IntlProvider, intlShape} from 'react-intl';
import {mount, shallow} from 'enzyme';
import {getTranslations} from '@i18n';
const intlProvider = new IntlProvider({locale: 'en'}, {});
const {intl} = intlProvider.getChildContext();
export function shallowWithIntl(node, {context} = {}) {
return shallow(React.cloneElement(node, {intl}), {
context: Object.assign({}, context, {intl}),
});
}
export function shallowWithIntlMessages(node, {context} = {}) {
const provider = new IntlProvider({locale: 'en', messages: getTranslations('en')}, {});
const {intl: intlWithMessages} = provider.getChildContext();
return shallow(React.cloneElement(node, {intl: intlWithMessages}), {
context: Object.assign({}, context, {intl: intlWithMessages}),
});
}
export function mountWithIntl(node, {context, childContextTypes} = {}) {
return mount(React.cloneElement(node, {intl}), {
context: Object.assign({}, context, {intl}),
childContextTypes: Object.assign({}, {intl: intlShape}, childContextTypes),
});
}

25
test/intl-test-helper.tsx Normal file
View File

@@ -0,0 +1,25 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {getTranslations} from '@i18n';
import {render} from '@testing-library/react-native';
import React, {ReactElement} from 'react';
import {IntlProvider} from 'react-intl';
export function renderWithIntl(ui: ReactElement, {locale = 'en', ...renderOptions} = {}) {
function Wrapper({children}: {children: ReactElement}) {
return (
<IntlProvider
locale={locale}
messages={getTranslations(locale)}
>
{children}
</IntlProvider>
);
}
return render(ui, {wrapper: Wrapper, ...renderOptions});
}
// eslint-disable-next-line no-duplicate-imports
export * from '@testing-library/react-native';

View File

@@ -5,19 +5,17 @@
import * as ReactNative from 'react-native';
// import MockAsyncStorage from 'mock-async-storage';
// import {configure} from 'enzyme';
// import Adapter from 'enzyme-adapter-react-16';
import MockAsyncStorage from 'mock-async-storage';
import 'react-native-gesture-handler/jestSetup';
require('react-native-reanimated/lib/reanimated2/jestUtils').setUpTests();
require('isomorphic-fetch');
// configure({adapter: new Adapter()});
const mockImpl = new MockAsyncStorage();
jest.mock('@react-native-community/async-storage', () => mockImpl);
// const mockImpl = new MockAsyncStorage();
// jest.mock('@react-native-community/async-storage', () => mockImpl);
// global.window = {};
// @ts-expect-error no window exist in global
global.window = {};
/* eslint-disable no-console */
@@ -51,14 +49,6 @@ jest.doMock('react-native', () => {
directEventTypes: {},
},
},
BlurAppScreen: () => true,
MattermostManaged: {
getConfig: jest.fn(),
},
MattermostShare: {
close: jest.fn(),
cacheDirName: 'mmShare',
},
PlatformConstants: {
forceTouchAvailable: false,
},
@@ -71,6 +61,9 @@ jest.doMock('react-native', () => {
},
},
KeyboardObserver: {},
JailMonkey: {
trustFall: jest.fn().mockReturnValue(true),
},
RNCNetInfo: {
getCurrentState: jest.fn().mockResolvedValue({isConnected: true}),
addListener: jest.fn(),
@@ -85,9 +78,6 @@ jest.doMock('react-native', () => {
RNReactNativeHapticFeedback: {
trigger: jest.fn(),
},
StatusBarManager: {
getHeight: jest.fn(),
},
RNDocumentPicker: {
pick: jest.fn(),
},
@@ -108,6 +98,12 @@ jest.doMock('react-native', () => {
...Platform,
OS: 'ios',
Version: 12,
constants: {
reactNativeVersion: {
major: 0,
minor: 64,
},
},
},
StyleSheet,
ViewPropTypes,
@@ -141,6 +137,17 @@ jest.mock('react-native-vector-icons', () => {
};
});
jest.mock('react-native-unimodules', () => ({
FileSystem: {
cacheDirectory: 'root/cache',
documentDirectory: 'root/documents',
deleteAsync: jest.fn().mockResolvedValue(true),
getInfoAsync: jest.fn().mockResolvedValue({exists: false}),
makeDirectoryAsync: jest.fn().mockResolvedValue(true),
readDirectoryAsync: jest.fn().mockResolvedValue([]),
},
}));
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
jest.mock('../node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter');
@@ -155,32 +162,6 @@ jest.mock('react-native-device-info', () => {
};
});
jest.mock('rn-fetch-blob', () => ({
fs: {
dirs: {
DocumentDir: '/data/com.mattermost.beta/documents',
CacheDir: '/data/com.mattermost.beta/cache',
},
exists: jest.fn(),
existsWithDiffExt: jest.fn(),
unlink: jest.fn(),
mv: jest.fn(),
},
fetch: jest.fn(),
config: jest.fn(),
}));
jest.mock('rn-fetch-blob/fs', () => ({
dirs: {
DocumentDir: () => jest.fn(),
CacheDir: '/data/com.mattermost.beta/cache',
},
exists: jest.fn(),
existsWithDiffExt: jest.fn(),
unlink: jest.fn(),
mv: jest.fn(),
}));
jest.mock('react-native-localize', () => ({
getTimeZone: () => 'World/Somewhere',
getLocales: () => ([
@@ -190,24 +171,20 @@ jest.mock('react-native-localize', () => ({
]),
}));
// jest.mock('react-native-cookies', () => ({
// addEventListener: jest.fn(),
// removeEventListener: jest.fn(),
// openURL: jest.fn(),
// getInitialURL: jest.fn(),
// clearAll: jest.fn(),
// get: () => Promise.resolve(({
// res: {
// MMCSRF: {
// value: 'the cookie',
// },
// },
// })),
// }));
// jest.mock('react-native-image-picker', () => ({
// launchCamera: jest.fn(),
// }));
jest.mock('@react-native-community/cookies', () => ({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
openURL: jest.fn(),
getInitialURL: jest.fn(),
clearAll: jest.fn(),
get: () => Promise.resolve(({
res: {
MMCSRF: {
value: 'the cookie',
},
},
})),
}));
jest.mock('react-native-navigation', () => {
const RNN = jest.requireActual('react-native-navigation');
@@ -273,71 +250,24 @@ jest.mock('react-native-share', () => ({
default: jest.fn(),
}));
// jest.mock('app/actions/navigation', () => ({
// resetToChannel: jest.fn(),
// resetToSelectServer: jest.fn(),
// resetToTeams: jest.fn(),
// goToScreen: jest.fn(),
// popTopScreen: jest.fn(),
// showModal: jest.fn(),
// showModalOverCurrentContext: jest.fn(),
// showSearchModal: jest.fn(),
// setButtons: jest.fn(),
// showOverlay: jest.fn(),
// mergeNavigationOptions: jest.fn(),
// popToRoot: jest.fn(() => Promise.resolve()),
// dismissModal: jest.fn(() => Promise.resolve()),
// dismissAllModals: jest.fn(() => Promise.resolve()),
// dismissOverlay: jest.fn(() => Promise.resolve()),
// }));
jest.mock('@screens/navigation', () => ({
resetToChannel: jest.fn(),
resetToSelectServer: jest.fn(),
resetToTeams: jest.fn(),
goToScreen: jest.fn(),
popTopScreen: jest.fn(),
showModal: jest.fn(),
showModalOverCurrentContext: jest.fn(),
showSearchModal: jest.fn(),
setButtons: jest.fn(),
showOverlay: jest.fn(),
mergeNavigationOptions: jest.fn(),
popToRoot: jest.fn(() => Promise.resolve()),
dismissModal: jest.fn(() => Promise.resolve()),
dismissAllModals: jest.fn(() => Promise.resolve()),
dismissOverlay: jest.fn(() => Promise.resolve()),
}));
// jest.mock('app/utils/file', () => {
// const file = jest.requireActual('../app/utils/file');
//
// return {
// ...file,
// generateId: jest.fn().mockReturnValue('123'),
// };
// });
let logs = [];
let warns = [];
let errors = [];
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.originalLog = console.log;
console.log = jest.fn((...params) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.originalLog(...params);
logs.push(params);
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.originalWarn = console.warn;
console.warn = jest.fn((...params) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.originalWarn(...params);
warns.push(params);
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.originalError = console.error;
console.error = jest.fn((...params) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.originalError(...params);
errors.push(params);
});
});
beforeEach(() => {
logs = [];
warns = [];
errors = [];
});
declare const global: {requestAnimationFrame: (callback: any) => void};
global.requestAnimationFrame = (callback) => {
setTimeout(callback, 0);

4
types/modules/mock-async-storage.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
declare module 'mock-async-storage';