Skip to content

Latest commit

 

History

History
158 lines (112 loc) · 5.31 KB

API.md

File metadata and controls

158 lines (112 loc) · 5.31 KB

API Reference

Only MockFirebase methods are included here. For details on normal Firebase API methods, consult the Firebase Web API documentation.

Core

Core methods of MockFirebase references for manipulating data and asynchronous behavior.

flush([delay]) -> ref

Flushes the queue of deferred data and authentication operations. If a delay is passed, the flush operation will be triggered after the specified number of milliseconds.

In MockFirebase, data operations can be executed synchronously. When calling any Firebase API method that reads or writes data (e.g. set(data) or on('value')), MockFirebase will queue the operation. You can call multiple data methods in a row before flushing. MockFirebase will execute them in the order they were called when flush is called.

flush will throw an exception if the queue of deferred operations is empty.

Example:

ref.set({
  foo: 'bar'
});
assert(ref.getData() === null);
ref.flush();
assert(ref.getData().foo === 'bar');

autoFlush([delay|setting]) -> ref

Configures the Firebase reference to automatically flush data and authentication operations when run. If no arguments or true are passed, the operations will be flushed immediately (synchronously). If a delay is provided, the operations will be flushed after the specified number of milliseconds. If false is provided, autoFlush will be disabled.


failNext(method, err) -> undefined

When method is next invoked, trigger the onComplete callback with the specified err. This is useful for simulating validation, authorization, or any other errors. The callback will be triggered with the next flush.

Example:

var error = new Error('Oh no!');
ref.failNext('set', error);
var err;
ref.set('data', function onComplete (_err_) {
  err = _err_;
});
assert(typeof err === 'undefined');
ref.flush();
assert(err === error);

forceCancel(err [, event] [, callback] [, context] -> undefined

Simulate a security error by cancelling listeners (callbacks registered with on) at the path with the specified err. If an optional event, callback, and context are provided, only listeners that match will be cancelled. forceCancel will also invoke off for the matched listeners so they will be no longer notified of any future changes. Cancellation is triggered immediately and not with a flush call.

Example:

var cancelled = false;
var error = new Error();
function onValue (snapshot) {}
function onCancel (_err_) {
  cancelled = true;
  err = _err_; 
}
ref.on('value', onValue, onCancel);
ref.flush();
ref.forceCancel(error, 'value', onValue);
assert(err === error);
assert(cancelled);

getData() -> Any

Returns a copy of the data as it exists at the time. Any writes must be triggered with flush before getData will reflect their results.


getKeys() -> Array

Returns an array of the keys at the path as they are ordered in Firebase.


fakeEvent(event [, key] [, data] [, previousChild] [, priority]) -> ref

Triggers a fake event that is not connected to an actual change to Firebase data. A child key is required unless the event is a 'value' event.

Example:

var snapshot;
function onValue (_snapshot_) {
  snapshot = _snapshot_;
}
ref.on('value', onValue);
ref.set({
  foo: 'bar';
});
ref.flush();
assert(ref.getData().foo === 'bar');
ref.fakeEvent('value', undefined, null);
ref.flush();
assert(ref.getData() === null);

Auth

Authentication methods for simulating changes to the auth state of a Firebase reference.

changeAuthState(authData) -> undefined

Changes the active authentication credentials to the authData object. Before changing the authentication state, changeAuthState checks whether the authData object is deeply equal to the current authentication data. onAuth listeners will only be triggered if the data is not deeply equal. To simulate no user being authenticated, pass null for authData. This operation is queued until the next flush.

authData should adhere to the documented schema.

Example:

ref.changeAuthState({
  uid: 'theUid',
  provider: 'github',
  token: 'theToken',
  expires: Math.floor(new Date() / 1000) + 24 * 60 * 60, // expire in 24 hours
  auth: {
    myAuthProperty: true
  }
});
ref.flush();
assert(ref.getAuth().auth.myAuthProperty);

getEmailUser(email) -> Object|null

Finds a user previously created with createUser. If no user was created with the specified email, null is returned instead.