Skip to content

Instantly share code, notes, and snippets.

View Goldziher's full-sized avatar
🐙
Codoctupus

Na'aman Hirschfeld Goldziher

🐙
Codoctupus
  • Philipps & Byrne GmbH
  • Berlin
  • 11:39 (UTC +02:00)
  • LinkedIn in/nhirschfeld
View GitHub Profile
@Goldziher
Goldziher / backend-api.service.ts
Created August 1, 2021 06:47
Example backend api client using axios
import { UserDTO } from '@shared/dto/user.dto';
import axios, { AxiosInstance } from 'axios';
class BackendClient {
private instance: AxiosInstance;
constructor() {
this.instance = axios.create({
baseURL: process.env.BACKEND_API_URL,
headers: {
@Goldziher
Goldziher / UserProfile.spec.ts
Created August 1, 2021 06:46
Example of using FixtureFactory in a snapshot test
import { UserDTOFactory } from '@shared/factories/User.dto.factory';
import { render } from '@testing-library/react';
import UserProfile from '@frontend/pages/user-profile';
describe('UserProfile', () => {
it('matches snapshot', () => {
const user = UserDTOFactory.fixtureSync(
__dirname + '/UserProfile.spec.json',
)
const { container } = render(<UserProfile user={user} /> );
@Goldziher
Goldziher / User.dto.factory.ts
Last active August 1, 2021 06:45
Reduced code duplication factory example
export const UserDTOFactory = new FixtureFactory<UserDTO>(() => {
const { profile, ...user } = UserFactory.buildSync();
return {
...user,
...profile,
};
});
@Goldziher
Goldziher / User.dto.factory.ts
Created August 1, 2021 06:43
Example UserDTO factory
import * as faker from 'faker';
import { FixtureFactory } from 'interface-forge';
import { UserDTO } from '@shared/dto/User.dto';
export const UserDTOFactory = new FixtureFactory<UserDTO>(() => ({
id: faker.datatype.uuid(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
isActive: true,
email: faker.internet.email(),
@Goldziher
Goldziher / User.dto.ts
Created August 1, 2021 06:42
Example UserDTO class
export class UserDTO {
id: string;
firstName: string;
lastName: string;
email: string;
isActive: boolean;
location: string;
employer: string;
profession: string;
phoneNumber: string;
@Goldziher
Goldziher / factories.ts
Created August 1, 2021 06:40
Re-written factory using EntityInterface helper
export const UserFactory = new TypeFactory<
EntityInterface<User, AbstractUUIDPrimaryKeyEntity>
>(() => ({
...
}));
@Goldziher
Goldziher / Types.ts
Created August 1, 2021 06:39
EntityInterface helper type
export type EntityInterface<Entity, Base> = {
[K in keyof Omit<Entity, keyof Base>]: K extends Base
? Omit<Entity, keyof Base>
: Entity[K];
};
@Goldziher
Goldziher / User.ts
Created August 1, 2021 06:38
Example of abstract class inheriting from BaseEntity
export abstract class AbstractUUIDPrimaryKeyEntity extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@CreateDateColumn()
createdDate: Date;
@UpdateDateColumn()
updatedDate: Date;
}
@Goldziher
Goldziher / some-test.spec.ts
Created August 1, 2021 06:37
Re-written test example with batch
import { User } from '@backend/db/entities/User';
import { UserFactory } from '@backend/db/factories/User.factory';
import { getConnection } from 'typeorm';
describe('Some Other Test', () => {
const connection = getConnection('default');
let users: User[];
beforeEach(async () => {
const userRepository = connection.getRepository(User);
@Goldziher
Goldziher / some-test.spec.ts
Created August 1, 2021 06:36
Re-written test using factories example
import { User } from '@backend/db/entities/User';
import { UserFactory } from '@backend/db/factories/User.factory';
import { getConnection } from 'typeorm';
describe('Some Test', () => {
const connection = getConnection('default');
let user: User;
beforeEach(async () => {
const userRepository = connection.getRepository(User);