-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Closed
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bbd774b
Add simple test for Ion
marcaaron 8386463
add comments
marcaaron 6e61797
add test for Ion.merge
marcaaron cb9e3dc
add tests for not notifying subscribers, merge, etc
marcaaron 3751973
fix style
marcaaron 593721c
test array appending
marcaaron 805db2f
fix style
marcaaron d3591fb
Test that withIon can be used to render a text result
marcaaron e5800c6
Add tests for withIon
marcaaron dbf5f3d
get rid of console log error
marcaaron e62e750
Merge remote-tracking branch 'origin' into marcaaron-ionTest
marcaaron f74a194
change name to resolveAllPromises
marcaaron 18b9599
add the withIon test
marcaaron 16ae058
fix style
marcaaron 5455712
add test components folder
marcaaron 1b3d953
fix style
marcaaron 38eafcc
Merge remote-tracking branch 'origin' into marcaaron-ionTest
marcaaron fa65f33
Ion to Onyx
marcaaron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import 'react-native'; | ||
import Ion from '../../src/libs/Ion'; | ||
|
||
const TEST_KEY = 'test'; | ||
|
||
jest.mock('../../node_modules/@react-native-community/async-storage', | ||
() => require('./mocks/@react-native-community/async-storage')); | ||
|
||
Ion.registerLogger(() => {}); | ||
Ion.init({ | ||
keys: { | ||
TEST_KEY, | ||
COLLECTION: {}, | ||
}, | ||
}); | ||
|
||
describe('Ion', () => { | ||
let connectionID; | ||
|
||
afterEach((done) => { | ||
Ion.disconnect(connectionID); | ||
Ion.clear().then(done); | ||
}); | ||
|
||
it('should set a simple key', (done) => { | ||
const mockCallback = jest.fn(); | ||
connectionID = Ion.connect({ | ||
key: TEST_KEY, | ||
initWithStoredValues: false, | ||
callback: (value) => { | ||
mockCallback(value); | ||
|
||
try { | ||
expect(value).toBe('test'); | ||
done(); | ||
} catch (error) { | ||
done(error); | ||
} | ||
} | ||
}); | ||
|
||
// Set a simple key | ||
Ion.set(TEST_KEY, 'test'); | ||
}); | ||
|
||
it('should merge an object with another object', (done) => { | ||
const mockCallback = jest.fn(); | ||
connectionID = Ion.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); | ||
} | ||
} | ||
}); | ||
|
||
Ion.set(TEST_KEY, {test1: 'test1'}); | ||
Ion.merge(TEST_KEY, {test2: 'test2'}); | ||
}); | ||
|
||
it('should notify subscribers when data has been cleared', (done) => { | ||
const mockCallback = jest.fn(); | ||
connectionID = Ion.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); | ||
} | ||
} | ||
}); | ||
|
||
Ion.set(TEST_KEY, 'test'); | ||
Ion.clear(); | ||
}); | ||
|
||
it('should not notify subscribers after they have disconnected', (done) => { | ||
const mockCallback = jest.fn(); | ||
connectionID = Ion.connect({ | ||
key: TEST_KEY, | ||
initWithStoredValues: false, | ||
callback: (value) => { | ||
mockCallback(value); | ||
expect(value).toBe('test'); | ||
} | ||
}); | ||
|
||
Ion.set(TEST_KEY, 'test') | ||
.then(() => { | ||
Ion.disconnect(connectionID); | ||
return Ion.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 = Ion.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); | ||
} | ||
} | ||
}); | ||
|
||
Ion.set(TEST_KEY, ['test1']); | ||
Ion.merge(TEST_KEY, ['test2', 'test3', 'test4']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Ion from '../../src/libs/Ion'; | ||
import withIon from '../../src/components/withIon'; | ||
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')); | ||
|
||
Ion.registerLogger(() => {}); | ||
Ion.init({ | ||
keys: { | ||
TEST_KEY, | ||
COLLECTION: {}, | ||
}, | ||
}); | ||
|
||
describe('withIon', () => { | ||
it('should render with the test data when using withIon', () => { | ||
let result; | ||
|
||
Ion.set(TEST_KEY, 'test1') | ||
.then(() => { | ||
const TestComponentWithIon = withIon({ | ||
text: { | ||
key: TEST_KEY, | ||
}, | ||
})(ViewWithText); | ||
|
||
result = render(<TestComponentWithIon />); | ||
return waitForPromisesToResolve(); | ||
}) | ||
.then(() => { | ||
const textComponent = result.getByText('test1'); | ||
expect(textComponent).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => new Promise(setImmediate); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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