Last active
October 19, 2019 00:07
-
-
Save danahartweg/6efe76f7ac4e46a7d8d2428f315860a5 to your computer and use it in GitHub Desktop.
Test helper constants - Unit testing Cloud Firestore
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
import * as uuid from 'uuid/v4'; | |
type Collections = | |
| 'catchAlls' | |
| 'homesteads' | |
| 'users'; | |
export enum COLLECTIONS { | |
CATCH_ALL = 'catchAlls', | |
HOMESTEADS = 'homesteads', | |
USERS = 'users', | |
} | |
export function generateMockDocument(data: Object = {}): Object { | |
return { name: 'document name', ...data }; | |
} | |
export function generateMockUpdateDocument(data: Object = {}): Object { | |
return { name: 'updated document name', ...data }; | |
} | |
export function generateMockDocumentWithHomesteadId(homesteadId: string) { | |
return generateMockDocument({ homesteadId }); | |
} | |
export function generateMockUpdateDocumentWithHomesteadId(homesteadId: string) { | |
return generateMockUpdateDocument({ homesteadId }); | |
} | |
export function generateSecurityRecordAny(): Object { | |
return { role: 'any' }; | |
} | |
export function generateSecurityRecordOwner(): Object { | |
return { role: 'owner' }; | |
} | |
export function documentPath(...parts: string[]): string { | |
return parts.join('/'); | |
} | |
export function membershipPath( | |
collection: Collections, | |
recordId: string, | |
userId: string | |
): string { | |
return documentPath(collection, recordId, 'members', userId); | |
} | |
export function generateId({ | |
append = '', | |
prepend = '', | |
}: { | |
append?: string; | |
prepend?: string; | |
} = {}): string { | |
let id = uuid(); | |
if (prepend) { | |
id = `${prepend}-${id}`; | |
} | |
if (append) { | |
id += `-${append}`; | |
} | |
return id; | |
} | |
export function generateUserId(): string { | |
return generateId({ prepend: 'USER' }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment