Skip to content

Commit

Permalink
ZulipAsyncStorage: Add a test for setItem.
Browse files Browse the repository at this point in the history
With a new mock for `TextCompressionModule`; we do that globally in
case it's useful elsewhere. That module should grow its own set of
unit tests.
  • Loading branch information
chrisbobbe committed Apr 29, 2021
1 parent 8d23f74 commit 60303c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions jest/jestSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ jest.mock('react-native', () => {
resourceURL:
'file:///private/var/containers/Bundle/Application/4DCD4D2B-F745-4C70-AD74-8E5F690CF593/ZulipMobile.app/',
};
} else if (ReactNative.Platform.OS === 'android') {
const header = 'z|mock|';
ReactNative.NativeModules.TextCompressionModule = {
header,
compress: async (input: string) => `${header}${input}`,
decompress: async (input: string) => input.replace(header, ''),
};
}

return ReactNative;
Expand Down
33 changes: 33 additions & 0 deletions src/boot/__tests__/ZulipAsyncStorage-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* @flow strict-local */
import { NativeModules, Platform } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';

import ZulipAsyncStorage from '../ZulipAsyncStorage';

// Some of these tests here are written to differ slightly between iOS
// and Android, which might make them a bit harder to understand. But
// most of the logic is appropriately in common between the platforms,
// so we haven't split the different logic up into different tests.
//
// We may in the future decide to add (or replace this file with) a
// `ZulipAsyncStorage-test.ios.js` and
// `ZulipAsyncStorage-test.android.js`, to target entire test suites
// to Android and iOS.
describe('ZulipAsyncStorage', () => {
describe('setItem', () => {
const setItemSpy = jest.spyOn(AsyncStorage, 'setItem');

const key = 'foo!';
const value = '123!';
const callback = err => {};

test('calls AsyncStorage.setItem as we expect it to', async () => {
await ZulipAsyncStorage.setItem(key, value, callback);

const expectedValue =
Platform.OS === 'ios' ? value : await NativeModules.TextCompressionModule.compress(value);
expect(setItemSpy).toHaveBeenCalledTimes(1);
expect(setItemSpy).toHaveBeenCalledWith(key, expectedValue, callback);
});
});
});

0 comments on commit 60303c7

Please sign in to comment.