From 3b1c995bf777648805b710aee28b38d21abf55dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oriol=20Jane=CC=81s=20Pereira?= Date: Mon, 11 Oct 2021 11:54:06 +0200 Subject: [PATCH] Mock data source refactor and example --- .../data-source/mock.data-source.ts | 138 +++++++++++++++--- 1 file changed, 117 insertions(+), 21 deletions(-) diff --git a/packages/core/src/repository/data-source/mock.data-source.ts b/packages/core/src/repository/data-source/mock.data-source.ts index 654d1865..f3668e0e 100644 --- a/packages/core/src/repository/data-source/mock.data-source.ts +++ b/packages/core/src/repository/data-source/mock.data-source.ts @@ -1,35 +1,131 @@ -import {DeleteDataSource, GetDataSource, PutDataSource} from './data-source'; -import {Query} from '..'; -import {DeviceConsoleLogger, Logger} from '../../helpers'; +import { IdQuery, Query } from "../query/query"; +import { SingleDataSourceRepository } from "../single-data-source.repository"; +import { DeleteDataSource, GetDataSource, PutDataSource } from "./data-source"; -export class MockDataSource implements GetDataSource, PutDataSource, DeleteDataSource { - constructor( - private readonly one: T, - private readonly many: T[], - private readonly logger: Logger = new DeviceConsoleLogger(), - ) {} +export class MockDataSource + implements + GetDataSource, + PutDataSource, + DeleteDataSource { + constructor( + protected readonly itemsFactory: MockItemDataSourceFactory, + protected readonly delay = 0, + ) {} + + async get(query: Query): Promise { + await this.sourceDelay(); + return this.itemsFactory.getGetItem(query); + } + + async getAll(query: Query): Promise { + await this.sourceDelay(); + return this.itemsFactory.getGetAllItems(query); + } + + async put( + value: T, + query: Query, + ): Promise { + await this.sourceDelay(); + return this.itemsFactory.getPutItem(value, query); + } + + async putAll( + values: T[], + query: Query, + ): Promise { + await this.sourceDelay(); + return this.itemsFactory.getPutAllItems(values, query); + } + + async delete(query: Query): Promise { + await this.sourceDelay(); + return Promise.resolve(); + } + + private sourceDelay(): Promise { + return new Promise(resolve => setTimeout(resolve, this.delay)); + } +} + +export interface MockItemDataSourceFactory { + getGetItem(query?: Query): T; + getGetAllItems(query?: Query): T[]; + getPutItem(value?: T, query?: Query): T; + getPutAllItems(values?: T[], query?: Query): T[]; +} + +export abstract class DefaultMockItemDataSourceFactory implements MockItemDataSourceFactory { + + protected get randId() { + return Math.floor(Math.random() * 10000); + } + + protected get randUid() { + return Math.random().toString(36).substr(2, 9); + } - public async get(query: Query): Promise { - return this.one; + public abstract getGetItem(query?: Query): T; + + public abstract getGetAllItems(query?: Query): T[]; + + public getPutItem(value?: T, query?: Query): T { + if (!!value && value.hasOwnProperty('id') && value['id'] === undefined) { + // Setting an id for object creation simulation + if (typeof value['id'] === 'string') { + value['id'] = this.randUid; + } else if (typeof value['id'] === 'number') { + value['id'] = this.randId; + } + } + return value; } - public async getAll(query: Query): Promise { - return this.many; + public getPutAllItems(values?: T[], query?: Query): T[] { + return values; } +} - public async put(value: T, query: Query): Promise { - return this.one; +// ---> App scope + +export class MockUserDataSourceFactory extends DefaultMockItemDataSourceFactory { + + private createUser(query?: Query): UserModel { + const randId = query instanceof IdQuery ? query.id : Math.floor(Math.random() * 1000); + return new UserModel ( + randId, + `User_${randId}`, + ); } - public async putAll(values: T[], query: Query): Promise { - return this.many; + public getGetItem(query?: Query): UserModel { + return this.createUser(query); } - public async delete(query: Query): Promise { - return; + public getGetAllItems(query?: Query): UserModel[] { + return [ + this.createUser(query), + this.createUser(query), + this.createUser(query), + this.createUser(query), + this.createUser(query), + ]; } +} - public async deleteAll(query: Query): Promise { - this.logger.warning('[DEPRECATION] `deleteAll` will be deprecated. Use `delete` instead.'); +export class UserModel { + constructor( + public readonly id: number, + public readonly name: string, + ) { } } + +export function getLandingPageRepository( +): SingleDataSourceRepository { + const dataSource = new MockDataSource( + new MockUserDataSourceFactory(), + 1500, + ); + return new SingleDataSourceRepository(dataSource, dataSource, dataSource); +}