mockbase
Version:
Firebase v7+ mock.
66 lines • 2.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserStore = void 0;
const user_1 = require("./user");
class UserStore {
constructor() {
this.nextId = 0;
this.store = new Map();
this.socialMocks = [];
}
/**
* Adds a user to this store with the given data.
* The new user is assigned a new ID and is returned.
*/
add(data) {
const uid = ++this.nextId + "";
const user = new user_1.User(Object.assign(Object.assign({}, data), {
// If the user is being created with a provider ID, then its data is coming from that provider.
providerData: data.providerId ? [new user_1.UserInfo(uid, data.providerId, data)] : [], uid }), this);
const schema = user.toJSON();
this.store.set(schema.uid, schema);
return user;
}
addSocialMock(mock) {
this.socialMocks.push(mock);
}
/**
* Finds the mock response for a given AuthProvider, removes it from the list of mocks
* and returns its response.
*
* If a mock for that AuthProvider is not set, then this method throws.
*/
consumeSocialMock(provider) {
const index = this.socialMocks.findIndex((mock) => mock.type === provider.providerId);
if (index === -1) {
throw new Error("No mock response set.");
}
// Mock is used, then it must go
const mock = this.socialMocks[index];
this.socialMocks.splice(index, 1);
return mock.response;
}
findByEmail(email) {
const schema = [...this.store.values()].find((user) => user.email === email);
return schema && new user_1.User(schema, this);
}
findByProviderAndEmail(email, providerId) {
const user = this.findByEmail(email);
if (!user) {
return undefined;
}
if (user.providerData.some((info) => info.providerId === providerId)) {
return new user_1.User(Object.assign(Object.assign({}, user.toJSON()), { providerId }), this);
}
throw new Error("auth/account-exists-with-different-credential");
}
update(id, data) {
const schema = this.store.get(id);
if (!schema) {
return;
}
Object.assign(schema, data);
}
}
exports.UserStore = UserStore;
//# sourceMappingURL=user-store.js.map