Skip to content

Commit

Permalink
feat(storage, emulator): implement storage emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehardy committed May 19, 2021
1 parent 69b6f88 commit 1d3e946
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 117 deletions.
3 changes: 3 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ jest.doMock('react-native', () => {
settings: jest.fn(),
},
RNFBPerfModule: {},
RNFBStorageModule: {
useEmulator: jest.fn(),
},
},
},
ReactNative,
Expand Down
2 changes: 0 additions & 2 deletions packages/app/lib/internal/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const APP_NATIVE_MODULE = 'RNFBAppModule';
export const DEFAULT_APP_NAME = '[DEFAULT]';

export const KNOWN_NAMESPACES = [
'admob',
'auth',
'analytics',
'remoteConfig',
Expand All @@ -29,7 +28,6 @@ export const KNOWN_NAMESPACES = [
'inAppMessaging',
'firestore',
'functions',
'iid',
'indexing',
'storage',
'dynamicLinks',
Expand Down
46 changes: 46 additions & 0 deletions packages/storage/__tests__/storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import storage, { firebase } from '../lib';

describe('Storage', function () {
describe('namespace', function () {
it('accessible from firebase.app()', function () {
const app = firebase.app();
expect(app.storage).toBeDefined();
expect(app.storage().useEmulator).toBeDefined();
});
});

describe('useEmulator()', function () {
it('useEmulator requires a string host', function () {
// @ts-ignore because we pass an invalid argument...
expect(() => storage().useEmulator()).toThrow(
'firebase.storage().useEmulator() takes a non-empty host',
);
expect(() => storage().useEmulator('', -1)).toThrow(
'firebase.storage().useEmulator() takes a non-empty host',
);
// @ts-ignore because we pass an invalid argument...
expect(() => storage().useEmulator(123)).toThrow(
'firebase.storage().useEmulator() takes a non-empty host',
);
});

it('useEmulator requires a host and port', function () {
expect(() => storage().useEmulator('', 9000)).toThrow(
'firebase.storage().useEmulator() takes a non-empty host and port',
);
// No port
// @ts-ignore because we pass an invalid argument...
expect(() => storage().useEmulator('localhost')).toThrow(
'firebase.storage().useEmulator() takes a non-empty host and port',
);
});

it('useEmulator -> remaps Android loopback to host', function () {
const foo = storage().useEmulator('localhost', 9000);
expect(foo).toEqual(['10.0.2.2', 9000]);

const bar = storage().useEmulator('127.0.0.1', 9000);
expect(bar).toEqual(['10.0.2.2', 9000]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ public void setMaxUploadRetryTime(String appName, double milliseconds, Promise p
promise.resolve(null);
}

/**
* @link https://firebase.google.com/docs/reference/js/firebase.storage.Storage#useEmulator
*/
@ReactMethod
public void useEmulator(String appName, String host, int port, Promise promise) {
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance(firebaseApp);
firebaseStorage.useEmulator(host, port);
promise.resolve(null);
}

/**
* @link https://firebase.google.com/docs/reference/js/firebase.storage.Reference#writeToFile
*/
Expand Down
Loading

0 comments on commit 1d3e946

Please sign in to comment.