Skip to content

Commit

Permalink
chore: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Akurganow committed Nov 23, 2023
1 parent f8d138a commit 8f10f7e
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# faker
A set of classes for mocking known data types such as browser history, browser download list, person, domain name, Jira project, GitHub repository, etc.
A set of classes for mocking known data types such as browser history, browser download list, browser bookmarks, person, domain name, Jira project, GitHub repository, etc.

## Installation

Expand Down
54 changes: 54 additions & 0 deletions src/base-extended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ import MockUser, { MockUserItem } from './user'
import MockProject, { MockProjectItem } from './project'
import MockDomain, { MockDomainItem } from './domain'
import { BaseItemMock } from './base'
import { faker } from '@faker-js/faker'

export type SiteQuery = {
domain?: MockDomainItem
user?: MockUserItem
project?: MockProjectItem
}

/**
* Class representing a base site mock.
* @extends BaseItemMock
*/
export abstract class BaseSiteMock<T extends object> extends BaseItemMock<T, SiteQuery> {
public readonly user: MockUserItem
public readonly project: MockProjectItem
public readonly domain: MockDomainItem

/**
* The templates of the site.
*/
protected abstract readonly templates: Record<string, string[][]>

constructor(query?: SiteQuery) {
Expand All @@ -24,7 +32,53 @@ export abstract class BaseSiteMock<T extends object> extends BaseItemMock<T, Sit
this.project = this.query?.project ?? new MockProject(this.domain).getItem()
}

/**
* Get the available templates of the site.
*/
public get providers() {
return Object.keys(this.templates)
}
}

/**
* Class representing a base items mock.
* @extends BaseItemsMock
*/
export abstract class BaseItemsMock<T extends object, Q extends object> extends BaseItemMock<T, Q> {
items: T[] = []

/**
* Abstract method to create a mock item.
* @abstract
* @return The mock item
*/
protected abstract createMockItem(): T

/**
* Abstract method to create an array of mock items.
* @abstract
* @return The array of mock items
*/
protected abstract createMockItems(): T[]

/**
* Get a random item from the items array.
*/
public getItem(): T {
return faker.helpers.arrayElement(this.items)
}

/**
* Get all items.
*/
public getItems(): T[] {
return this.items
}

/**
* Reset the items array.
*/
public reset(): void {
this.items = this.createMockItems()
}
}
41 changes: 15 additions & 26 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
import { faker } from '@faker-js/faker'

export abstract class BaseItemsMock<T extends object, Q extends object> {
query: Q
items: T[] = []

protected abstract createMockItem(): T
protected abstract createMockItems(): T[]

constructor(query?: Q, defaultQuery: Q = {} as Q) {
this.query = query ?? defaultQuery
}

public getItem(): T {
return faker.helpers.arrayElement(this.items)
}

public getItems(): T[] {
return this.items
}

public reset(): void {
this.items = this.createMockItems()
}
}

/**
* Class representing a base item mock.
* @extends BaseItemMock
*/
export abstract class BaseItemMock<T extends object, Q extends object> {
query: Q
item: T = {} as T

/**
* Abstract method to create a mock item.
* @abstract
*/
protected abstract createMockItem(): T

constructor(query?: Q, defaultQuery: Q = {} as Q) {
this.query = query ?? defaultQuery
}

/**
* Get the mock item.
* @return The mock item
*/
public getItem(): T {
return this.item
}

/**
* Reset the item.
*/
public reset(): void {
this.item = this.createMockItem()
}
Expand Down
3 changes: 2 additions & 1 deletion src/bookmarks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { faker } from '@faker-js/faker'
import { WithRequired } from './types'
import { createBalancedArray } from '@plq/array-functions'
import { BaseItemsMock } from './base'

import { BaseItemsMock } from './base-extended'

export interface MockBookmarksItem extends chrome.bookmarks.BookmarkTreeNode {}
export interface MockBookmarksQuery extends chrome.bookmarks.BookmarkSearchQuery {
Expand Down
3 changes: 2 additions & 1 deletion src/downloads.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { faker } from '@faker-js/faker'

import { BaseItemsMock } from './base'

import { BaseItemsMock } from './base-extended'

export interface MockDownloadItem extends chrome.downloads.DownloadItem {}

Expand Down
2 changes: 1 addition & 1 deletion src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type { MockDomainItem } from './domain'
import type { MockUserItem } from './user'
import type { MockProjectItem } from './project'

import { BaseItemsMock } from './base'
import { isEmpty } from '@plq/is'
import { BaseItemsMock } from './base-extended'

export interface MockHistoryQuery extends chrome.history.HistoryQuery {}
export interface MockHistoryItem extends chrome.history.HistoryItem {}
Expand Down
11 changes: 11 additions & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ export interface MockUserItem {
fullName: string
}

/**
* Class representing a mock user.
*/
export default class MockUser extends BaseItemMock<MockUserItem, MockDomainItem>{
/**
* Create a mock user.
* @param {MockDomainItem} domain - The domain of the user
*/
constructor(domain?: MockDomainItem) {
super(domain, new MockDomain().getItem())

this.reset()
}

/**
* Create a mock user item.
* @return {MockUserItem} The mock user item
*/
createMockItem(): MockUserItem {
const firstName = faker.person.firstName()
const lastName = faker.person.lastName()
Expand Down

0 comments on commit 8f10f7e

Please sign in to comment.