Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOLD] Add Automated Tests for Onyx / withOnyx #768

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"android-build": "fastlane android build",
"test": "jest",
"lint": "eslint .",
"lint-tests": "eslint tests/**",
"postversion": "react-native-version",
"print-version": "echo $npm_package_version",
"detox-build": "detox build --configuration ios.sim.debug",
Expand Down
1 change: 1 addition & 0 deletions src/CONFIG.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {
},
// eslint-disable-next-line no-undef
IS_IN_PRODUCTION: Platform.OS === 'web' ? process.env.NODE_ENV === 'production' : !__DEV__,
IS_JEST_RUNNING: typeof jest !== 'undefined',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depending on which is merged first you might get a conflict with this https://github.com/Expensify/ReactNativeChat/pull/771/files

PUSHER: {
APP_KEY: Config.PUSHER_APP_KEY,
CLUSTER: 'mt1',
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ export default new Logger({
clientLoggingCallback: (message) => {
console.debug(message);
},
isDebug: !CONFIG.IS_IN_PRODUCTION,
isDebug: !CONFIG.IS_JEST_RUNNING && !CONFIG.IS_IN_PRODUCTION,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the issue where we were console logging after the test has finished and Jest complains locally.

});
16 changes: 16 additions & 0 deletions tests/components/ViewWithText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import PropTypes from 'prop-types';
import {View, Text} from 'react-native';

const propTypes = {
text: PropTypes.string.isRequired,
};

const ViewWithText = props => (
<View>
<Text>{props.text}</Text>
</View>
);

ViewWithText.propTypes = propTypes;
export default ViewWithText;
155 changes: 155 additions & 0 deletions tests/unit/onyxTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import 'react-native';
import Onyx from 'react-native-onyx';

const TEST_KEY = 'test';

jest.mock('../../node_modules/@react-native-community/async-storage',
() => require('./mocks/@react-native-community/async-storage'));

Onyx.registerLogger(() => {});
Onyx.init({
keys: {
TEST_KEY,
COLLECTOnyx: {},
},
registerStorageEventListener: () => {},
});

describe('Onyx', () => {
let connectionID;

afterEach((done) => {
Onyx.disconnect(connectionID);
Onyx.clear().then(done);
});

it('should set a simple key', (done) => {
const mockCallback = jest.fn();
connectionID = Onyx.connect({
key: TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
mockCallback(value);

try {
expect(value).toBe('test');
done();
} catch (error) {
done(error);
}
}
});

// Set a simple key
Onyx.set(TEST_KEY, 'test');
});

it('should merge an object with another object', (done) => {
const mockCallback = jest.fn();
connectionID = Onyx.connect({
key: TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
mockCallback(value);

try {
if (mockCallback.mock.calls.length === 1) {
expect(value).toStrictEqual({
test1: 'test1',
});
return;
}

expect(value).toStrictEqual({
test1: 'test1',
test2: 'test2',
});
done();
} catch (error) {
done(error);
}
}
});

Onyx.set(TEST_KEY, {test1: 'test1'});
Onyx.merge(TEST_KEY, {test2: 'test2'});
});

it('should notify subscribers when data has been cleared', (done) => {
const mockCallback = jest.fn();
connectionID = Onyx.connect({
key: TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
mockCallback(value);

try {
if (mockCallback.mock.calls.length === 1) {
expect(value).toBe('test');
return;
}

expect(value).toBe(null);
done();
} catch (error) {
done(error);
}
}
});

Onyx.set(TEST_KEY, 'test');
Onyx.clear();
});

it('should not notify subscribers after they have disconnected', (done) => {
const mockCallback = jest.fn();
connectionID = Onyx.connect({
key: TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
mockCallback(value);
expect(value).toBe('test');
}
});

Onyx.set(TEST_KEY, 'test')
.then(() => {
Onyx.disconnect(connectionID);
return Onyx.set(TEST_KEY, 'test updated');
})
.then(() => {
try {
expect(mockCallback.mock.calls.length).toBe(1);
done();
} catch (error) {
done(error);
}
});
});

it('should merge arrays by appending new items to the end of a value', (done) => {
const mockCallback = jest.fn();
connectionID = Onyx.connect({
key: TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
mockCallback(value);

try {
if (mockCallback.mock.calls.length === 1) {
expect(value).toStrictEqual(['test1']);
return;
}

expect(value).toStrictEqual(['test1', 'test2', 'test3', 'test4']);
done();
} catch (err) {
done(err);
}
}
});

Onyx.set(TEST_KEY, ['test1']);
Onyx.merge(TEST_KEY, ['test2', 'test3', 'test4']);
});
});
43 changes: 43 additions & 0 deletions tests/unit/withOnyxTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'react-native';
import {render} from '@testing-library/react-native';
import React from 'react';
import Onyx, {withOnyx} from 'react-native-onyx';
import ViewWithText from '../components/ViewWithText';

import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';

const TEST_KEY = 'test';

jest.mock('../../node_modules/@react-native-community/async-storage',
() => require('./mocks/@react-native-community/async-storage'));

Onyx.registerLogger(() => {});
Onyx.init({
keys: {
TEST_KEY,
COLLECTION: {},
},
registerStorageEventListener: () => {},
});

describe('withOnyx', () => {
it('should render with the test data when using withOnyx', () => {
let result;

Onyx.set(TEST_KEY, 'test1')
.then(() => {
const TestComponentWithOnyx = withOnyx({
text: {
key: TEST_KEY,
},
})(ViewWithText);

result = render(<TestComponentWithOnyx />);
return waitForPromisesToResolve();
})
.then(() => {
const textComponent = result.getByText('test1');
expect(textComponent).toBeTruthy();
});
});
});
1 change: 1 addition & 0 deletions tests/utils/waitForPromisesToResolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => new Promise(setImmediate);